请教一个定时器的问题,我用TIMER2做了个溢出中断,我本来是要1S中断一次的,我在中断服务程序中不断的取反一个IO口,结果用示波器量出来是0。5S中断一次,是否我在哪设置错了。
代码如下:
void Timer2Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_DeInit( TIM2);//复位TIM2定时器
// TIM2 configuration
TIM_TimeBaseStructure.TIM_Period = 9999; //最大计数值0xffff
TIM_TimeBaseStructure.TIM_Prescaler = 3599;//分频0x3599 + 1 =3600
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方向向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// Clear TIM2 update pending flag[清除TIM2溢出中断标志]
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
// Enable TIM2 Update interrupt [TIM2溢出中断允许
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
// TIM2 enable counter [允许tim2计数]
TIM_Cmd(TIM2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
if(Second >= 60)
{
Second = 0;
Minute++;
}
Second++;
if(GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_7))
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
else
GPIO_SetBits(GPIOB, GPIO_Pin_7);
/* if(GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_5))
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
else
GPIO_SetBits(GPIOB, GPIO_Pin_5);*/
}
}
} |