定时器中断实验
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2,); LED_Init(); delay_init(); TIM3_Int_Init(4999,7199);//500ms while(1) { LED1=!LED1; TIM_ClearITPendingBit(TIM3,TIM_IT_Update); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "timer.h" #include "led.h" void TIM3_Int_Init(u16 arr,u16 psc) //定时器初始化 { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); TIM_TimeBaseInitStrue.TIM_Period=arr; TIM_TimeBaseInitStrue.TIM_Prescaler=psc; TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrue); TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能定时器更新中断 NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//TIM3中断 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ通道被使能 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//先抢占优先级设置为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//从优先级3级 NVIC_Init(&NVIC_InitStructure);//初始化NVIC寄存器 TIM_Cmd(TIM3,ENABLE);//定时器3使能 } void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) { LED1=!LED1; TIM_ClearITPendingBit(TIM3,TIM_IT_Update); } }