The STM32 timer is a powerful module, and the frequency of the timer is also very high. The timer can do some basic timing, and can also do PWM output or input capture functions.
Clock source problem:
There are eight TIMx, of which TIM1 and TIM8 are connected to the APB2 bus, while TIM2-TIM7 are connected to the
On the APB1 bus. TIM1 & TIM8 are called advanced control timers. The APB2 bus where they are located is also better than the APB1 bus. APB2 can work at 72MHz, while the maximum of APB1 is 36MHz.
The timer clock does not come directly from APB1 or APB2, but comes from a frequency multiplier whose input is APB1 or APB2.
The function of this frequency multiplier is explained below using the clocks of timers 2 to 7: when the pre-division coefficient of APB1 is 1, this frequency multiplier does not work, and the clock frequency of the timer is equal to the frequency of APB1; when the pre-division coefficient of APB1 is other values (that is, the pre-division coefficient is 2, 4, 8 or 16), this frequency multiplier works, and the clock frequency of the timer is equal to twice the frequency of APB1.
Assume AHB=36MHz, because the maximum frequency allowed by APB1 is 36MHz, the pre-division coefficient of APB1 can take any value; when the pre-division coefficient = 1, APB1 = 36MHz, the clock frequency of TIM2~7 = 36MHz (the multiplier does not work); when the pre-division coefficient = 2, APB1 = 18MHz, under the action of the multiplier, the clock frequency of TIM2~7 = 36MHz.
Some people may ask, since the clock frequency of TIM2~7 is required to be 36MHz, why not directly use the pre-scaling coefficient of APB1 = 1? The answer is: APB1 not only provides clocks for TIM2~7, but also provides clocks for other peripherals; setting this multiplier can ensure that TIM2~7 can still get a higher clock frequency when other peripherals use a lower clock frequency.
Another example: when AHB=72MHz, the pre-scaling factor of APB1 must be greater than 2, because the maximum frequency of APB1 can only be 36MHz. If the pre-scaling factor of APB1 = 2, then because of this multiplier, TIM2~7 can still get a clock frequency of 72MHz. Being able to use a higher clock frequency undoubtedly improves the resolution of the timer, which is also the original intention of designing this multiplier.
TIM general timer configuration steps:
1. Configure the TIM clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
2. Basic configuration of timer
void TIM2_Configuration(void)
{
}
TIM_Period sets the value of the period of the auto-reload register that is loaded into the activity at the next update event. Its value must be between 0x0000 and 0xFFFF.
TIM_Prescaler sets the prescaler value used as the divisor of the TIMx clock frequency. Its value must be between 0x0000 and 0xFFFF.
The function of TIM_ClockDivision is to make a delay, which is usually used in special occasions, but you don't need to worry about it.
TIM_CounterMode selects the counter mode.
TIM_CounterMode_Up
TIM up counting mode
TIM_CounterMode_Down
TIM down counting mode
TIM_CounterMode_CenterAligned1
TIM_CounterMode_CenterAligned2
TIM_CounterMode_CenterAligned3
The MCU clock frequency is 72MHz, APB1 is divided by 2 to 36MHz, so TIM2 automatically doubles the frequency to 72MHz, so the timer interrupt frequency is 72000000/36000/5=400Hz
3. Enable timer interrupt TIM_Cmd(TIM2, ENABLE);
4. Configure NVIC.
5. Write interrupt function
void TIM2_IRQHandler(void)
{
......//Interrupt processing
}
STM32 |
[2012-3-5 7:44:00 | By: zhpg009 ]
|
|
STM32 has 8 registers, including two advanced timers TIM1 and TIM8, two basic timers TIM6 and TIM7, and four general timers TIM2-TIM5. The timers are completely independent and do not share any resources with each other. They can be operated synchronously together. All TIMx timers are connected internally for timer synchronization or linking. When a timer is in master mode, it can reset, start, stop or provide clock to the counter of another timer in slave mode. Timer clock: The counter clock can be provided by the following clock sources: The specific selection of these clocks can be set by the relevant bits of the TIMx_SMCR register. The CK_INT clock here is from the APB1 multiplier. Unless the clock division number of APB1 is set to 1, the clock of the general timer TIMx is twice the clock of APB1. When the clock of APB1 is not divided, the clock of the general timer TIMx is equal to the clock of APB1. It should also be noted that the clock of the advanced timer does not come from APB1, but from APB2. The core of the timer: When it comes to the core of the timer, there are naturally two things that are indispensable. One is the counting clock (how long it takes to count once), and the other is how many overflows are counted. These two together determine the overflow time. The count clock of the timer comes from APB1 or APB2 according to the different timers. To put it simply, the count clock is to divide one second into many parts. However, since the bus clock is generally tens of megahertz, the divided APB is also tens of megahertz, so the APB needs to be divided to a lower frequency, which requires setting the pre-dividing register. For example, the current APB1 is 36MHz. The bold paragraph above has already said that unless the clock division number of APB1 is set to 1, the clock of the general timer TIMx is twice the clock of APB1. At this time, the TIMx clock is 72MHz. Therefore, to divide it to 10KHz, the pre-dividing register TIMx_PSC (as shown below) needs to be set to 7199. Why is it 7199 instead of 7200? The following register introduction explains this point: the counter clock CK_CNT is equal to the TIMx clock/(PSC+1), so you only need to set the register value to 7199. Here, the frequency of 10KHz is equivalent to dividing one second into 10,000 parts, that is, 0.0001 seconds, and the timer increments every 0.0001 seconds. Note: Because PSC is a 16-bit register, the value range is 0-65535. The counter automatically reloads the register TIMx_PSC, which stores the number of times the counter is to increase (how many times the counter overflows). Note: Because ARR is also a 16-bit register, the value range is 0-65535. In this way, these two registers determine the overflow time. Continuing with the above example, if the ARR register value is set to 5000, it means that the timer increases every 0.0001 seconds, for a total of 5000 times, which means it overflows once every 0.5 seconds. In summary, the timer overflow formula is: Overflow time (seconds) = General timer 3 initialization function: void { RCC->APB1ENR|=1<<1; //TIM3 clock enable TIM3->PSC=psc; //These two registers must be set at the same time to use the interrupt TIM3->DIER|=1<<0; TIM3->DIER|=1<<6; TIM3->CR1|=0x01; } This function is the initialization function of TIM3. The main function enters an infinite loop waiting for the TIM3 overflow interrupt. When the value of TIM3_CNT is equal to the value of TIM3_ARR, a TIM3 update interrupt will be generated. After the interrupt program is executed in the interrupt, TIM3_CNT TIMx_CNT register: This register is the timer counter, which stores the number of times the current timer has counted. The control register 1 (TIMx_CR1) When the timer overflows, |
Previous article:STM32 AWU RTC alarm wakes up CPU from stop mode
Next article:STM32 eight-channel AD conversion is successfully debugged with DMA transfer, and DMA transfer is not misaligned
- Popular Resources
- Popular amplifiers
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- I decided to quit my job and develop my operating system full-time (transferred)
- Design rules for four-layer PCB
- If there is no reference ground at all, do you think it is still possible to control the impedance?
- Why is the stm32 pin reading different from the oscilloscope measurement?
- msp430f5529 uart pwm adc
- Please help me how to install the PIC software EPOCH. The csdn is very vague and I have never seen the operation described in the installation instructions.
- Understanding NB-IoT technology
- 3DH Model
- What kind of products need a dedicated shutdown discharge circuit?
- [First Round] Interview Questions for Embedded Engineers