摘要
嵌入式开发中经常遇到一个这样尴尬的问题,由于单片机的中断源是唯一的,意思是整个工程中只能定义一次。在应用时,有可能多个模块中会用到同一个中断源,且每个模块都是在独立的.c文件中,为了增强代码的可移植性,在产生一个中断时,不同文件中都能独立响应这个中断,接下来就是要解决这个问题了。
实现技术要点
- 结构体
- 指针函数
- 函数注册
- 中断函数
- 结构体自身类型成员定义
实现
每次调用执行10个汇编指令周期,有n个注册执行就增加n*8个汇编指令周期。
irqCallbackTemp = firstIrqCallback;
while (irqCallbackTemp)
{
irqCallbackTemp->thisCb();
irqCallbackTemp = irqCallbackTemp->nextStruct;
}
中断函数.h文件
typedef void (*IRQCALLBACK)(void);
typedef struct __irqCallback
{
IRQCALLBACK thisCb;
struct __irqCallback *nextStruct;
}irqCallback_ts;
中断函数.c文件
irqCallback_ts *irqCallback_tim1;
irqCallback_ts *firstIrqCallback_tim1;
void TIM1_callbackRegiste(irqCallback_ts *cbStruct)
{
if (irqCallback_tim1 == 0)
{
irqCallback_tim1 = cbStruct;
firstIrqCallback_tim1 = irqCallback_tim1;
}
else
{
irqCallback_tim1->nextStruct = cbStruct;
irqCallback_tim1 = cbStruct;
}
}
void TIM1_UP_IRQHandler(void)
{
irqCallback_ts *irqCallbackTemp;
irqCallbackTemp = firstIrqCallback_tim1;
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update );
while (irqCallbackTemp)
{
irqCallbackTemp->thisCb();
irqCallbackTemp = irqCallbackTemp->nextStruct;
}
}
}
应用模块1.c文件
重点执行
irqCallback_ts myIrqCallback_tim1; myIrqCallback_tim1.thisCb = tim1_callback; TIM1_callbackRegiste(&myIrqCallback_tim1);
#include "stm32f10x_it.h"
TIM_CALLBACK timCallBack;
irqCallback_ts myIrqCallback_tim1;
void tim1_callback(void)
{
if(timCallBack)
{
timCallBack();
}
}
void myTim1_init(uint32_t period_us, TIM_CALLBACK cb)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = period_us;
TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE );
TIM_Cmd(TIM1, ENABLE);
timCallBack = cb;
myIrqCallback_tim1.thisCb = tim1_callback;
TIM1_callbackRegiste(&myIrqCallback_tim1);
}
应用模块2.c文件
在这个模块中也需要调用tim1定时器中断,只需要再注册一个回调函数即可
#include "stm32f10x_it.h"
irqCallback_ts nbIrqCallback_tim1;
void tim1_callback(void)
{
if(timCallBack)
{
timCallBack();
}
}
void nb_init(void)
{
nbIrqCallback_tim1.thisCb = tim1_callback;
TIM1_callbackRegiste(&nbIrqCallback_tim1);
}
|