这是中断初始化
void TIM_INIT(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseInitStructure.TIM_Period = 1000; //定时器自动重装载计数周期值,当计数寄存器的值递增到等于该值时,将相关事件标志位置位,范围0~65535。
TIM_TimeBaseInitStructure.TIM_Prescaler = 48000-1; //计数器预分频设置0-65535
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频。有不分频、二分频、四分频。基本定时器没有这个功能
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //使能TIM1中断,允许更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1溢出中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //设置抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //设置响应优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道1中断
NVIC_Init(&NVIC_InitStructure); //初始化NVIC
TIM_Cmd(TIM1, ENABLE); //TIM1使能
}
void TIM1_UP_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) //检查TIM1中断是否发生。
{
TIM_ClearITPendingBit(TIM1,TIM_IT_Update); //清除TIM1的中断挂起位。
IOTtimeCount++;
}
}