STM32F103ZET6 timer

Four general-purpose timers, two basic timer, two timers senior

[Difference] three kinds of timer

Here Insert Picture Description

[Overview] general-purpose timers

STM3 universal TIMx (TIM2, TIM3, TIM4 and TIM5) Timer features include:

  1. APB1 located on low speed bus (APB1) 16-bit up, down, up / down (center alignment) counting mode, the counter automatically loaded (TIMx_CNT).
  2. 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.
  3. 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
  4. Using an external signal (TIMx_ETR) interconnect and timer control timer (the timer may be a timer controls a further) synchronizing circuit.
  5. 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
  6. 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.
  7. 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]

Here Insert Picture Description

[] Select the counter clock

Counter clock may be provided by the following clock sources:

  1. Internal clock (CK_INT) PS: default internal clock.
  2. External clock mode 1: the external input pin (TIx).
  3. External clock mode 2: External trigger input (ETR).
  4. Internal trigger input (ITRx): using a timer as prescaler another timer, such a timer may be configured as another timer Timer1 prescaler of Timer2.
    Here Insert Picture Description
    Here Insert Picture Description
    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:
Here Insert Picture Description
① 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. Here Insert Picture Description
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. Here Insert Picture Description
③ 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.Here Insert Picture Description

【register】

Counter current value register ① the CNT Here Insert Picture Description
② prescaler register TIMx_PSC Here Insert Picture Description
③ auto-reload register (in TIMx_ARR) Here Insert Picture Description
④ control register. 1 (the TIMx_CR1)
Here Insert Picture Description
⑤DMA interrupt enable register (TIMx_DIER)
Here Insert Picture Description

[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
 }
Published 19 original articles · won praise 2 · Views 694

Guess you like

Origin blog.csdn.net/qq_44431690/article/details/104180056