定时器定时不准的问题

2019-07-20 16:11发布

[mw_shl_code=c,true]int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2 delay_init(168); //初始化延时函数 LED_Init(); //初始化LED端口 TIM2_Int_Init(10000-1,8400-1); //定时器时钟84M,分频系数8400,所以84M/8400=10Khz的计数频率,计数5000次为500ms TIM3_Int_Init(10000-1,8400-1); //定时器时钟84M,分频系数8400,所以84M/8400=10Khz的计数频率,计数5000次为500ms TIM9_Int_Init(10000-1,8400-1); //定时器时钟84M,分频系数8400,所以84M/8400=10Khz的计数频率,计数5000次为500ms while(1) { LED3=!LED3;//DS0翻转 delay_ms(200);//延时200ms }; } void TIM2_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); ///使能TIM2时钟 TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//初始化TIM2 TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //允许定时器2更新中断 TIM_Cmd(TIM2,ENABLE); //使能定时器2 NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; //定时器2中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } //通用定时器3中断初始化 //arr:自动重装值。 //psc:时钟预分频数 //定时器溢出时间计算方法:Tout=((arr+1)*(psc+1))/Ft us. //Ft=定时器工作频率,单位:Mhz //这里使用的是定时器3! void TIM3_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); ///使能TIM3时钟 TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//初始化TIM3 TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许定时器3更新中断 TIM_Cmd(TIM3,ENABLE); //使能定时器3 NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM9_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9,ENABLE); ///使能TIM9时钟 TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM9,&TIM_TimeBaseInitStructure);//初始化TIM9 TIM_ITConfig(TIM9,TIM_IT_Update,ENABLE); //允许定时器9更新中断 TIM_Cmd(TIM9,ENABLE); //使能定时器9 NVIC_InitStructure.NVIC_IRQChannel=TIM1_BRK_TIM9_IRQn; //定时器9中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } //定时器2中断服务函数 void TIM2_IRQHandler(void) { if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET) //溢出中断 { LED2=!LED2;//DS1翻转 } TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位 } //定时器9中断服务函数 void TIM1_BRK_TIM9_IRQHandler(void) { if(TIM_GetITStatus(TIM9,TIM_IT_Update)==SET) //溢出中断 { LED0=!LED0;//DS1翻转 } TIM_ClearITPendingBit(TIM9,TIM_IT_Update); //清除中断标志位 } //定时器3中断服务函数 void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断 { LED1=!LED1;//DS1翻转 } TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位 } [/mw_shl_code] 在例程《实验8 定时器中断实验》发现定时1秒时间不准,按例程的叙述定时器为1秒,延时为200m秒,应该是延时的灯闪5次,定时器的灯闪1次。试验的结果延时的灯闪3次,定时器的灯闪1次;用示波器看定时器的定时不是1秒,而是2秒多。请问是怎么回事?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。