void TIM4_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = 1;//1us
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
extern volatile u32 time2;
void TIM4_IRQHandler(void)
{
// TIM4->SR = (uint16_t)~TIM_IT_Update;
// time2++;
if ( TIM_GetITStatus(TIM4 , TIM_IT_Update) != RESET )
{
TIM_ClearITPendingBit(TIM4 , TIM_FLAG_Update);
time2++;
}
}
这样子程序跑起来一直在中断函数里跑,根本不运行主程序了。貌似1微秒中断,而中断函数里运行的超过1微秒了。
void TIM4_IRQHandler(void)
{
TIM4->SR = (uint16_t)~TIM_IT_Update;
time2++;
// if ( TIM_GetITStatus(TIM4 , TIM_IT_Update) != RESET )
// {
// TIM_ClearITPendingBit(TIM4 , TIM_FLAG_Update);
// time2++;
// }
}
改成上面这样子,两条语句运行时间应该不足1微秒了,程序终于可以运行了。
可是呢,我在主程序里有个1秒钟延时(用systick,优先级高),然后用串口打印TIM4中的计数值,结果每次计数增加510092左右,即实际2us,差了一半呢?
我把TIM4周期设为10us,则打印结果每次增加92626,即10.8us,这还比较接近。
TIM4周期设为5us,结果为169844,即5.887us。
TIM4周期设为2us,结果339899,即2.94us。
请教这些误差是怎么导致的,该怎么避免呢?
|
|