STM32 定时器实现样例

STM32 定时器实现样例

  • 1 定时器的初始化
  • 2 中断使能和中断优先级设置

1 定时器的初始化

void Tim5_init(void)
{
	TIM_TimeBaseInitTypeDef   TIM5_TimeBaseStructure;
    //NVIC_InitTypeDef NVIC_InitStructure;
        
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
        
    TIM_DeInit(TIM5);
	TIM5_TimeBaseStructure.TIM_Period =99;
	TIM5_TimeBaseStructure.TIM_Prescaler =719; 
	TIM5_TimeBaseStructure.TIM_ClockDivision = 0x0;
	TIM5_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
	TIM_TimeBaseInit(TIM5, &TIM5_TimeBaseStructure); // Time base configuration
    TIM_ClearFlag(TIM5,TIM_FLAG_Update);
    TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE ); 
       
    TIM_Cmd(TIM5, ENABLE);                
}

2 中断使能和中断优先级设置

	NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	NVIC_Init(&NVIC_InitStructure);
	```
# 3 中断处理程序
void TIM5_IRQHandler(void)
{

	if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
	{
		TIM_ClearITPendingBit(TIM5, TIM_IT_Update );
		TIM_SetCounter(TIM5, 83);
		//GPIO_ToggleBits(GPIOC, GPIO_Pin_4);//test 定时  1ms
        //Counter_Update();  中断处理接口
	}
}
	

你可能感兴趣的:(ARM/STM32,STM32,定时器)