请教 用STM32_TIM2向上计数定时时出现两个中断?
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 20000;
TIM_TimeBaseStructure.TIM_Prescaler = 35999;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//==TIM_CKD_DIV1
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
////
TIM_ARRPreloadConfig(TIM2, DISABLE);
//清中断,以免一启用中断后立即产生中断
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
中断向量配置代码:
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
这是中断部分的代码
void TIM2_IRQHandler(void)
{
//
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
//
{
time2++;
USART_SendData(USART1,time2);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET); //判断是否发送完成。
/* Clear the TIM3 update interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//
}
}
运行时每次在pc的串口上都会收到两个连续的数
如果把代码改成
void TIM2_IRQHandler(void)
{
time2++;
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
//
time2++;
USART_SendData(USART1,time2);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET); //判断是否发送完成。
/* Clear the TIM3 update interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
则在PC串口上只能有道奇数?????求解??
这些天在看《STM32技术参考手册中文翻译第10版》的TIMx 说实话,有点像看日文,完全找不到系统的把握,希望那位前辈有好的资料共享一下,小菜在此谢谢了!!!!!!!!!!!!!!!!! |