问题背景
完成了对led灯和按键的封装之后,准备用基本定时器TIM6实现一个演示函数达到1s的延时
问题描述
延时函数会陷入死循环
调试结果:无法产生预期的中断
问题解决方法
分析:
在stm32f10x.it中,中断服务函数写成了BASIC_TIM_IRQHandler,期待编译器将BASIC_TIM替换为TIM6
而在bsp_basicTim.h宏定义处并没有定义BASIC_TIM,因此这个中端服务函数根本不会执行
解决方法:
将BASIC_TIM改为BASIC_TIM1,将TIM7的部分另外构造一个函数
void BASIC_TIM1_IRQHandler(void)
{
if(TIM_GetITStatus(BASIC_TIM1,TIM_IT_Update)!=RESET)
{
BASIC_TIM_Time1++;
TIM_ClearITPendingBit(BASIC_TIM1,TIM_FLAG_Update);
}
}
void BASIC_TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(BASIC_TIM2,TIM_IT_Update)!=RESET)
{
BASIC_TIM_Time2++;
TIM_ClearITPendingBit(BASIC_TIM2,TIM_FLAG_Update);
}
}
硬件
STM32f103rct6主控板
问题相关代码
bsp_basicTim.h
#ifndef __BSP_BASICTIM_H
#define __BSP_BASICTIM_H
#include "stm32f10x.h"
#ifndef __LOGIC
#define __LOGIC
//手动定义bool型
#define bool uint8_t
#define true 1
#define false 0
#endif
#define BASIC_TIM1 TIM6
#define BASIC_TIM1_CLK RCC_APB1Periph_TIM6
#define BASIC_TIM1_Period (1000-1)
#define BASIC_TIM1_Prescaler 71
#define BASIC_TIM1_IRQ TIM6_IRQn
#define BASIC_TIM1_IRQHandler TIM6_IRQHandler
#define BASIC_TIM2 TIM7
#define BASIC_TIM2_CLK RCC_APB1Periph_TIM7
#define BASIC_TIM2_Period (1000-1)
#define BASIC_TIM2_Prescaler 71
#define BASIC_TIM2_IRQ TIM7_IRQn
#define BASIC_TIM2_IRQHandler TIM7_IRQHandler
void BASIC_TIM1_Init(void);
void BASIC_TIM2_Init(void);
void Delay_ms(uint16_t);
#endif
stm32f10x_it.c(部分)
#include "bsp_basicTim.h"
extern uint16_t BASIC_TIM_Time1;
extern uint16_t BASIC_TIM_Time2;
//出错部分
void BASIC_TIM_IRQHandler(void)
{
if(TIM_GetITStatus(BASIC_TIM1,TIM_IT_Update)!=RESET)
{
BASIC_TIM_Time1++;
TIM_ClearITPendingBit(BASIC_TIM1,TIM_FLAG_Update);
}
if(TIM_GetITStatus(BASIC_TIM2,TIM_IT_Update)!=RESET)
{
BASIC_TIM_Time2++;
TIM_ClearITPendingBit(BASIC_TIM2,TIM_FLAG_Update);
}
}
总结
中断服务函数的函数名称是固定的,如果出错就会导致无法进入中断,且该错误不会引起编译器报错