TIM—Basic Timer

Timer classification

      In the STM32F1 series, in addition to the interconnected products, there are a total of 8 timers , which are divided into basic timers, general timers and advanced timers .

      The basic timers TIM6 and TIM7 are 16-bit timers that can only count up, can only be timed, and have no external IO.

      The general- purpose timer TIM2/3/4/5 is a 16-bit timer that can count up/down. It can be timed, output comparison, and input capture. Each timer has four external IOs.

      The advanced timer TIM1/8 is a 16-bit timer that can count up/down. It can be timed, can output comparison, can input capture, and can also have three-phase motor complementary output signals. Each timer has 8 external IOs. More specific classification details are shown in Fig.

1

Basic timer function block diagram to explain

     The core of the basic timer is the time base, not only the basic timer, but also the general timer and the advanced timer. When learning timers, we start with simple basic timers, and in the later learning of general and advanced timers, we can just skip the explanation of the time base part. The functional block diagram of the basic timer is shown in Fig.

2

1. Clock source

      The timer clock TIMxCLK, the internal clock CK_INT, is provided by the APB1 prescaler after frequency division. If the APB1 prescaler coefficient is equal to 1, the frequency remains unchanged, otherwise the frequency is multiplied by 2, and the APB1 prescaler coefficient in the library function It is 2, that is, PCLK1=36M, so the timer clock TIMxCLK=36*2=72M.

2. Counter Clock

      After the timer clock passes through the PSC prescaler, that is, CK_CNT, it is used to drive the counter to count. PSC is a 16-bit prescaler, which can divide the timer clock TIMxCLK by any number between 1~65536. The specific calculation method is: CK_CNT=TIMxCLK/(PSC+1).

3. Counter

The counter CNT is a 16-bit counter that can only count up, and the maximum count value is 65535. When the count reaches the auto-reload register, an update event is generated, and the count is reset from the beginning.

4. Auto-reload registers

      The auto-reload register ARR is a 16-bit register that holds the maximum value that the counter can count. When the count reaches this value, if the interrupt is enabled, the timer will generate an overflow interrupt.

5. Calculation of timing time

      The timing of the timer is equal to the interrupt period of the counter multiplied by the number of interrupts. Driven by CK_CNT, the time to count a number is the reciprocal of CK_CLK, which is equal to: 1/(TIMxCLK/(PSC+1)), and the time to generate an interrupt is equal to: 1/(CK_CLK * ARR). If a variable time is set in the interrupt service routine to record the number of interrupts, then it can be calculated that the timing time we need is equal to: 1/CK_CLK * (ARR+1)*time.

Detailed explanation of timer initialization structure

      In the standard library function header file stm32f4xx_tim.h, four initialization structures are established for the timer peripherals. The basic timer only uses one of them, TIM_TimeBaseInitTypeDef. For details, see the code list. The other three are explained in the advanced timer chapter. .

 typedef struct {
 uint16_t TIM_Prescaler; // prescaler 
 uint16_t TIM_CounterMode; // count mode 
 uint32_t TIM_Period; // timer period 
 uint16_t TIM_ClockDivision; // clock divider 
 uint8_t TIM_RepetitionCounter; // repeat calculator
 } TIM_TimeBaseInitTypeDef;

      (1) TIM_Prescaler: Timer prescaler setting, the clock source is the timer clock after the prescaler, which sets the value of the TIMx_PSC register. The settable range is 0 to 65535 to achieve a frequency division of 1 to 65536.

      (2) TIM_CounterMode: Timer counting mode, but it is counting up, counting down, and three center-aligned modes. The basic timer can only count up, that is, TIMx_CNT can only be incremented from 0, and no initialization is required.

      (3) TIM_Period: timer period, which is actually to set the value of the auto-reload register and update it to the shadow register when the event is generated. The settable range is 0 to 65535.

       (4) TIM_ClockDivision: Clock division, set the frequency division ratio of the timer clock CK_INT frequency and the sampling clock frequency of the digital filter. The basic timer does not have this function and does not need to be set.

      (5) TIM_RepetitionCounter: Repetition counter, which belongs to the special register bit of the advanced control register. It can be used to control the number of output PWM very easily. No settings are required here. Although the timer basic initialization structure has 5 members, you only need to set two of them for the basic timer. It is easy to think about using the basic timer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325707263&siteId=291194637