Four general-purpose timers, two basic timer, two timers senior
[Difference] three kinds of timer
[Overview] general-purpose timers
STM3 universal TIMx (TIM2, TIM3, TIM4 and TIM5) Timer features include:
- APB1 located on low speed bus (APB1) 16-bit up, down, up / down (center alignment) counting mode, the counter automatically loaded (TIMx_CNT).
- 16-bit programmable (can be modified in real time) prescaler (TIMx_PSC), the counter clock frequency division ratio to any value between 1 and 65535.
- Four independent channels (TIMx_CH1 ~ 4), these channels can be used as:
① input capture
② output compare
③PWM generated (edge or center-aligned mode)
④ single-pulse mode output - Using an external signal (TIMx_ETR) interconnect and timer control timer (the timer may be a timer controls a further) synchronizing circuit.
- Interrupt / DMA (6 independent IRQ / DMA request generator) when the following events occur:
① Update: counter overflow / underflow counter is initialized (triggered by software or internal / external)
② trigger event (start counter, stop, the trigger count is initialized or internal / external)
③ input capture
④ output compare
⑤ support (quadrature) encoder and the Hall sensor circuit for incremental positioning of
⑥ trigger input as an external clock, or by a current management cycle - The STM32 general-purpose timer may be used to: measure the pulse length (input capture) or the input signal to generate an output waveform (output compare and PWM) and the like.
- Using the timer prescaler and prescaler clock controller RCC, pulse length and waveform period may be between a few milliseconds to a few microseconds adjusted. STM32 each general-purpose timer is completely independent, without any resources to share with each other.
[Universal timer operation]
[] Select the counter clock
Counter clock may be provided by the following clock sources:
- Internal clock (CK_INT) PS: default internal clock.
- External clock mode 1: the external input pin (TIx).
- External clock mode 2: External trigger input (ETR).
- Internal trigger input (ITRx): using a timer as prescaler another timer, such a timer may be configured as another timer Timer1 prescaler of Timer2.
Unless pre APB1 division ratio is 1, the common timer or clock is equal to 2 times APB1 clock.
The default function is call SystemInit case:
the SYSCLK = 72M
the AHB clock = 72M
APB1 clock = 36M
so pre APB1 division ratio = AHB / APB1 clock = 2
Therefore, general-purpose timer clock CK_INT = 2 * 36M = 72M
[Overflow time]
Tout (overflow time) = (the ARR +. 1) (the PSC +. 1) / Tclk
the ARR: preload value
PSC: prescaler
TCLK: timer clock frequency (default state 72M)
[Counter Mode]
General timer can count up, counting down, up and down-count mode bidirectional:
① counting up mode
the counter counts from 0 to reload value (in TIMx_ARR), and then re-starts counting from 0 and generates a counter overflow event.
CNT_EN: Enable
② down-count mode
value (in TIMx_ARR) is automatically loaded from the counter starts counting down to zero, and start again from the auto-load value, and generates a counter underflow event.
③ center-aligned mode (up / down count)
counter counts from 0 to the value -1 is automatically loaded, generates a counter overflow event, then counted down to 1 and generates a counter overflow event; then again from zero count.
【register】
Counter current value register ① the CNT
② prescaler register TIMx_PSC
③ auto-reload register (in TIMx_ARR)
④ control register. 1 (the TIMx_CR1)
⑤DMA interrupt enable register (TIMx_DIER)
[Common library functions]
① timer initialization function
void TIM_TimeBaseInit (TIM_TypeDef * TIMx, TIM_TimeBaseInitTypeDef * TIM_TimeBaseInitStruct);
② timer enable function
void TIM_Cmd (TIM_TypeDef * TIMx, FunctionalState the NewState)
③ timer interrupt enable function
void TIM_ITConfig (TIM_TypeDef * TIMx, uint16_t TIM_IT, FunctionalState the NewState);
④ state flag acquisition and clearance
FlagStatus TIM_GetFlagStatus (TIM_TypeDef * TIMx, uint16_t TIM_FLAG);
void TIM_ClearFlag (TIM_TypeDef * TIMx, uint16_t TIM_FLAG);
ITStatus TIM_GetITStatus (TIM_TypeDef * TIMx, uint16_t TIM_IT);
void TIM_ClearITPendingBit (TIM_TypeDef * TIMx , uint16_t TIM_IT);
[] Timer interrupt implementation steps
① the timer clock.
RCC_APB1PeriphClockCmd ();
② timer initialization, configuration ARR, PSC.
TIM_TimeBaseInit ();
③ open the timer interrupt, configure the NVIC.
TIM_ITConfig void ();
NVIC_Init ();
④ enable the timer.
TIM_Cmd ();
⑥ write interrupt service function.
TIMx_IRQHandler ();
[Sample Code]
timer.c
void TIME3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef MyTimerstructure;
NVIC_InitTypeDef MyNVICstructure;
//①使能定时器时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//②初始化定时器
MyTimerstructure.TIM_Period=arr; //自动装载值
MyTimerstructure.TIM_Prescaler=psc; //预分频系数
MyTimerstructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式
MyTimerstructure.TIM_ClockDivision=1;
TIM_TimeBaseInit(TIM3,&MyTimerstructure);
//③开启定时器中断
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能更新中断
//④初始化中断
MyNVICstructure.NVIC_IRQChannel=TIM3_IRQn;//选择TIM3通道
MyNVICstructure.NVIC_IRQChannelCmd=ENABLE;//使能
MyNVICstructure.NVIC_IRQChannelPreemptionPriority=1;//配置抢占优先级
MyNVICstructure.NVIC_IRQChannelSubPriority=1;//配置响应优先级
NVIC_Init(&MyNVICstructure);
//⑤使能定时器
TIM_Cmd(TIM3,ENABLE);
}
void TIM3_IRQHandler(void) //⑥定时器中断函数
{
if(TIM_GetFlagStatus(TIM3,TIM_FLAG_Update)!=RESET)//检查标志位
{
LED0=~LED0;
TIM_ClearFlag(TIM3,TIM_FLAG_Update); //清除标志位
}
}
main.c
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(); //延时函数初始化
LED_Init(); //初始化与LED连接的硬件接口
TIME3_Int_Init(4999,7199); //500ms
}