Detailed STM32 timing calculation

STM32 timer

Time = times x 1 / frequency

((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+7199)/72M)*(1+9999)=1秒

1. Introduction to Timer

1. Clock source

2. Timer structure (take basic timer as an example)

2. Programming method of basic timer

1. The register of the basic timer

2. Routine

 

/**
  * @brief  定时器6的初始化,定时周期0.01s
  * @param  无
  * @retval 无
  */
void TIM6_Init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    /*AHB = 72MHz,RCC_CFGR的PPRE1 = 2,所以APB1 = 36MHz,TIM2CLK = APB1*2 = 72MHz */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
    
    /* 时基初始化 */         
    TIM_TimeBaseStructure.TIM_Period = 99;          //当定时器从0计数到99,即定时周期为100次
    TIM_TimeBaseStructure.TIM_Prescaler = 7199;     //设置预分频:10KHz
    TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
    TIM_ARRPreloadConfig(TIM6, ENABLE);             //使能TIM6重载寄存器ARR
    
    /* 设置更新请求源只在计数器上溢或下溢时产生中断 */
    TIM_UpdateRequestConfig(TIM6,TIM_UpdateSource_Global);
    /* 定时器6的上溢或下溢中断使能 */
    TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
    /* 定时器6启动 */
    TIM_Cmd(TIM6, ENABLE);                          //使能定时器6
    TIM_ClearITPendingBit(TIM6,TIM_IT_Update);
    /* 定时器6的NVIC中断配置 */
    NVIC_TIM6_Configuration(); 
}

Three, doubts and answers

  The following questions are explained using basic timers as examples 

1. What is an update event

  An update event means that after this event occurs, the timer register will be updated to make the timer work in a new configuration, such as when a timing period ends (counter overflows) or other events.

2. What is an auto-reload register?

  The auto-reload register determines the overflow timing of the timer. When the value in the timer counter reaches the value specified by the auto-reload register, the counter will return to zero. In other words, the auto-reload register determines the period of the timer. Assuming TIMx_ARR=0x36, and the frequency division factor is 1, you can see the situation below.

3. The difference and connection between automatic reload register and preload register

   When "TIMx_CR1.ARPE = 1", there are automatic reload registers and preload registers (TIMx_ARR) in STM32.

  The preload register is the "shadow" of the auto reload register, that is, the preload register is the buffer of the auto reload register. The function of the automatic reload register has been explained in point 2, but the automatic reload register is not directly operated by the user's program, and the user needs to use the preload register (buffer) to access it.

  Its purpose is to ensure that the automatic reload register is modified at an appropriate time, and it is not allowed to be modified at will, otherwise it may cause undesired results during the transition.

  What is this concept?

  At the end of one period of the timer, an update interrupt is generated. We modify the preload register (TIMx_ARR) in the interrupt service routine, but it is not directly written to the auto reload register. When the interrupt is first generated (earlier than our service program), the original TIMx_ARR value is automatically loaded into the auto-reload register by the hardware. Therefore, the length of the next timer period depends on the "original TIMx_ARR value", not the modified value in the interrupt service routine.

  So when does our modified value take effect?

  When the next timer period ends, our modified value of TIMx_ARR is automatically written into the auto-reload register by the hardware, so our modified value will take effect in the next timer period.

  When "TIMx_CR1.ARPE = 0", there is only the auto reload register (TIMx_ARR) in STM32, and there is no preload register. The auto-reload register has no buffer. The modification of TIMx_ARR is the modification of the auto-reload register directly.

  What about this situation?

  At the end of one cycle of the timer, an update interrupt is generated. We modify the automatic reload register (TIMx_ARR) in the interrupt service routine. So the timing length of the next timer period depends on our modified value.

to sum up:

  ① TIMx_CR1.ARPE = 0, the auto-reload register has no buffer, and the modification of TIMx_ARR directly affects the timing length of the next cycle.

  ② TIMx_CR1.ARPE = 1, the automatic reload register has a buffer, and the modification of TIMx_ARR affects the timing length of the next cycle.

      ③ TIMx_CR1.ARPE = 1, the automatic reload register has a buffer preload register (TIMx_ARR). The timing of updating the preload register to the automatic reload register is when an update event is generated at the end of a timing period of the timer.

  ④ TIMx_CR1.ARPE = 1. Note that when we write the program, we assign a value to TIMx_ARR, which is not actually written into the auto reload register, but into the preload register.

  When we need the timer to work alternately at T1 and T2:

  ⑤ TIMx_CR1.ARPE = 0, there is no buffer in the auto-reload register. We only set the length of the timing period T1 when the T1 timing period has started for a while, and set it when the T2 timing period has started for a while The length of the timing period T2. Because when T1 ends, after the interrupt occurs, we set the timing period as T2 in the interrupt program. In fact, the timer period T2 has already started for a while. It is necessary to know that when one cycle of the timer ends, the hardware automatically enters the count of the next cycle without being controlled by the software.

  ⑥ TIMx_CR1.ARPE = 1, the automatic reload register has a buffer, we set the length of the timing period T1 at the beginning of the T1 timing period; at the beginning of the T2 timing period, set the length of the timing period T2 . Because when T1 ends, an update event is generated (interrupts also occur), (we have set the timing period to T2 in the interrupt program of the last timing period), the T2 value in TIMx_ARR is updated by the hardware into the auto-reload register in.

  ⑦ When the two cycles of T1 and T2 are both large, more ticks are needed, and there will be no errors in both methods.

   But when the two cycles of T1 and T2 are very small, fewer ticks are needed. For the case of "TIMx_CR1.ARPE = 0", there may be problems. Because it is possible to set the timing period T1 when the T1 timing period has exceeded the length of T1; when the T2 period has exceeded the length of T2, set the timing period T2.

to sum up:

  When the period of the timer needs to be constantly switched, and the period is relatively short, the programmer needs to operate the timer through the preload register and the automatic reload register to ensure a smooth transition of the timer period.  

The auto-reload register is pre-loaded. Each time the auto-reload register is read or written, it is actually realized by reading and writing the pre-load register. According to the automatic reload preload enable bit (ARPE) in the TIMx_CR1 register, the content written into the preload register can be transferred to its shadow register immediately or every update event. Extracted from "STM32 Chinese Version Chip Manual"

Guess you like

Origin blog.csdn.net/seek97/article/details/87862841