Basic attempts and exercises on STM32 PWM and DAC

STM32's PWM first attempt

Introduction to PWM

PWM is the abbreviation of Pulse Width Modulation, which means pulse width modulation in Chinese, or pulse width modulation for short. It is a very effective technology that uses the digital output of a microprocessor to control analog circuits. It has the advantages of simple control, flexibility and good dynamic response, and has become the most widely used control method for power electronics. Its application areas include Measurement, communication, power control and conversion, motor control, servo control, dimming, switching power supply, and even some audio amplifiers, so learning PWM has very important practical significance. In fact, we can also understand that PWM is a method of digitally encoding the analog signal level. Through the use of high-resolution counters, the duty cycle of the square wave is modulated to encode the level of a specific analog signal. The PWM signal is still digital, because at any given moment, the full-scale DC power supply is either completely (ON) or completely absent (OFF). The voltage or current source is applied to the analog load in a repetitive pulse sequence of on (ON) or off (OFF). When it is on, it is when the DC power supply is added to the load, and when it is off, it is when the power supply is disconnected. As long as the bandwidth is sufficient, any analog value can be encoded using PWM.
Insert picture description here
 In addition to the basic timers TIM6 and TIM7 of STM32F1, other timers can generate PWM output. Among them, the advanced timers TIM1 and TIM8 can generate up to 7 PWM outputs at the same time. The PWM output is actually a square wave signal with adjustable pulse width (that is, duty cycle adjustment). The signal frequency is determined by the value of the automatic reload register ARR, and the duty cycle is determined by the value of the compare register CCR.
Insert picture description here
  There are a total of 8 PWM output compare modes, which are specifically configured by the bits OCxM[2:0] of the CCMRx register. We only explain the two most commonly used PWM output modes: PWM1 and PWM2. The
  usage of PWM1 and PWM2 is similar. The difference is that the polarity of the output level is different.

Use STM32 to output a PWM waveform

1. This experiment uses code to realize the simulation in keil5. The test project adopts the punctual atom complete project.
2. The specific waveform generation method.
Open the project configuration debugging tool
Insert picture description here
Insert picture description here
2. Open the debugging
Insert picture description here
3. Set and check the output pin of the GPIO to
find the logic analyzer ( This option is only available under the debug panel)
Insert picture description here
Insert picture description here
4. Run and observe the waveform
Insert picture description here
5. Main code part

int main(void)
 {
    
    		
 	u16 led0pwmval=0;
	u8 dir=1;	
	delay_init();	    	 //延时函数初始化	  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 	 //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
	uart_init(115200);	 //串口初始化为115200
 	LED_Init();			     //LED端口初始化
 	TIM3_PWM_Init(899,0);	 //不分频。PWM频率=72000000/900=80Khz
   	while(1)
	{
    
    
 		delay_ms(10);	 
		if(dir)led0pwmval++;
		else led0pwmval--;

 		if(led0pwmval>300)dir=0;
		if(led0pwmval==0)dir=1;										 
		TIM_SetCompare2(TIM3,led0pwmval);		   
	}	 
 }

Oscilloscope to observe the output waveform

Insert picture description here

STM32 DAC basic exercises

Monophonic realization

Here, Adobe Audition is used to directly generate a sine wave to achieve a single tone. The
Insert picture description here
settings are as follows.
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
This is a single tone that presents "drop...". Select the
appropriate playback length and save it in wav format.

DAC practice

1. Convert a piece of digital audio song data into analog audio waveform output (loop) 8khz, quantized 16bit, single channel, duration is only 5~10 seconds.
2. Open your favorite song, select the appropriate length and save it for 5~10s.
Insert picture description here
3. Save as wav format with the following settings
Insert picture description here
Insert picture description here

Observe the output

1. Open the wav file with UltraEdit.
Insert picture description here
2. CTRL+A (select all), right-click and select hexadecimal to copy the selected view, create a new file, and paste.
Insert picture description here
3. Find the part we need.
Find the bottom byte, right-click to select the range and enter the starting row number and column number, confirm that the entire content we need is selected, and copy and paste to create a new file.
Insert picture description here
4. Then open notepad++, copy the required part, and convert it to 0x format.
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
5. Copy the result to the project waveform data location.
Insert picture description here
Insert picture description here
6. Burn the program after compilation and observe the waveform
Insert picture description here

Guess you like

Origin blog.csdn.net/rude_dragon/article/details/112132509