STM32定时器注意事项

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/mzldxf/article/details/102529210

清除中断标志位

/******************************************************
* 满洲里国峰电子科技	 www.guofengdianzi.com
* 微信:GuofengDianZi
*******************************************************/
void  BASIC_TIM_IRQHandler (void)
{	//检查basic timer的更新中断是否发生
    if ( TIM_GetITStatus( BASIC_TIM, TIM_IT_Update) != RESET )
		UART1_PutChar(0x32);
}

在定时中断中输出0x32,即ASCII字符2,按道理应该周期性在串口助手上显示,但是没有。原因是没有清楚中断标志位。

/******************************************************
* 满洲里国峰电子科技	 www.guofengdianzi.com
* 微信:GuofengDianZi
*******************************************************/
void  BASIC_TIM_IRQHandler (void)
{	//检查basic timer的更新中断是否发生
    if ( TIM_GetITStatus( BASIC_TIM, TIM_IT_Update) != RESET )
		UART1_PutChar(0x32);
	TIM_ClearITPendingBit(BASIC_TIM , TIM_FLAG_Update);//清除中断标志位  	
}

调用TIM_ClearITPendingBit,清楚中断标志位即可。

检查变量time

在stm32f10x_it.c中必须要有 extern volatile uint32_t time;

BASIC_TIM_Period 的计算

/******************************************************
* 满洲里国峰电子科技	 www.guofengdianzi.com
* 微信:GuofengDianZi
*******************************************************/
#define            BASIC_TIM                   TIM6
#define            BASIC_TIM_APBxClock_FUN     RCC_APB1PeriphClockCmd
#define            BASIC_TIM_CLK               RCC_APB1Periph_TIM6
#define            BASIC_TIM_Period            1000-1					// 1 ms interrupt, 1/20s between 1/16s and 1/128s, fit keyboard requirement.
#define            BASIC_TIM_Prescaler         71						//  if 72 MHz HCLK used, timer clk is 1MHz, means 1us
#define            BASIC_TIM_IRQ               TIM6_IRQn
#define            BASIC_TIM_IRQHandler        TIM6_IRQHandler

注意此处是1000-1,而不是1000。上段代码的定时周期是1ms。

作者:伏熊(专业:射频芯片设计、雷达系统。爱好:嵌入式。欢迎大家项目合作交流。)
微信:GuoFengDianZi

猜你喜欢

转载自blog.csdn.net/mzldxf/article/details/102529210
今日推荐