初始化定时器1,开启定时器1中断,
初始化定时器1,开启定时器1中断,定时1S切换LED的状态。但是写好之后,测试发现灯的状态在44s才切换一次,一直找不到原因。最后发现了一点是高级定时器和普通定时器的区别:
TIM_RepetitionCounte是用来配置重复计数,就是重复溢出多少次才给你来一个溢出中断,只有高级定时器需要配置。
下面是定时器1的程序:
#include "Timer1.h"
void Timer1_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//开启定时器1时钟
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;//定时器1向上计数中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM1,ENABLE);//使能定时器1
}
void TIM1_UP_IRQHandler(void)
{
//u8 seal_buzzer_time= 0x64;//发送指令响蜂鸣器
if(TIM_GetITStatus(TIM1,TIM_IT_Update))
{
TIM_ClearITPendingBit(TIM1,TIM_FLAG_Update);
SYSTEM_LED_PA0 = ~SYSTEM_LED_PA0;
}
}
以后常来论坛学习