Today, my biggest feeling is that the Internet is a huge treasure trove. I am really fortunate that in this complex social environment, in an era full of selfishness and self-interest, many netizens on various websites and forums always maintain an open source atmosphere. Learning must be communicated with others, and the Internet provides such an excellent platform.
Let’s stop talking nonsense and get down to business.
Function: Use channel 2 of timer 2 to make PA1 output PWM waveform with frequency 1K and duty cycle 40, and use PA8 to delay and invert the LED light at will to indicate program running.
First, let's get familiar with the PWM related parts of the timer.
In fact, PWM is just a comparison function of the timer.
The value in CNT keeps ++ing, and once it is equal to the value in CCRX register, the corresponding action will be taken. This is very similar to AVR microcontroller. In this case, if we want to generate the required PWM signal, we need to set the PWM frequency and PWM duty cycle.
First, let's talk about determining the frequency. Since the clock source of the general timer is PCLK1, and I like to use the default settings of the firmware library, the clock frequency of the timer is determined as follows:
AHB (72MHz) → APB1 divider (default 2) → APB1 clock signal (36MHz) → multiplier (*2 times) → general timer clock signal (72MHz).
The reason why this is the case is recorded in detail in the RCC module learning record, so I won’t go into details.
Therefore, the CK_PSC in the figure is 72MHz.
The following information is also a lot of online search, I will list it:
The STM32 PWM output has two modes, Mode 1 (PWM1) and Mode 2 (PWM2), which are determined by the OCxM bit in the TIMx_CCMRx register ("110" is Mode 1, "111" is Mode 2). The differences between Mode 1 and Mode 2 are as follows:
110: PWM mode 1 - When counting up, channel 1 is at an invalid level (OC1REF=0) once TIMx_CNT=TIMx_CCR1, otherwise it is at a valid level (OC1REF=1).
111: PWM mode 2 - When counting up, channel 1 is at a valid level once TIMx_CNT = TIMx_CCR1, otherwise it is at an invalid level.
From this point of view, Mode 1 and Mode 2 are complementary and opposite to each other, so there is not much difference in their use. I use Mode 1, so the following settings are all set according to Mode 1.
The PWM cycle is determined by the timer's auto-reload value and the CNT count frequency. The CNT count clock is obtained by CK_PSC through the divider PSC, so the CNT clock is CK_PSC/division factor. This division factor is determined by TIM_TimeBaseStructure.TIM_Prescaler. The value I set is 72, so the CNT count frequency, that is, the frequency of CK_CNT, is 1MHz.
The next step is to determine the timer auto-reload value. Because CNT will automatically clear to zero every time it is added to the value of the ARR register, of course, the premise is set to up-counting mode, and the PWM period is changed according to this overflow event. Therefore, the frequency of the PWM signal is determined by the value of ARR. The value I set is 1000-1, that is, TIM_TimeBaseStructure.TIM_Period = 1000-1; therefore, the PWM period is 1MHz/1000=1KHz.
Next, we need to determine the duty cycle of PWM. Because CNT will constantly compare the value of CRRX when adding itself to the ARR value, once the two are equal, a match event will occur, but please note that CNT will not pay attention to this, it will continue to ++ until it is equal to ARR. I set the value of CRRX to 400-1, so the duty cycle is determined to be 40%.
Okay, here is the configuration of the library function.
TIMER output PWM implementation steps
1.
2.
3.
4.
First, the main function and global variables are declared. It is very simple and will not be explained.
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TimOCInitStructure;
int main(void)
{
//
}
The following is the configuration of the IO port:
void gpio_cfg()
{
}
What needs to be noted here is that the PWM output port must be configured as multiplexed push-pull output. I don’t know the reason, but I just copy it anyway.
[page]
The following is the TIM configuration function. The comments are very clear and no further explanation is required:
void tim2_cfg()
{
}
Next is the key PWM configuration function:
void pwm_cfg()
{
}
The output comparison unit structure of the stm32 firmware library is defined separately from the time base unit of the timer, and the PWM mode is just a value of the output comparison structure member TimOCInitStructure.TIM_OCMode. After filling this structure, it must be mapped to a timer and implemented with the TIM_OCXInit function. I used an X to indicate that there is more than one such function. In fact, the general timer of stm32 has four channels, and each channel corresponds to an initialization function. It's really tangled here! Finally, the PWM output function of the timer must be enabled. Note that the TIM_CtrlPWMOutputs (TIM2, ENABLE) function is outputs instead of output, which means that TIM2 has more than one channel! It's complicated and cumbersome!
The following is the structure prototype of the output comparison unit:
typedef struct
{
} TIM_OCInitTypeDef;
The members without color are only for advanced timers, and do not need to be taken care of by general timers.
There is also a member called TimOCInitStructure.TIM_OCPolarity that needs attention. What is its function? The information I found online is as follows:
As mentioned above, PWM has two modes: PWM1 and PWM2. These two modes can only control up to OCXREF. TIM_OCPolarity can control whether OC1 is directly equal to OCXREF or reverse polarity! OC1 is the final PWM signal.
Here is a small episode. I used an oscilloscope to measure the PWM signal and found that the signal was bipolar. Then I changed TIM_OCPolarity and measured it again. It was still bipolar, just reversed. I really thought that the stm32 microcontroller could output bipolar PWM. Later, I changed the oscilloscope to DC gear (it was AC gear before), and the waveform moved vertically from zero potential. Pay attention next time!
Previous article:Detailed explanation of 8 modes of GPIO in STM32
Next article:Installation of arm-linux 4.3.2 cross toolchain
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- About the problem of AGND and DGND of microcontroller
- "Summer Benefits to Celebrate Dragon Boat Festival" Selected Resources for the Grab-a-Hot Event
- Reshaping the engineer experience: Tektronix invites you to attend the global launch of the new 2-series MSO at 2 pm on June 8 (today)!
- How to draw multiple boards with the same functional schematics on one PCB
- Quickly build the Hongmeng development environment for BearPi HM Nano
- Variable memory depth and segmented memory of LOTO oscilloscope
- [SAMR21 new gameplay] 21. Touchio
- STM32 learning the ninth post can not turn off the PWM pin
- I have a SOT23-6 package with a silk screen model number.
- 【CH579M-R1】+PWM breathing light and serial communication experiment