The basic timer STM32F103ZET6

1, the timer classification

  STM32F103ZET6 total of eight timers, they are: TIM1 ~ TIM8. STM32 timer into basic timer, general purpose timer and the timer higher.

  TIM6, TIM7 basic timer. The basic timer is counted up only 16-bit timer, the timer only the basic timing functions, no external IO port, so there is no channel capture and compare.

  TIM2, TIM3, TIM4, TIM5 is a general-purpose timer. General timer is counting up, counting down may be 16-bit timer. General-purpose timer may be timing output compare, input capture, each having four general-purpose timers external IO ports.

  TIM1, TIM8 is higher timer. Higher can timers are counting up, counting down may be 16-bit timer. Higher timer may be timing output compare, input capture, output three-phase motor may be complementary signals, each higher timer has eight external IO ports.

  Timer classification is as follows:

2, the basic timer

  The basic timer is not external IO port, so it is only the timing function.

  The basic timer can only count up, that basic timer can only count up.

  The basic timer function diagram is as follows:

  Function can be seen in FIG. 1, the basic TIMxCLK timer clock from the internal clock, the internal clock after APB1 prescaler provided. The basic relationship with APB1 bus timer clock as follows:

    • If APB1 prescaler is 1, then the clock timer is substantially equal to APB1 bus clock.
    • If APB1 prescaler is not 1, then the clock timer is substantially equal to twice through APB1 bus clock after frequency division.

  For example APB1 through the bus clock is divided by two after 36MHZ, then the clock timer is substantially 72MHZ3 (36 * 2).

  2 is a functional diagram of a prescaler, the clock from the internal pre-divider frequency-divided clock, for driving basic timer counter. Prescaler basic timer is a 16-bit prescaler, the prescaler can be any number between 1 and 65536 dividing the clock timer. It is calculated as follows:

  Timer clock = clock from the APB1 / (prescaler +1)

  功能图中的3是一个16位的计数器,该计数器能能向上计数,最大计数值位65535。基本定时器的计数器从0开始向上计数,当计数器的值与自动重装载寄存器相等时产生更新事件,并清零从头开始计数。

  功能图中的4是一个16位的自动重装载寄存器。该寄存器装着计数器能计数的最大数值。当基本定时器的计数器计数到这个值的时候,如果使能了中断。定时器就会产生溢出中断。

3、定时器的时间计算

  基本定时器每计数一次所经过的时间为:

  Time = (PSC + 1)/ TIMxCLK(us)

  PSC是定时器的分频系数,TIMxCLK是内部时钟。

  基本定时器的计数次数由自动重装载寄存器决定的,基本定时器的计数器从0开始向上计数,当计数器的值与自动重装载寄存器相等时,产生溢出。所以基本定时器的溢出时间计算公式如下:

  Time = (PSC+1)*(ARR)/ TIMxCLK(us)

  ARR是自动重装载寄存器的值。

  假设基本定时器TIMxCLK = 72MHZ,PSC = 71,ARR = 1000,那么定时器的溢出时间为:

  Time = (71+1)*1000/72 = 1000(us) = 1(ms)

4、基本定时器的配置流程

  使能基本定时的时钟,不然无法使用定时器。

  设置基本定时器的预分频系数和自动重装载寄存器值,通用定时器和高等定时器还要设置计数方向,因为基本定时器只能向上计数,所以不用设置。

  开启基本定时器。

  通过NVIC配置基本定时器的中断。

  编写基本定时器的中断服务函数,在中断服务函数中,可以通过基本定时器的状态寄存器的值来判断此次产生的中断属于什么类型。

Guess you like

Origin www.cnblogs.com/h1019384803/p/11032712.html