[STM32学习]用基本定时器实现精确延时出现的问题

问题背景

完成了对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);
	}
}

总结

中断服务函数的函数名称是固定的,如果出错就会导致无法进入中断,且该错误不会引起编译器报错

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32的1S #include "stm32f10x_it.h" /** @addtogroup STM32F10x_StdPeriph_Template * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ extern u32 SystickCounter; extern u8 KeySwitch_Press; extern u8 KeyAdjust_Press; #define TRUE 1 #define FALSE 0 /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /******************************************************************************/ /* Cortex-M3 Processor Exceptions Handlers */ /******************************************************************************/ /** * @brief This function handles NMI exception. * @param None * @retval None */ void NMI_Handler(void) { } /** * @brief This function handles Hard Fault exception. * @param None * @retval None */ void HardFault_Handler(void) { /* Go to infinite loop when Hard Fault exception occurs */ while (1) { } } /** * @brief This function handles Memory Manage exception. * @param None * @retval None */ void MemManage_Handler(void) { /* Go to infinite loop when Memory Manage exception occurs */ while (1) { } } /** * @brief This function handles Bus Fault exception. * @param None * @retval None */ void BusFault_Handler(void) { /* Go to infinite loop when Bus Fault exception occurs */ while (1) { } } /** * @brief This function handles Usage Fault exception. * @param None * @retval None */ void UsageFault_Handler(void) { /* Go to infinite loop when Usage Fault exception occurs */ while (1) { } } /** * @brief This function handles SVCall exception. * @param None * @ret

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值