【教学课件】第6章中断系统.ppt
第第第第6 6 6 6章章章章 中断系统中断系统中断系统中断系统6.1 6.1 中断的一般概念中断的一般概念CPUCPU对突发事件响应方式对突发事件响应方式中断源及中断申请中断源及中断申请开中断与关中断开中断与关中断中断优先级及中断嵌套中断优先级及中断嵌套中断向量与中断处理中断向量与中断处理一个应用系统,面对处理一个应用系统,面对处理器众多的中,是根据应用来选器众多的中,是根据应用来选取允许中断的事件与不允许中取允许中断的事件与不允许中断的事件。断的事件。中断向量,是中断源提供的处理程序中断向量,是中断源提供的处理程序的入口,其入口处,是由系统统一划拨存的入口,其入口处,是由系统统一划拨存储空间,给各中断源存放中断处理程序的储空间,给各中断源存放中断处理程序的入口,即中断向量表。入口,即中断向量表。中断响应过程:申请中断响应过程:申请-响应(响应(终止当前程终止当前程序序-保护断点保护断点-转到处理程序。转到处理程序。)-中断处中断处理(理(保护现场保护现场-处理处理-清该标志、恢复现场清该标志、恢复现场-返回。软件返回。软件)。)。硬件实现硬件实现软件完成软件完成6.2 6.2 6.2 6.2 SPCE061ASPCE061A中断系统中断系统中断系统中断系统中断类型与中断源中断类型与中断源异常中断异常中断-RESET,向量地址为向量地址为FFF7H。优先级最高。优先级最高。软中断软中断-执行指令执行指令BREAK,向量地址为向量地址为FFF5H。优先级最低。优先级最低。事件中断事件中断-外部与片内外部与片内I/O事件。快速中事件。快速中断断FIQ与中断与中断IRQ。FIQ优先级高于优先级高于IRQ。中断源中断源中断源中断源(事件)(事件)(事件)(事件)优先级优先级优先级优先级向量向量向量向量(通道)(通道)(通道)(通道)保留字保留字保留字保留字PWMPWM音频中断音频中断音频中断音频中断FIQFIQ/IRQ0IRQ0FFF6HFFF6H/FFF8HFFF8H_FIQ_FIQ/_IRQ0_IRQ0TimerATimerA溢出信号溢出信号溢出信号溢出信号FIQFIQ/IRQ1IRQ1FFF6HFFF6H/FFF9HFFF9H_FIQ_FIQ/_IRQ1_IRQ1TimerBTimerB溢出信号溢出信号溢出信号溢出信号FIQFIQ/IRQ2IRQ2FFF6HFFF6H/FFFAHFFFAH_FIQ_FIQ/_IRQ2_IRQ2EXT2EXT2IRQ3IRQ3FFFBHFFFBH_IRQ3_IRQ3EXT1EXT1键唤醒键唤醒键唤醒键唤醒4096Hz4096Hz时基信号时基信号时基信号时基信号IRQ4IRQ4FFFCHFFFCH_IRQ4_IRQ42048Hz2048Hz时基信号时基信号时基信号时基信号1024Hz1024Hz时基信号时基信号时基信号时基信号4Hz4Hz时基信号时基信号时基信号时基信号IRQ5IRQ5FFFDHFFFDH_IRQ5_IRQ52Hz2Hz时基信号时基信号时基信号时基信号TMB1TMB1选频信号选频信号选频信号选频信号IRQ6IRQ6FFFEHFFFEH_IRQ6_IRQ6TMB2TMB2选频信号选频信号选频信号选频信号UARTUARTIRQ7IRQ7FFFFHFFFFH_IRQ7_IRQ7如果同时有两个以上的如果同时有两个以上的IRQ中断时,按此优先级中断时,按此优先级响应响应。但较。但较“高级别高级别”IRQ中断不能中断中断不能中断“较较低级别低级别”IRQ中断。即在中断。即在IRQ中中断中不允许中断嵌套。断中不允许中断嵌套。事事件件中中断断 中断服务中断服务程序以此取名。程序以此取名。中断控制寄存器中断控制寄存器P_INT_Ctrl(7010H)读读/写写 读,中断标志字,用于判别产生中断的读,中断标志字,用于判别产生中断的源。写,选择允许中断的源。源。写,选择允许中断的源。P_INT_Clear(7011H)写写 清中断标志。选择的源与中断标志字对清中断标志。选择的源与中断标志字对应。写应。写“1”清该中断标志。清该中断标志。P_INT_Ctrl_New(702DH)读读/写写 激活与屏蔽中断。读,了解哪些中断源被激活与屏蔽中断。读,了解哪些中断源被屏蔽。写,对应屏蔽。写,对应0的中断源被屏蔽。对应的中断源被屏蔽。对应1的中的中断源被激活。断源被激活。中断控中断控中断控中断控制制制制 字字字字事事事事 件件件件说说说说 明明明明b15b15FIQ FIQ PWMPWM音频中断音频中断音频中断音频中断 b14b14IRQ0 IRQ0 PWMPWM音频中断音频中断音频中断音频中断b12b12FIQ_TMAFIQ_TMAb13b13IRQ1_TMAIRQ1_TMAb11b11FIQ_TMBFIQ_TMBb10b10IRQ2_TMBIRQ2_TMBb9b9IRQ3_ EXT2IRQ3_ EXT2b8b8IRQ3_ EXT1IRQ3_ EXT1b7b7IRQ3_KEYIRQ3_KEYb6b6IRQ4_ 4KHzIRQ4_ 4KHzb5b5IRQ4_ 2KHzIRQ4_ 2KHzb4b4IRQ4_ 1KHzIRQ4_ 1KHzb3b3IRQ5_ 4HzIRQ5_ 4Hzb2b2IRQ5_ 2HzIRQ5_ 2Hzb1b1IRQ6_ TMB1IRQ6_ TMB1b0b0IRQ6_ TMB2IRQ6_ TMB2UART IRQUART IRQ写写写写 P_UART_Command2P_UART_Command2 b7b7、b6b6中中断断控控制制字字 每一位对应一个事件,每一位对应一个事件,每一位对应一个事件,每一位对应一个事件,如果通过如果通过如果通过如果通过P_INT_CtrlP_INT_Ctrl向某向某向某向某位位位位写写写写1 1,则允许该事件向,则允许该事件向,则允许该事件向,则允许该事件向CPUCPU申请中断。申请中断。申请中断。申请中断。如果通过如果通过如果通过如果通过P_INT_CtrlP_INT_Ctrl读读读读,是读中断标志,是读中断标志,是读中断标志,是读中断标志,置置置置1 1的的的的位,说明该事件已向位,说明该事件已向位,说明该事件已向位,说明该事件已向CPUCPU申申申申请了中断。请了中断。请了中断。请了中断。P_INT_Ctrl_NewP_INT_Ctrl_New端口端口端口端口的每一位,也对应表中的事的每一位,也对应表中的事的每一位,也对应表中的事的每一位,也对应表中的事件。件。件。件。如果通过如果通过如果通过如果通过P_INT_ClearP_INT_Clear向某位写向某位写向某位写向某位写1 1,则清除该事件的中断标志。则清除该事件的中断标志。则清除该事件的中断标志。则清除该事件的中断标志。注意:只有在注意:只有在P_INT_Ctrl中设中设置了中断源允许位为置了中断源允许位为“1”,相应开相应开/关中断指令才有意义。关中断指令才有意义。中断控制指令中断控制指令例例6.1IRQ5有有2个中断源,个中断源,2Hz和和4Hz。TIME2对对2Hz中断计数,中断计数,TIME4对对4Hz中中断计数。断计数。2Hz中断控制中断控制A口口0LED以以2S速率闪烁,速率闪烁,4Hz中断控制中断控制B口口0LED以以1S速率闪烁。速率闪烁。Y否,就是否,就是2Hz中断。中断。.DEFINE P_IOA_DATA 0 x7000.DEFINE P_IOA_DATA 0 x7000.DEFINE P_IOA_DIR 0 x7002.DEFINE P_IOA_DIR 0 x7002.DEFINE P_IOA_ATTRI 0 x7003.DEFINE P_IOA_ATTRI 0 x7003.DEFINE P_IOB_DATA 0 x7005.DEFINE P_IOB_DATA 0 x7005.DEFINE P_IOB_DIR 0 x7007.DEFINE P_IOB_DIR 0 x7007.DEFINE P_IOB_ATTRI 0 x7008.DEFINE P_IOB_ATTRI 0 x7008.DEFINE P_INT_Ctrl 0 x7010.DEFINE P_INT_Ctrl 0 x7010.DEFINE P_INT_CLEAR 0 x7011.DEFINE P_INT_CLEAR 0 x7011.DEFINE P_INT_CTRL_New 0 x702D.DEFINE P_INT_CTRL_New 0 x702D.RAM.RAM.VAR TIME2 /2Hz.VAR TIME2 /2Hz计数器计数器计数器计数器.VAR TIME4 /4Hz.VAR TIME4 /4Hz计数器计数器计数器计数器.CODE.CODE.PUBLIC _main/.PUBLIC _main/主程序主程序主程序主程序 _main:_main:INT offINT offR1=0 xffff /r1R1=0 xffff /r1的值为的值为的值为的值为0 xffff 0 xffff P_IOA_ATTRI=r1/IOAP_IOA_ATTRI=r1/IOA口设置为同相高电平输出口设置为同相高电平输出口设置为同相高电平输出口设置为同相高电平输出P_IOA_DIR=r1 P_IOA_DIR=r1 P_IOA_DATA=r1;P_IOA_DATA=r1;P_IOB_ATTRI=r1/IOBP_IOB_ATTRI=r1/IOB口设置为同相高电平输出口口设置为同相高电平输出口口设置为同相高电平输出口口设置为同相高电平输出口 P_IOB_DIR=r1 P_IOB_DIR=r1 P_IOB_DATA=r1;P_IOB_DATA=r1;R1=0 x000CR1=0 x000CP_INT_Ctrl=R1P_INT_Ctrl=R1P_INT_CTRL_New=R1P_INT_CTRL_New=R1R1=0R1=0TIME2=R1TIME2=R1TIME4=R1TIME4=R1INT IRQINT IRQLoopLoop:NOPNOPNOPNOPJMP JMP LoopLoop/=/=.TEXT.TEXT.TEXT.TEXT.PUBLIC _IRQ5.PUBLIC _IRQ5.PUBLIC _IRQ5.PUBLIC _IRQ5_IRQ5:_IRQ5:_IRQ5:_IRQ5:PUSH R1,R5 TO SPPUSH R1,R5 TO SPPUSH R1,R5 TO SPPUSH R1,R5 TO SPR1=0 x0008;R1=0 x0008;R1=0 x0008;R1=0 x0008;TEST R1,P_INT_Ctrl;/TEST R1,P_INT_Ctrl;/TEST R1,P_INT_Ctrl;/TEST R1,P_INT_Ctrl;/中断识别中断识别中断识别中断识别JNZ JNZ JNZ JNZ L_IRQ5_4HzL_IRQ5_4HzL_IRQ5_4HzL_IRQ5_4Hz;L_IRQ5_2Hz:L_IRQ5_2Hz:L_IRQ5_2Hz:L_IRQ5_2Hz:R1=0 x0004R1=0 x0004R1=0 x0004R1=0 x0004P_INT_Clear=R1;/P_INT_Clear=R1;/P_INT_Clear=R1;/P_INT_Clear=R1;/清中断清中断清中断清中断R2=R2=R2=R2=TIME2TIME2TIME2TIME2 /计数器计数器计数器计数器+1+1+1+1R2+=1R2+=1R2+=1R2+=1 TIME2TIME2TIME2TIME2=R2=R2=R2=R2CMP R2CMP R2CMP R2CMP R2,2 2 2 2/是否是否是否是否1S?1S?1S?1S?JBE JBE JBE JBE LED2Hz_OFF LED2Hz_OFF LED2Hz_OFF LED2Hz_OFF/否否否否,LED,LED,LED,LED灭灭灭灭R1=0 xFFF0 R1=0 xFFF0 R1=0 xFFF0 R1=0 xFFF0/是是是是,LED,LED,LED,LED亮亮亮亮P_IOA_DATA=R1P_IOA_DATA=R1P_IOA_DATA=R1P_IOA_DATA=R1CMP R2,4 CMP R2,4 CMP R2,4 CMP R2,4/是否是否是否是否2S?2S?2S?2S?JBE JBE JBE JBE LED2Hz_RET LED2Hz_RET LED2Hz_RET LED2Hz_RET/否否否否,中断返回中断返回中断返回中断返回R2=0 R2=0 R2=0 R2=0/是是是是,清清清清TIME2TIME2TIME2TIME2TIME2=R2TIME2=R2TIME2=R2TIME2=R2JMP LED2Hz_RETJMP LED2Hz_RETJMP LED2Hz_RETJMP LED2Hz_RETLED2Hz_OFF:LED2Hz_OFF:LED2Hz_OFF:LED2Hz_OFF:R1=0 xFFFFR1=0 xFFFFR1=0 xFFFFR1=0 xFFFFP_IOA_DATA=R1P_IOA_DATA=R1P_IOA_DATA=R1P_IOA_DATA=R1LED2Hz_RETLED2Hz_RETLED2Hz_RETLED2Hz_RETPOP R1,R5 FROM SP;POP R1,R5 FROM SP;POP R1,R5 FROM SP;POP R1,R5 FROM SP;RETI;RETI;RETI;RETI;L_IRQ5_4Hz:L_IRQ5_4Hz:L_IRQ5_4Hz:L_IRQ5_4Hz:R1=0 x0008R1=0 x0008R1=0 x0008R1=0 x0008P_INT_Clear=R1;/P_INT_Clear=R1;/P_INT_Clear=R1;/P_INT_Clear=R1;/清中断清中断清中断清中断R2=R2=R2=R2=TIME4TIME4TIME4TIME4 /计数器计数器计数器计数器+1+1+1+1R2+=1R2+=1R2+=1R2+=1 TIME4TIME4TIME4TIME4=R2=R2=R2=R2CMP R2CMP R2CMP R2CMP R2,2 2 2 2/是否是否是否是否0.5S?0.5S?0.5S?0.5S?JBE JBE JBE JBE LED4Hz_OFFLED4Hz_OFFLED4Hz_OFFLED4Hz_OFF/否否否否,LED,LED,LED,LED灭灭灭灭R1=0 xFFF0R1=0 xFFF0R1=0 xFFF0R1=0 xFFF0/是是是是,LED,LED,LED,LED亮亮亮亮P_IOB_DATA=R1P_IOB_DATA=R1P_IOB_DATA=R1P_IOB_DATA=R1CMP R2,4CMP R2,4CMP R2,4CMP R2,4/是否是否是否是否1S?1S?1S?1S?JBE JBE JBE JBE LED4Hz_RETLED4Hz_RETLED4Hz_RETLED4Hz_RET/否否否否,中断返回中断返回中断返回中断返回R2=0R2=0R2=0R2=0/是是是是,清清清清TIME4TIME4TIME4TIME4TIME4=R2TIME4=R2TIME4=R2TIME4=R2JMP LED4Hz_RETJMP LED4Hz_RETJMP LED4Hz_RETJMP LED4Hz_RETLED4Hz_OFF:LED4Hz_OFF:LED4Hz_OFF:LED4Hz_OFF:R1=0 xFFFFR1=0 xFFFFR1=0 xFFFFR1=0 xFFFFP_IOA_DATA=R1P_IOA_DATA=R1P_IOA_DATA=R1P_IOA_DATA=R1LED4Hz_RETLED4Hz_RETLED4Hz_RETLED4Hz_RETPOP R1,R5 FROM SP;POP R1,R5 FROM SP;POP R1,R5 FROM SP;POP R1,R5 FROM SP;RETI;RETI;RETI;RETI;例例6.2键唤醒,键唤醒,A口键盘输入,口键盘输入,B口口LED输出。输出。.include Hardwore.inc/.include Hardwore.inc/硬件资源接口硬件资源接口硬件资源接口硬件资源接口.define P_IOA_RL 0 x7004.define P_IOA_RL 0 x7004/P_IOA_LatchP_IOA_Latch .CODE.CODE.PUBLIC _main.PUBLIC _main_main:_main:r1=0 r1=0 /IOA /IOA为带下拉电阻输入。为带下拉电阻输入。为带下拉电阻输入。为带下拉电阻输入。P_IOA_Dir=r1 P_IOA_Dir=r1 P_IOA_Attr=r1 P_IOA_Attr=r1 P_IOA_Data=r1 P_IOA_Data=r1 r1=0 xFFFF /IOB r1=0 xFFFF /IOB低电平输出低电平输出低电平输出低电平输出 P_IOB_Dir=r1 P_IOB_Dir=r1 P_IOB_Attrib=r1 P_IOB_Attrib=r1 r1=0 r1=0 P_IOA_Data=r1 P_IOA_Data=r1 INT OFF INT OFFKeydown:Keydown:R1=P_IOA_Data R1=P_IOA_Data CMP R1,0 x0001 CMP R1,0 x0001 JNE JNE KeydownKeydownKeyup:Keyup:R1=P_IOA_Data R1=P_IOA_Data CMP R1,0 x0000 CMP R1,0 x0000 JNE JNE KeyupKeyup R1=0 xFFFF R1=0 xFFFF P_IOA_Data=R1 P_IOA_Data=R1/LED/LED灭灭灭灭?R1=0 x0080 R1=0 x0080 P_INT_Ctrl=R1 P_INT_Ctrl=R1/开开开开IRQ3_KEYIRQ3_KEY中断中断中断中断 R1=P_IOA_RL R1=P_IOA_RL/激活键唤醒激活键唤醒激活键唤醒激活键唤醒 INT IRQ INT IRQ R1=0 x0007 R1=0 x0007 P_SystemClock=R1 P_SystemClock=R1/CPU/CPU入睡入睡入睡入睡 R1=0 xFFFE R1=0 xFFFE/低位为低位为低位为低位为0 0 R4=0 xFFFF R4=0 xFFFF R4=R4 LSL 4 R4=R4 LSL 4/SR/SR置全置全置全置全1 1 R2=0 xFFFF R2=0 xFFFFL_Loopin:L_Loopin:R2-=1 R2-=1 JNZ JNZ L_LoopinL_Loopin R1=R1 ROL 1 R1=R1 ROL 1/循环左移循环左移循环左移循环左移 P_IOB_Data=R1 P_IOB_Data=R1/LED/LED显示显示显示显示 CMP R1,0 xFF7F CMP R1,0 xFF7F/是否最后一个是否最后一个是否最后一个是否最后一个LEDLED JNE JNE L_LoopinL_Loopin R1=0 xFFFE R1=0 xFFFE JMP JMP L_LoopinL_Loopin这个循环起什这个循环起什么作用?么作用?低位低位LED会亮会亮吗?程序如何吗?程序如何修改?修改?/_IRQ3/_IRQ3中断服务程序中断服务程序中断服务程序中断服务程序.TEXT.TEXT.PUBLIC _IRQ3.PUBLIC _IRQ3_IRQ3:_IRQ3:PUSH R1,R4 TO SP PUSH R1,R4 TO SP R1=0 x0080 R1=0 x0080 TEST R1,P_INT_Ctrl TEST R1,P_INT_Ctrl JZ JZ L_NOTKeyArouseL_NOTKeyArouse R1=0 x0080 R1=0 x0080 P_INT_Clear=R1 P_INT_Clear=R1L_NOTKeyArouse:L_NOTKeyArouse:POP R1,R4 FROM SP POP R1,R4 FROM SP RETI RETI.END.END交换后,高位交换后,高位LED会亮吗?程会亮吗?程序如何修改?序如何修改?P_IOB_Data=R1 /LED P_IOB_Data=R1 /LED显示显示显示显示 R1=R1 ROL 1 R1=R1 ROL 1 /循环左移循环左移循环左移循环左移CMP R1,0 xFEFFCMP R1,0 xFEFF用用用用C CC C语言编写中断服务程序语言编写中断服务程序语言编写中断服务程序语言编写中断服务程序 函数库函数库函数库函数库SPCE061.LIBSPCE061.LIB提供了相提供了相提供了相提供了相关的底层函数,中断服务程序可以关的底层函数,中断服务程序可以关的底层函数,中断服务程序可以关的底层函数,中断服务程序可以直接用这些函数来描述。直接用这些函数来描述。直接用这些函数来描述。直接用这些函数来描述。SPCE061.LIB中与中断相关的函数中与中断相关的函数函数原形:函数原形:函数原形:函数原形:void Set_INT_Ctrl(unsigned int);void Set_INT_Ctrl(unsigned int);void Set_INT_Mask(unsigned int);void Set_INT_Mask(unsigned int);unsigned int Get_INT_Ctrl(void);unsigned int Get_INT_Ctrl(void);unsigned int Get_INT_Mask(void);unsigned int Get_INT_Mask(void);void INT_Clear(unsigned int);void INT_Clear(unsigned int);void FIQ_ON(void)void FIQ_ON(void)void FIQ_OFF(void);void FIQ_OFF(void);void IRQ_ON(void);void IRQ_ON(void);void IRQ_OFF(void);void IRQ_OFF(void);void INT_FIQ(void);void INT_FIQ(void);void INT_IRQ(void);void INT_IRQ(void);void INT_FIQ_IRQ(void);void INT_FIQ_IRQ(void);void INT_OFF(void);void INT_OFF(void);FIQ_ON();FIQ_ON();FIQ ONFIQ ONFIQ_OFF();FIQ_OFF();FIQ OFFFIQ OFFIRQ_ON();IRQ_ON();IRQ ONIRQ ONIRQ_OFF();IRQ_OFF();IRQ OFFIRQ OFFINT_FIQ();INT FIQINT_FIQ();INT FIQINT_IRQ();INT IRQINT_IRQ();INT IRQINT_FIQ_IRQ();INT FIQ_IRQINT_FIQ_IRQ();INT FIQ_IRQINT_OFF();INT OFFINT_OFF();INT OFF例例6.4A口的口的LED按按1S速率闪烁速率闪烁#include “SPCE061.H”#include “SPCE061.H”main()main()asm(“INT OFF”);asm(“INT OFF”);*P_IOA_Dir=0 x00FF;*P_IOA_Dir=0 x00FF;*P_IOA_Attib=0 x00FF;*P_IOA_Attib=0 x00FF;*P_IOA_DATA=0 x0000;*P_IOA_DATA=0 x0000;/A/A口为输出,低电平。口为输出,低电平。口为输出,低电平。口为输出,低电平。*P_INT_Ctrl=C_IRQ5_2Hz;*P_INT_Ctrl=C_IRQ5_2Hz;/0004H/0004H P_INT_Ctrl P_INT_Ctrl asm(“INT IRQ”);asm(“INT IRQ”);while(1)while(1)/死循环。死循环。死循环。死循环。*P_Watchdog_Clear=C_WDTCLR *P_Watchdog_Clear=C_WDTCLR#include “SPCE061.H”#include “SPCE061.H”unsigned int g_uioutput=0;unsigned int g_uioutput=0;void IRQ5(void)_attribute_(ISR)void IRQ5(void)_attribute_(ISR)void IRQ5(void)void IRQ5(void)if(*P_INT_Ctrl&0 x0004)if(*P_INT_Ctrl&0 x0004)/IRQ5_2Hz /IRQ5_2Hz*P_IOA_Data=g_uioutput;*P_IOA_Data=g_uioutput;g_uioutput=0 xFFFF;g_uioutput=0 xFFFF;/取反取反取反取反*P_INT_Clear=0 x0004;*P_INT_Clear=0 x0004;/C_IRQ5_2Hz/C_IRQ5_2Hz else else /IRQ5_4Hz /IRQ5_4Hz *P_INT_Clear=0 x0008;*P_INT_Clear=0 x0008;/C_IRQ5_4Hz/C_IRQ5_4Hz 如果不喜欢使用如果不喜欢使用如果不喜欢使用如果不喜欢使用SPCE061.HSPCE061.H,也不想包含,也不想包含,也不想包含,也不想包含SPCE061.libSPCE061.lib,你也可以象下面这样写程,你也可以象下面这样写程,你也可以象下面这样写程,你也可以象下面这样写程序,其实,这里只是将序,其实,这里只是将序,其实,这里只是将序,其实,这里只是将SPCE061.libSPCE061.lib中我们用到的部分代码摘出来了。中我们用到的部分代码摘出来了。中我们用到的部分代码摘出来了。中我们用到的部分代码摘出来了。volatile unsigned int*P_IOA_Data=(unsigned int*)(0 x7000);/Data vector for IOAvolatile unsigned int*P_IOA_Data=(unsigned int*)(0 x7000);/Data vector for IOAvolatile unsigned int*P_IOA_Dir=(unsigned int*)(0 x7002);/Direction vector for volatile unsigned int*P_IOA_Dir=(unsigned int*)(0 x7002);/Direction vector for IOAIOAvolatile unsigned int*P_IOA_Attrib=(unsigned int*)(0 x7003);/Attribute vector volatile unsigned int*P_IOA_Attrib=(unsigned int*)(0 x7003);/Attribute vector for IOAfor IOAvolatile unsigned int*P_IOB_Data=(unsigned int*)(0 x7005);/Data vector for IOBvolatile unsigned int*P_IOB_Data=(unsigned int*)(0 x7005);/Data vector for IOBvolatile unsigned int*P_IOB_Dir=(unsigned int*)(0 x7007);/Direction vector for volatile unsigned int*P_IOB_Dir=(unsigned int*)(0 x7007);/Direction vector for IOBIOBvolatile unsigned int*P_IOB_Attrib=(unsigned int*)(0 x7008);/Attribute vector volatile unsigned int*P_IOB_Attrib=(unsigned int*)(0 x7008);/Attribute vector for IOBfor IOBISR-interrupt service routine例例6.5不同中断入口的中断源程序不同中断入口的中断源程序举例。举例。LED,A口为口为1S、B口口0.5S闪烁。闪烁。#include “SPCE061.H”#include “SPCE061.H”#define TIMER_DATA_FOR_4KHz (65535-2048)#define TIMER_DATA_FOR_4KHz (65535-2048)/计数器初值计数器初值计数器初值计数器初值main()main()asm(“INT OFF”);asm(“INT OFF”);*P_IOA_Dir=0 xFFFF;*P_IOA_Dir=0 xFFFF;*P_IOA_Attib=0 xFFFF;*P_IOA_Attib=0 xFFFF;*P_IOA_Data=0 x00FF;*P_IOA_Data=0 x00FF;/A /A口为输出,低口为输出,低口为输出,低口为输出,低8 8位输出低电平。位输出低电平。位输出低电平。位输出低电平。*P_IOB_Dir=0 xFFFF;*P_IOB_Dir=0 xFFFF;*P_IOB_Attib=0 xFFFF;*P_IOB_Attib=0 xFFFF;*P_IOB_Data=0 x00FF;*P_IOB_Data=0 x00FF;/B /B口为输出,低口为输出,低口为输出,低口为输出,低8 8位输出低电平。位输出低电平。位输出低电平。位输出低电平。*P_Timer_Data=TIMER_DATA_FOR_4KHz;*P_Timer_Data=TIMER_DATA_FOR_4KHz;/0.5S/0.5S,20482048次分频次分频次分频次分频*P_Timer_Ctrl=C_SourceA_4096Hz*P_Timer_Ctrl=C_SourceA_4096Hz/0004H/0004H (P_Timer_Ctrl)P_Timer_Ctrl)*P_INT_Ctrl=C_IRQ4_1KHz*P_INT_Ctrl=C_IRQ4_1KHz|C_IRQ2_TMB;C_IRQ2_TMB;/允许中断允许中断允许中断允许中断 asm(“INT IRQ”);asm(“INT IRQ”);while(1)while(1)/死循环。死循环。死循环。死循环。*P_Watchdog_Clear=C_WDTCLR *P_Watchdog_Clear=C_WDTCLR#include “SPCE061.H”#include “SPCE061.H”unsigned int g_uiIOA_LED=0 xFFunsigned int g_uiIOA_LED=0 xFF,g_uiIOB_LED=0 xFF;g_uiIOB_LED=0 xFF;unsigned int g_uiClockCnt=0;unsigned int g_uiClockCnt=0;void IRQ2(void)_attribute_(ISR)void IRQ2(void)_attribute_(ISR);void IRQ2(void)void IRQ2(void)/TimerB/TimerB 中断中断中断中断*P_IOB_Data=g_uiIOB_LED*P_IOB_Data=g_uiIOB_LED;g_uiIOB_LED=0 xFFFF;g_uiIOB_LED=0 xFFFF;/取反取反取反取反*P_INT_Clear=0 x0400;*P_INT_Clear=0 x0400;/清该中断标志清该中断标志清该中断标志清该中断标志 void IRQ4(void)_attribute_(ISR)void IRQ4(void)_attribute_(ISR);void IRQ4(void)void IRQ4(void)IF*P_INT_Ctrl&C_IRQ4_1KHzIF*P_INT_Ctrl&C_IRQ4_1KHz/如果是如果是如果是如果是IRQ4_1KHzIRQ4_1KHz中断中断中断中断 IF g_uiClockCnt 1024 IF g_uiClockCnt 1024/且计数器且计数器且计数器且计数器10240.25S?TIME4+1TIME4+1LED亮亮TIME4清清0清清清清4KHz4KHz中断标志中断标志中断标志中断标志LED灭灭0.5S?寄存器组出栈恢复寄存器组出栈恢复寄存器组出栈恢复寄存器组出栈恢复1S?TIME1+1LED亮亮TIME1清清0清清清清1KHz中断标志中断标志中断标志中断标志2S?中断返回中断返回中断返回中断返回YYYYIRQ4IRQ4中断程序中断程序中断程序中断程序Y2KHz中断中断?0.5S?TIME2+1TIME2+1LED亮亮TIME2清清0清清清清2KHz2KHz中断标志中断标志中断标志中断标志LED灭灭1S?YYLED灭灭否则,否则,4KHz中断中断YNIOA0,1IOA4,7IOA2,3程序设计程序设计.DEFINE P_IOA_DATA 0 x7000.DEFINE P_IOA_DATA 0 x7000.DEFINE P_IOA_DIR 0 x7002.DEFINE P_IOA_DIR 0 x7002.DEFINE P_IOA_ATTRI 0 x7003.DEFINE P_IOA_ATTRI 0 x7003.DEFINE P_INT_Ctrl 0 x7010.DEFINE P_INT_Ctrl 0 x7010.DEFINE P_INT_CLEAR 0 x7011.DEFINE P_INT_CLEAR 0 x7011.DEFINE P_WATCHDOG_CLEAR 0 x7012.DEFINE P_WATCHDOG_CLEAR 0 x7012.RAM.RAM.VAR TIME1 /1KHz.VAR TIME1 /1KHz计数器计数器计数器计数器.VAR TIME2 /2KHz.VAR TIME2 /2KHz计数器计数器计数器计数器.VAR TIME4 /4KHz.VAR TIME4 /4KHz计数器计数器计数器计数器.CODE.CODE.PUBLIC _main/.PUBLIC _main/主程序主程序主程序主程序 _main:_main:INT offINT offR1=0 xffff /r1R1=0 xffff /r1的值为的值为的值为的值为0 xffff 0 xffff P_IOA_ATTRI=r1/IOAP_IOA_ATTRI=r1/IOA口设置为同相低电平输出口设置为同相低电平输出口设置为同相低电平输出口设置为同相低电平输出P_IOA_DIR=r1;P_IOA_DIR=r1;R1=0R1=0P_IOA_DATA=r1;P_IOA_DATA=r1;R1=0 x0070R1=0 x0070P_INT_Ctrl=R1P_INT_Ctrl=R1R1=0R1=0TIME1=R1TIME1=R1TIME2=R1TIME2=R1TIME4=R1TIME4=R1INT onINT onLoopLoop:R1=1R1=1P_WATCHDOG_CLEAR=R1P_WATCHDOG_CLEAR=R1JMP JMP LoopLoop/=/=.TEXT.T