Table of contents
2. CubuMX configures the timer to output PWM
1. Chip selection (stm32f103c8t6)
3. Program engineering code analysis
1. TIM1_channel1 initialization
2. The interrupt callback function realizes the breathing light
4. Experimental effect display
1. PWM pulse width modulation
1. What is PWM
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.
PWM is to control the on-off of the switching device of the inverter circuit, so that the output terminal gets a series of pulses with equal amplitude, and these pulses are used 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.
PWM Pulse Width Modulation Schematic
2. How to generate PWM signal
(1) The PWM signal can be output directly through the internal module of the chip. The premise is that the I/O port has an integrated module, and only a few simple steps are required. This kind of functional module with PWM output is easier to program. At the same time the data is more accurate.
(2) But if there is no PWM function module inside the IC, or if the requirements are not very high, you can use the I/O port to set some parameters to output PWM signals, because PWM signals are actually a combination of a series of high and low levels. The specific method is to add a timer to the I/O. For the frequency of the PWM signal you require to output is consistent with your timer, use the timer interrupt to count, but this method is generally not used, unless the requirements for accuracy, frequency, etc. are not Highly achievable.
2. CubuMX configures the timer to output PWM
1. Chip selection (stm32f103c8t6)
2. Clock configuration
Select crystal/ceramic oscillator as external high speed clock (HSE) input
System clock number configuration
3. Configure timer output PWM
Configure PA8 as PWM output IO port
4. Export project
3. Program engineering code analysis
1. TIM1_channel1 initialization
MX_TIM1_Init(); //Timer 1 initialization function
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1); //Start tim1 channel 1 timer
Only when the timer interrupt timer is turned on will it start working and counting
2. The interrupt callback function realizes the breathing light
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1,i); //PWM output duty ratio setting function
MX_GPIO_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1); //开启tim1通道1计时器
/* USER CODE END 2 */
int i=0,flag=0;
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(flag==0) //0状态占空比按+10的步长逐渐增大
{
i+=10;
if(i>400)
flag=1;
}
else //1状态占空比按-10的步长逐渐减小
{
i-=10;
if(i<5)
flag=0;
}
/*PAB2总线时钟为72Mhz,经过72分频后为1Mhz,计数周期RCC为65535
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1,i); //设置该PWM输出占空比为变量i
/* USER CODE BEGIN 3 */
}
Project code file portal: project code
4. Experimental effect display
PWM_LED
V. Summary
The breathing light mainly changes the IO output voltage through PWM pulse width modulation to realize the gradual change of LED brightness. PWM is a very useful thing, and it may be often encountered or used in future engineering designs. So I hope that future readers can learn about PWM through this small experiment.
6. Reference link
Use STM32 to output PWM waveform
PWM (Pulse Width Modulation) Signal Principle
STM32CUBEMX_PWM-based breathing light