The most basic function of Timer is timing, such as sending USART data and collecting AD data. If the timer and GPIO combined with the use of very rich functions can be achieved, can measure the pulse width of the input signal, can produce output waveform. Timer production PWM control motor state is a common method of industrial control, this knowledge is very necessary to understand. STM32F4xx series controllers have 2 advanced control timers, 10 general timers and 2 basic timers.

The clock frequency of the universal timer is determined by the frequency division coefficient of APB1. If the pre-division coefficient of APB1 is 1, the clock frequency of the universal timer is equal to that of APB1; otherwise, it is twice that of APB1.

The clock source

Timers must have a clock source in order to count,Basic timer clock can only come from internal clock, advanced control timer and universal timer can also choose from external clock source or directly from other timer wait mode. We can set the clock frequency of all timers through the TIMPRE bit of the RCC special clock configuration register (RCC_DCKCFGR). We generally set this bit to the default value 0, making the table optionalThe maximum timer clock is 90MHz, that is, the internal clock (CK_INT) frequency of the basic timer is 90MHz.The basic timer can only use the internal clockWhen TIM6 and TIM7 control CEN position 1 of register 1(TIMx_CR1), start the basic timer, and the clock source of the precrossover is CK_INT. For advanced control timer and universal timer clock source can find controller external clock, other timer and so on mode, more complex.When the SystenInit function is used for initialization, the clock frequencies are as follows: SYSCLK = 72M AHB = 72M APB1= 36M APB1= AHB/APB1=2 CK_INT clock frequency = 72M The final frequency of the counter also needs to be calculated by PSC predivision

counter

The basic timer counting process mainly involves three registers, respectivelyCounter register (TIMx_CNT),Predivider register (TIMx_PSC),Automatic Overload register (TIMx_ARR), these three registers are 16 significant digits and can be set to values from 0 to 65535.

Timer period calculation

The generation time of periodic events is mainly determined by the values of the TIMx_PSC and TIMx_ARR registers, which is also the period of the timer. For example, we need a timer with a period of 1s. How should we set the values of the two registers? Suppose, we first set the TIMx_ARR register value to 9999, that is, when TIMx_CNT is calculated from 0 and just equal to 9999, the event will be generated for 10000 times in total. Then, if the clock source period is 100us at this time, the timing period of just 1s can be obtained. The next problem is to set the TIMx_PSC register value so that CK_CNT outputs a 100us cycle (10000Hz) clock. The input clock CK_PSC of the predivider is 90MHz, so the value of the predivider can be set to (9000-1).

Detailed description of timer initialization structure

typedef struct { uint16_t TIM_Prescaler; // uint16_t TIM_CounterMode; // Uint32_t TIM_Period; // Uint16_t TIM_ClockDivision; // Uint8_t TIM_RepetitionCounter; // repeat calculator} TIM_TimeBaseInitTypeDef;Copy the code

(1) TIM_Prescaler: timer predivider is set. The clock source is timer clock through this predivider. It sets the value of TIMx_PSC register. The range is 0 to 65535, and the frequency division is 1 to 65536. Why to make a predivider, it is because the clock frequency of the system is too fast, 90MHZ ah, this general person timer can not hold such a fast speed, so the frequency of the timer, let him give a little less clock frequency, that’s all. (2) TIM_CounterMode: timer counting mode, but in the up counting, down counting and three center alignment modes. The base timer can only be counted upward, that is, TIMx_CNT can only be incremented from 0 and does not need to be initialized. (3) TIM_Period: timer period, which actually sets the value of the automatic overload register and updates it to the shadow register when the event is generated. The value ranges from 0 to 65535. Automatic reloading of register values: For example, if you want to pour water into a bucket, then empty it when it is full. How much water does it take to fill up? I set a value for him, 100,000 drops of water, take it away. After dumping, in the reset drop 10000 drops, full and then dumping…… (4) TIM_ClockDivision: Clock division. Set the CK_INT frequency of the timer clock and the frequency division ratio of the sampling clock frequency of the digital filter. The basic timer does not have this function, so no need to set it. (5) TIM_RepetitionCounter: the repetition counter belongs to the special register bit of the advanced control register, and it is very easy to control the number of PWM output by using it. There’s no setting.

Application Settings

Set the universal timer and generate the corresponding interrupt, mainly divided into the following steps (take TIM3 as an example)

  1. TIM3 The clock function is enabled
  2. Set the TIM3_ARR and TIM3_PSC values
  3. Set TIM3_DIER to allow update interrupts
  4. Allow TIM3 to work
  5. TIM3 Interrupt group Settings
  6. Write interrupt service functions
void TIM3_Int_Init() { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_Prescaler =7199; // Tim_timebasestructure. TIM_Prescaler =7199; Tim_timebasestructure. TIM_Period = 4999; // Set the count frequency of 10Khz to tim_timebasestructure. TIM_Period = 4999; Tim_timebasestructure.tim_clockdivision = 0; tim_timeBasestructure.tim_clockDivision = 0; tim_timeBaseStructure.tim_clockDivision = 0; // Set clock split :TDTS = Tck_tim tim_timebasestructure. TIM_CounterMode = TIM_CounterMode_Up; // TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_ITConfig(TIM3, TIM_IT_Update,ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; / / interrupt NVIC_InitStructure TIM3. NVIC_IRQChannelPreemptionPriority = 0; // Priority 0 nvic_initstructure. NVIC_IRQChannelSubPriority = 3; NVIC_IRQChannelCmd = ENABLE; nvic_initstructure. NVIC_IRQChannelCmd = ENABLE; // the IRQ channel is enabled with NVIC_Init(&NVIC_InitStructure); // Initialize the external NVIC register TIM_Cmd(TIM3, ENABLE) according to the parameters specified in NVIC_InitStruct; // Enable TIMx peripheral}Copy the code

The timer clock must be turned on before using the timer. The basic timer belongs to APB1 bus peripherals. APB1 bus peripheral clock =72M. We set the timer to the value of the automatic reloading register ARR as 4999 and the value of the clock precrossover register PSC as 7199. Then the clock that drives the counter is CK_CNT = APB1Periph/ (7199+1)=72M/7200=10K, and the time of counting once is equal to: 1/CK_CNT=0.0001s=0.1ms=100us, when the counter counts from 0 to 4999, produce an interruption, then the interruption time is: 100usX5000=0.0001sX5000=0.5s=500ms is half a second.

Void TIM3_IRQHandler(void) //TIM3 interrupt {if (TIM_GetITStatus(TIM3, TIM_IT_Update)! TIM interrupt source {TIM_ClearITPendingBit(TIM3, TIM_IT_Update); // Clear the interrupt bit of TIMx :TIM interrupt source LED1=! LED1; }}Copy the code

The interrupt service function starts by using the if statement, the TIM_GetITStatus() function, to determine whether TIM3 has been interrupted, and if so, to clear the interrupt flag bit of TIM3. Reverse the LED1 light.

int main(void) { delay_init(); // Delay function initializes NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set the interrupt priority group 2 LED_Init(); TIM3_Int_Init(); TIM3_Int_Init(); While (1) {LED0=! LED0; delay_ms(200); }}Copy the code

The main function firstly initializes the delay function, sets the interrupt priority grouping 2, initializes the hardware interface connected with LED, and initializes timer 3. LED0 will now flip every 0.5 seconds. We also flip the LED1 lamp 0.2 seconds in the while function for comparison. Now let’s use Keil simulation to see if LED0 is 0.2ms and LED1 is 0.5ms. 1. Configure the Keil simulation debugging tool.2. Enable debugging. On the debugging page, open the Logic Analysis window and set the PWM output pins 3. Click to run at full speed and observe the oscilloscope It can be clearly seen that LED0 is flipped once for 0.5ms. 4. Use the same method to configure another LED1, found is 0.2ms flip, and the program configured the same.Conclusion: when we use timer, we mainly want to know the frequency division factor and reload value. The two determines how long will you be timing, and if you want to use stm32 timer, timing function, need how long it will take time to do the corresponding operation, write your code directly inside the interrupt service function, using interrupt, too, to do things directly in your response service function to do operation. And the difference between a timer and a counter, a timer is a counter. You count first, count to a certain extent overflow is not the implementation of timing? So don’t be a beginner.