STM32 outputs PWM waveform and realizes breathing light

1. Environment configuration

Software: STM32CubeMX:6.6.1
Keil 5.31
mcuisp
Hardware: STM32F103C8T6 core board

2. Introduction to PWM

PWM meaning:
PWM (Pulse Width Modulation) is pulse width modulation, referred to as pulse width modulation. It is a very effective technology that uses the digital output of the microprocessor to control the analog circuit; it is an analog control method that modulates the bias of the transistor base or MOS transistor gate according to the change of the corresponding load. To realize the change of the conduction time of the transistor or MOS tube, thereby realizing the change of the output of the switching regulated power supply.

The basic principle of PWM:
PWM is to control the on-off of the switching device of the inverter circuit, so that the output terminal can get a series of pulses with equal amplitude, and use these pulses to replace the sine wave or the required waveform. It can also be understood that PWM is a method of digitally encoding the level of an analog signal. 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, full-scale DC power is either completely on (ON) or not at all (OFF). A voltage or current source is applied to an analog load in a repetitive pulse sequence of ON or OFF. Any analog value can be encoded using PWM as long as the bandwidth is sufficient.

Advantages and application range of PWM:
Due to its advantages of simple control, flexibility and good dynamic response , it has become the most widely used control method in power electronics technology. Its application fields include measurement, communication, power control and conversion, motor control, servo control, regulation Light, switching power supply, and even some audio amplifiers, so learning PWM has very important practical significance.

3. Use STM32CubeMX to configure the project

1. Create a new project and perform basic configuration. Create a new project by clicking "ACCESS TO MCU SELECTOR":
insert image description here

2. Search the model, select the appropriate model to build the project
insert image description here
3. Set RCC
insert image description here

4. Set SYS
insert image description here

5. Configure timer 3. As shown in the picture:
insert image description here

6. Configure timer 4. As shown in the picture:
insert image description here

7. Click the "Clock Configuration" page first, and configure it from left to right according to the value in the red box below.
insert image description here

8. On the project management page "Project Manager", click "Code Generator" first, and select the following configuration:
insert image description here

9. Click "Project" again to configure as follows:
insert image description here

4. Use Keil to configure the code

Open the main. Add the specified code in the area as shown below:

uint16_t duty_num1 = 10;
uint16_t duty_num2 = 20;

insert image description here

HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_2);

insert image description here

while (1)
  {
    
    
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		HAL_Delay(50);
		duty_num1=duty_num1+10;
		duty_num2=duty_num2+20;
		if(duty_num1>500)
		{
    
    
			duty_num1=0;
		}
		__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_2,duty_num1);
		if(duty_num2>500)
		{
    
    
			duty_num2=0;
		}
		__HAL_TIM_SetCompare(&htim4,TIM_CHANNEL_2,duty_num2);
  }

insert image description here

5. Operation effect

TIMER_PWM

6. Use the logic simulator that comes with Keil to observe the duty cycle

The configuration will not be explained here, and you can refer to another blog:
https://blog.csdn.net/qq_55894922/article/details/127232999?spm=1001.2014.3001.5501
Observation results:

TIMER_PWM duty cycle change

7. Summary

This experiment successfully completed the output PWM waveform with STM32CubeMX and realized the breathing light. Learned a lot about PWM, and accumulated a little embedded knowledge.

8. References

http://www.mcublog.cn/stm32/2021_01/stm32cubemx-pwm-huxideng/
https://blog.csdn.net/qq_45237293/article/details/111997424
https://blog.csdn.net/zmhDD/article/details/111942507
https://blog.csdn.net/qq_55894922/article/details/127232999?spm=1001.2014.3001.5501

Guess you like

Origin blog.csdn.net/qq_55894922/article/details/127583737