STM32 普通定时器(TIM3)中断 实现流水灯实验

/***********************************************************

通过定时器TIM3产生间隔时间为1秒的中断,在中断响应

函数中控制LED发光二级管,实现流水灯效果

***********************************************************/

void TimeInit()

{
   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
   NVIC_InitTypeDef NVIC_InitStructure;
   
   //打开相应的时钟
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);


   //清除TIM3的中断待处理位:TIM 中断源
   TIM_ClearITPendingBit(TIM3,TIM_IT_Update);


   //设置定时器TIM_TimeBaseInit初始化定时器的时钟基数
   TIM_TimeBaseStructure.TIM_Period = 2000 -1;
   TIM_TimeBaseStructure.TIM_Prescaler = 36000 -1;
   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure);


   //使能指定的TIM中断
   TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE );


   //使能TIM定时器
   TIM_Cmd(TIM3, ENABLE);


   //设置NVIC
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);

}

中断函数编写

//TIM3中断响应函数
void TIM3_IRQHandler(void)
{
    static int i = 0;
//判断是否发生中断
if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
{
//清除EXTI线路挂起位
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);

   GPIO_Write(GPIOC, (u16)~(0x01<if(8 == i)  i= 0;
}
}




你可能感兴趣的:(STM32)