To create a sound, we need to make the speaker membrane oscillate with a certain frequency. Each note corresponds to its own frequency, for example, a note To 1 octave, corresponds to a frequency of 261 Hz. Those. jerking the foot of the microcontroller connected to the speaker, at a speed of 261 times per second, we will hear the sound of this note. For those who are not strong in musical theory, the sound closer from 1kHz and higher will be more squeaky, below 300Hz will bass.
//--------------------------------------------------------- // , . //--------------------------------------------------------- #define PER_CLOCK_PORTE (1<<25) // E. #define PORT_OE_OUT_PORTE_0 (1<<0) // PORTE_0 "". #define ANALOG_EN_DIGITAL_PORTE_0 (1<<0) // PORTE_0. #define PWR_MAX_PORTE_0 (3<<0) // PORTE_0 . #define PORT_RXTX_PORTE_0_OUT_1 (1<<0) // "1" . void Buzzer_out_init (void) { RST_CLK->PER_CLOCK |= PER_CLOCK_PORTE; // E. PORTE->OE |= PORT_OE_OUT_PORTE_0; //. PORTE->ANALOG |= ANALOG_EN_DIGITAL_PORTE_0; //. PORTE->PWR |= PWR_MAX_PORTE_0; // ( 10 ). }
void Init_SysTick (void) // 10^(-5) . { SysTick->LOAD = (8000000/100000)-1; SysTick->CTRL |= CLKSOURCE|TCKINT|ENABLE; } volatile uint32_t Delay_dec = 0; // SysTick . void SysTick_Handler (void) { if (Delay_dec) Delay_dec--; } void Delay (uint32_t Delay_Data) // SysTick . { Delay_dec = Delay_Data; while (Delay_dec) {}; }
int main (void) { Buzzer_out_init(); // . Init_SysTick(); // 10^(-5) . while (1) // 261.63 . { PORTE->RXTX |= PORT_RXTX_PORTE_0_OUT_1; // "1" , . Delay(191); PORTE->RXTX = 0; Delay(191); } }
int main (void) { HSE_Clock_ON(); // HSE . HSE_Clock_OffPLL(); // "" HSE . Buzzer_out_init(); // . Init_SysTick(); // 10^(-5) . while (1) // 261.63 . { PORTE->RXTX |= PORT_RXTX_PORTE_0_OUT_1; // "1" , . Delay(191); PORTE->RXTX = 0; Delay(191); } }
const uint32_t MES[13] = {191, 180, 170, 161, 152, 143, 135, 128, 120, 114, 107, 101, 96};
int main (void) { HSE_Clock_ON(); // HSE . HSE_Clock_OffPLL(); // "" HSE . Buzzer_out_init(); // . Init_SysTick(); // 10^(-5) . while (1) // 261.63 . { for (uint32_t Nambe = 0; Nambe<13; Nambe++) // . { for (uint32_t LoopN = 0; LoopN<MES[12-Nambe]*3; LoopN++) // . { PORTE->RXTX |= PORT_RXTX_PORTE_0_OUT_1; // "1" , . Delay(MES[Nambe]); PORTE->RXTX = 0; Delay(MES[Nambe]); } } } }
MDR_DAC controller ................................................ .................................................. ......................... 326
The microcontroller has two DACs. To enable the DAC, you need to set the Cfg_ON_DACx bit to 1, the used D / A port pins of the E port have been configured as analog and any internal braces have been disabled. Both DACs can work independently or jointly. When the DAC operates independently (Cfg_SYNC_A bit = 0), after writing data to the DACx_DATA data register, the voltage level corresponding to the recorded value is generated at the DACx_OUT output. During synchronous operation (Cfg_SYNC_A = 1 bit), the data of both DACs can be updated with one entry in one of the DACx_DATA registers. The DAC can operate from the internal support Cfg_M_REFx = 0, then the DAC generates an output signal in the range from 0 to the supply voltage AUCC. In the mode of operation with external support, Cfg_M_REFx = 1, the DAC generates an output voltage in the range from 0 to the value DACx_REF.
//--------------------------------------------------------- //. //--------------------------------------------------------- #define PCLK_EN_DAC (1<<18) // . #define CFG_Cfg_ON_DAC0 (1<<2) // 1. void ADC_Init (void) { RST_CLK->PER_CLOCK |= PCLK_EN_DAC; // . DAC->CFG = CFG_Cfg_ON_DAC0; // 1. . . }
void Buzzer_out_DAC_init (void) { RST_CLK->PER_CLOCK |= PER_CLOCK_PORTE; // E. PORTE->OE |= PORT_OE_OUT_PORTE_0; //. PORTE->ANALOG = 0; //. PORTE->PWR |= PWR_MAX_PORTE_0; // ( 10 ). }
int main (void) { HSE_Clock_ON(); // HSE . HSE_Clock_OffPLL(); // "" HSE . Buzzer_out_DAC_init(); // . ADC_Init(); // . Init_SysTick(); // 10^(-5) . DAC->DAC1_DATA = 0xFFF; // (). while (1) { } }
//--------------------------------------------------------- //. //--------------------------------------------------------- #define PCLK_EN(DAC) (1<<18) // . #define CFG_Cfg_ON_DAC0 (1<<2) // 1. #define CFG_Cfg_ON_DAC1 (1<<3) void ADC_Init (void) { RST_CLK->PER_CLOCK |= PCLK_EN(DAC); // . DAC->CFG = CFG_Cfg_ON_DAC1; // 2. . . }
const uint32_t MES[13] = {191, 180, 170, 161, 152, 143, 135, 128, 120, 114, 107, 101, 96}; int main (void) { HSE_Clock_ON(); // HSE . HSE_Clock_OffPLL(); // "" HSE . Buzzer_out_DAC_init(); // . ADC_Init(); // . Init_SysTick(); // 10^(-5) . while (1) { for (uint32_t Nambe = 0; Nambe<13; Nambe++) // . { for (uint32_t LoopN = 0; LoopN<MES[12-Nambe]*3; LoopN++) // . { DAC->DAC2_DATA = 0xFFF; Delay(MES[Nambe]); DAC->DAC2_DATA = 0; Delay(MES[Nambe]); } } } }
Program Sin_wav; Var Real_Data, PR: double; // . samplerate: word; // . wavefrequency: double;// . Loop: word; //. Name: string; // . Begin write(' : '); readln(samplerate); // . write(' : '); readln(wavefrequency); write(' : '); readln(Name); write('const uint16_t ', Name, '[', samplerate, '] = {'); PR:=samplerate/2; // . for Loop:=0 to samplerate-1 do //-1, .. 0. Begin Real_Data := 2047*sin(Loop*pi/PR) + 2047; // sine-. //.. - 0 . // 2048-1 ( 0 4095) = 0, 2045 = -2. //2047 - . +, -. 0. write(Round(Real_Data)); if (Loop<>samplerate-1) then write(', '); End; write('};') End.
: 100 : 261.63 : C_4 const uint16_t C_4[100] = {2047, 2176, 2304, 2431, 2556, 2680, 2801, 2919, 3033, 3144, 3250, 3352, 3448, 3539, 3624, 3703, 3775, 3841, 3899, 3950, 3994, 4030, 4058, 4078, 4090, 4094, 4090, 4078, 4058, 4030, 3994, 3950, 3899, 3841, 3775, 3703, 3624, 3539, 3448, 3352, 3250, 3144, 3033, 2919, 2801, 2680, 2556, 2431, 2304, 2176, 2047, 1918, 1790, 1663, 1538, 1414, 1293, 1175, 1061, 950, 844, 742, 646, 555, 470, 391, 319, 253, 195, 144, 100, 64, 36, 16, 4, 0, 4, 16, 36, 64, 100, 144, 195, 253, 319, 391, 470, 555, 646, 742, 844, 950, 1061, 1175, 1293, 1414, 1538, 1663, 1790, 1918};
void Init_SysTick (void) // 10000000 . { SysTick->LOAD = (80000000/10000000)-1; SysTick->CTRL |= CLKSOURCE|TCKINT|ENABLE; } const uint16_t C_4[100] = {2047, 2176, 2304, 2431, 2556, 2680, 2801, 2919, 3033, 3144, 3250, 3352, 3448, 3539, 3624, 3703, 3775, 3841, 3899, 3950, 3994, 4030, 4058, 4078, 4090, 4094, 4090, 4078, 4058, 4030, 3994, 3950, 3899, 3841, 3775, 3703, 3624, 3539, 3448, 3352, 3250, 3144, 3033, 2919, 2801, 2680, 2556, 2431, 2304, 2176, 2047, 1918, 1790, 1663, 1538, 1414, 1293, 1175, 1061, 950, 844, 742, 646, 555, 470, 391, 319, 253, 195, 144, 100, 64, 36, 16, 4, 0, 4, 16, 36, 64, 100, 144, 195, 253, 319, 391, 470, 555, 646, 742, 844, 950, 1061, 1175, 1293, 1414, 1538, 1663, 1790, 1918}; volatile uint16_t Loop = 0; volatile uint32_t Delay_dec = 0; // SysTick . void SysTick_Handler (void) { Delay_dec++; if (Delay_dec==(382-1)) { DAC->DAC2_DATA = C_4[Loop]; if (Loop<99) Loop++; else Loop = 0; Delay_dec=0; } }
int main (void) { HSE_Clock_ON(); // HSE . HSE_Clock_OffPLL(); // "" HSE . Buzzer_out_DAC_init(); // . ADC_Init(); // . HSE_PLL(10); //8 -> 80 . Init_SysTick(); // . while (1) { } }
void Init_SysTick (void) // 10000000 . { SysTick->LOAD = (80000000/1000000)-1; SysTick->CTRL |= CLKSOURCE|TCKINT|ENABLE; } const uint16_t C_4[100] = {2047, 2176, 2304, 2431, 2556, 2680, 2801, 2919, 3033, 3144, 3250, 3352, 3448, 3539, 3624, 3703, 3775, 3841, 3899, 3950, 3994, 4030, 4058, 4078, 4090, 4094, 4090, 4078, 4058, 4030, 3994, 3950, 3899, 3841, 3775, 3703, 3624, 3539, 3448, 3352, 3250, 3144, 3033, 2919, 2801, 2680, 2556, 2431, 2304, 2176, 2047, 1918, 1790, 1663, 1538, 1414, 1293, 1175, 1061, 950, 844, 742, 646, 555, 470, 391, 319, 253, 195, 144, 100, 64, 36, 16, 4, 0, 4, 16, 36, 64, 100, 144, 195, 253, 319, 391, 470, 555, 646, 742, 844, 950, 1061, 1175, 1293, 1414, 1538, 1663, 1790, 1918}; volatile uint16_t Loop = 0; volatile uint32_t Delay_dec = 0; // SysTick . void SysTick_Handler (void) { Delay_dec++; if (Delay_dec==(38-1)) { DAC->DAC2_DATA = C_4[Loop]; if (Loop<99) Loop++; else Loop = 0; Delay_dec=0; } }
: 20 : 261.63 : C_4 const uint16_t C_4[20] = {2047, 2680, 3250, 3703, 3994, 4094, 3994, 3703, 3250, 2680, 2047, 1414, 844, 391, 100, 0, 100, 391, 844, 1414};
void Init_SysTick (void) // 1000000 . { SysTick->LOAD = (80000000/1000000)-1; SysTick->CTRL |= CLKSOURCE|TCKINT|ENABLE; } const uint16_t C_4[20] = {2047, 2680, 3250, 3703, 3994, 4094, 3994, 3703, 3250, 2680, 2047, 1414, 844, 391, 100, 0, 100, 391, 844, 1414}; volatile uint16_t Loop = 0; volatile uint32_t Delay_dec = 0; // SysTick . void SysTick_Handler (void) { Delay_dec++; if (Delay_dec==(191-1)) { DAC->DAC2_DATA = C_4[Loop]; if (Loop<19) Loop++; else Loop = 0; Delay_dec=0; } }
Program Sin_wav; Var Real_Data, PR: double; // . samplerate: word; // . wavefrequency: double;// . Loop: word; //. Name: string; // . Begin write(' : '); readln(samplerate); // . write(' : '); readln(wavefrequency); write(' : '); readln(Name); write('const uint16_t ', Name, '[', samplerate, '] = {'); PR:=samplerate/2; // . for Loop:=0 to samplerate-1 do //-1, .. 0. Begin Real_Data := 70*sin(Loop*pi/PR) + 2047; // sine-. //.. - 0 . // 2048-1 ( 0 4095) = 0, 2045 = -2. //2047 - . +, -. 0. write(Round(Real_Data)); if (Loop<>samplerate-1) then write(', '); End; write('};') End.
const uint16_t C_4[20] = {2047, 2069, 2088, 2104, 2114, 2117, 2114, 2104, 2088, 2069, 2047, 2025, 2006, 1990, 1980, 1977, 1980, 1990, 2006, 2025};
: 100 : 261.63 : C_4 const uint16_t C_4[100] = {2047, 2051, 2056, 2060, 2064, 2069, 2073, 2077, 2081, 2085, 2088, 2092, 2095, 2098, 2101, 2104, 2106, 2108, 2110, 2112, 2114, 2115, 2116, 2116, 2117, 2117, 2117, 2116, 2116, 2115, 2114, 2112, 2110, 2108, 2106, 2104, 2101, 2098, 2095, 2092, 2088, 2085, 2081, 2077, 2073, 2069, 2064, 2060, 2056, 2051, 2047, 2043, 2038, 2034, 2030, 2025, 2021, 2017, 2013, 2009, 2006, 2002, 1999, 1996, 1993, 1990, 1988, 1986, 1984, 1982, 1980, 1979, 1978, 1978, 1977, 1977, 1977, 1978, 1978, 1979, 1980, 1982, 1984, 1986, 1988, 1990, 1993, 1996, 1999, 2002, 2006, 2009, 2013, 2017, 2021, 2025, 2030, 2034, 2038, 2043};
Source: https://habr.com/ru/post/255513/
All Articles