中断服务程序需要满足如下要求:
(1) 不能返回值
(2) 不能向ISR传递参数
(3) ISR应该尽可能的短小精悍
(4) printf(char* lpFormatString,...)函数会带来重入和性能问题,不能在ISR中采用。
在某项目的开发中,我们设计了一个队列,在中断服务程序中,只是将中断类型添加入该队列中,在主程序
的死循环中不断的扫描中断队列是否有了中断,有则取出队列中的第一个中断类型,进行相应的处理。
/*存放中断的队列*/
typedef struct tagIntQueue
{
int intType;/*中断类型*/
struct tagIntQueue *next;
}IntQueue;
IntQueue lpIntQueueHead;
__interrupt ISRexample()
{
int intType;
intType = GetSysType();
QueueAddTail(lpIntQueueHead,intType);/*在队列尾加入新的中断*/
}
在主程序循环中判断是否有中断:
while(1)
{
if(! IsIntQueueEmpty())
{
intType = GetFirstInt();
switch(intType)/*是不是很像WIN32程序的消息解析函数*/
{
/*对,我们的中断类型解析很类似于消息驱动*/
case xxx: /*我们称之为'中断驱动'吧*/
...
break;
case xxx:
...break;
...
}
}
}
按上述的方法设计的中断服务程序很小,实际的工作都交由主程序执行了。