《【教学课件】第6章中断系统.ppt》由会员分享,可在线阅读,更多相关《【教学课件】第6章中断系统.ppt(23页珍藏版)》请在三一办公上搜索。
1、第6章 中断系统,6.1 中断的一般概念,CPU对突发事件响应方式中断源及中断申请开中断与关中断中断优先级及中断嵌套中断向量与中断处理,一个应用系统,面对处理器众多的中,是根据应用来选取允许中断的事件与不允许中断的事件。,中断向量,是中断源提供的处理程序的入口,其入口处,是由系统统一划拨存储空间,给各中断源存放中断处理程序的入口,即中断向量表。中断响应过程:申请-响应(终止当前程序-保护断点-转到处理程序。)-中断处理(保护现场-处理-清该标志、恢复现场-返回。软件)。,硬件实现,软件完成,6.2 SPCE061A中断系统,中断类型与中断源,异常中断-RESET,向量地址为FFF7H。优先级最
2、高。软中断-执行指令BREAK,向量地址为FFF5H。优先级最低。事件中断-外部与片内I/O事件。快速中断FIQ与中断IRQ。FIQ优先级高于IRQ。,如果同时有两个以上的IRQ中断时,按此优先级响应。但较“高级别”IRQ中断不能中断“较低级别”IRQ中断。即在IRQ中断中不允许中断嵌套。,事件中断,中断服务程序以此取名。,中断控制寄存器,P_INT_Ctrl(7010H)读/写 读,中断标志字,用于判别产生中断的源。写,选择允许中断的源。P_INT_Clear(7011H)写 清中断标志。选择的源与中断标志字对应。写“1”清该中断标志。P_INT_Ctrl_New(702DH)读/写 激活与
3、屏蔽中断。读,了解哪些中断源被屏蔽。写,对应0的中断源被屏蔽。对应1的中断源被激活。,中断控制字,每一位对应一个事件,如果通过P_INT_Ctrl向某位写1,则允许该事件向CPU申请中断。如果通过P_INT_Ctrl读,是读中断标志,置1的位,说明该事件已向CPU申请了中断。P_INT_Ctrl_New端口的每一位,也对应表中的事件。如果通过P_INT_Clear向某位写1,则清除该事件的中断标志。,注意:只有在P_INT_Ctrl中设置了中断源允许位为“1”,相应开/关中断指令才有意义。,中断控制指令,例6.1IRQ5有2个中断源,2Hz和4Hz。TIME2对2Hz中断计数,TIME4对4H
4、z中断计数。2Hz中断控制A口0LED以2S速率闪烁,4Hz中断控制B口0LED以1S速率闪烁。,Y,否,就是2Hz中断。,.DEFINE P_IOA_DATA 0 x7000.DEFINE P_IOA_DIR 0 x7002.DEFINE P_IOA_ATTRI 0 x7003.DEFINE P_IOB_DATA 0 x7005.DEFINE P_IOB_DIR 0 x7007.DEFINE P_IOB_ATTRI 0 x7008.DEFINE P_INT_Ctrl 0 x7010.DEFINE P_INT_CLEAR 0 x7011.DEFINE P_INT_CTRL_New 0 x702
5、D.RAM.VAR TIME2/2Hz计数器.VAR TIME4/4Hz计数器.CODE.PUBLIC _main/主程序 _main:INT offR1=0 xffff/r1的值为0 xffff P_IOA_ATTRI=r1/IOA口设置为同相高电平输出P_IOA_DIR=r1 P_IOA_DATA=r1;,P_IOB_ATTRI=r1/IOB口设置为同相高电平输出口 P_IOB_DIR=r1 P_IOB_DATA=r1;R1=0 x000CP_INT_Ctrl=R1P_INT_CTRL_New=R1R1=0TIME2=R1TIME4=R1INT IRQLoop:NOPNOPJMP Loop
6、/=.TEXT.PUBLIC _IRQ5_IRQ5:PUSH R1,R5 TO SPR1=0 x0008;TEST R1,P_INT_Ctrl;/中断识别JNZ L_IRQ5_4Hz;,L_IRQ5_2Hz:R1=0 x0004P_INT_Clear=R1;/清中断R2=TIME2/计数器+1R2+=1TIME2=R2CMP R2,2/是否1S?JBE LED2Hz_OFF/否,LED灭R1=0 xFFF0/是,LED亮P_IOA_DATA=R1CMP R2,4/是否2S?JBE LED2Hz_RET/否,中断返回R2=0/是,清TIME2TIME2=R2JMP LED2Hz_RETLED2H
7、z_OFF:R1=0 xFFFFP_IOA_DATA=R1LED2Hz_RETPOP R1,R5 FROM SP;RETI;,L_IRQ5_4Hz:R1=0 x0008P_INT_Clear=R1;/清中断R2=TIME4/计数器+1R2+=1TIME4=R2CMP R2,2/是否0.5S?JBE LED4Hz_OFF/否,LED灭R1=0 xFFF0/是,LED亮P_IOB_DATA=R1CMP R2,4/是否1S?JBE LED4Hz_RET/否,中断返回R2=0/是,清TIME4TIME4=R2JMP LED4Hz_RETLED4Hz_OFF:R1=0 xFFFFP_IOA_DATA=R
8、1LED4Hz_RETPOP R1,R5 FROM SP;RETI;,例6.2键唤醒,A口键盘输入,B口LED输出。,.include Hardwore.inc/硬件资源接口.define P_IOA_RL 0 x7004/P_IOA_Latch.CODE.PUBLIC _main_main:r1=0/IOA为带下拉电阻输入。P_IOA_Dir=r1 P_IOA_Attr=r1 P_IOA_Data=r1 r1=0 xFFFF/IOB低电平输出 P_IOB_Dir=r1 P_IOB_Attrib=r1 r1=0 P_IOA_Data=r1 INT OFF,Keydown:R1=P_IOA_Da
9、ta CMP R1,0 x0001 JNE KeydownKeyup:R1=P_IOA_Data CMP R1,0 x0000 JNE Keyup R1=0 xFFFF P_IOA_Data=R1/LED灭?R1=0 x0080 P_INT_Ctrl=R1/开IRQ3_KEY中断 R1=P_IOA_RL/激活键唤醒 INT IRQ R1=0 x0007 P_SystemClock=R1/CPU入睡,R1=0 xFFFE/低位为0 R4=0 xFFFF R4=R4 LSL 4/SR置全1 R2=0 xFFFFL_Loopin:R2-=1 JNZ L_Loopin R1=R1 ROL 1/循环左移
10、 P_IOB_Data=R1/LED显示 CMP R1,0 xFF7F/是否最后一个LED JNE L_Loopin R1=0 xFFFE JMP L_Loopin,这个循环起什么作用?,低位LED会亮吗?程序如何修改?,/_IRQ3中断服务程序.TEXT.PUBLIC _IRQ3_IRQ3:PUSH R1,R4 TO SP R1=0 x0080 TEST R1,P_INT_Ctrl JZ L_NOTKeyArouse R1=0 x0080 P_INT_Clear=R1L_NOTKeyArouse:POP R1,R4 FROM SP RETI.END,交换后,高位LED会亮吗?程序如何修改?,
11、P_IOB_Data=R1/LED显示 R1=R1 ROL 1/循环左移,CMP R1,0 xFEFF,用C语言编写中断服务程序,函数库SPCE061.LIB提供了相关的底层函数,中断服务程序可以直接用这些函数来描述。,SPCE061.LIB中与中断相关的函数,函数原形:void Set_INT_Ctrl(unsigned int);void Set_INT_Mask(unsigned int);unsigned int Get_INT_Ctrl(void);unsigned int Get_INT_Mask(void);void INT_Clear(unsigned int);void FI
12、Q_ON(void)void FIQ_OFF(void);void IRQ_ON(void);void IRQ_OFF(void);void INT_FIQ(void);void INT_IRQ(void);void INT_FIQ_IRQ(void);void INT_OFF(void);,FIQ_ON();FIQ ONFIQ_OFF();FIQ OFFIRQ_ON();IRQ ONIRQ_OFF();IRQ OFFINT_FIQ();INT FIQINT_IRQ();INT IRQINT_FIQ_IRQ();INT FIQ_IRQINT_OFF();INT OFF,例6.4A口的LED按1
13、S速率闪烁,#include“SPCE061.H”main()asm(“INT OFF”);*P_IOA_Dir=0 x00FF;*P_IOA_Attib=0 x00FF;*P_IOA_DATA=0 x0000;/A口为输出,低电平。*P_INT_Ctrl=C_IRQ5_2Hz;/0004H P_INT_Ctrl asm(“INT IRQ”);while(1)/死循环。*P_Watchdog_Clear=C_WDTCLR,#include“SPCE061.H”unsigned int g_uioutput=0;void IRQ5(void)_attribute_(ISR)void IRQ5(v
14、oid)if(*P_INT_Ctrl/C_IRQ5_4Hz,如果不喜欢使用SPCE061.H,也不想包含SPCE061.lib,你也可以象下面这样写程序,其实,这里只是将SPCE061.lib中我们用到的部分代码摘出来了。volatile 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 IOAvolatile unsigned int*P_IOA_A
15、ttrib=(unsigned int*)(0 x7003);/Attribute vector for IOAvolatile 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 IOBvolatile unsigned int*P_IOB_Attrib=(unsigned int*)(0 x7008);/Attribute vector for IO
16、B,ISR-interrupt service routine,例6.5不同中断入口的中断源程序举例。LED,A口为1S、B口0.5S闪烁。,#include“SPCE061.H”#define TIMER_DATA_FOR_4KHz(65535-2048)/计数器初值main()asm(“INT OFF”);*P_IOA_Dir=0 xFFFF;*P_IOA_Attib=0 xFFFF;*P_IOA_Data=0 x00FF;/A口为输出,低8位输出低电平。*P_IOB_Dir=0 xFFFF;*P_IOB_Attib=0 xFFFF;*P_IOB_Data=0 x00FF;/B口为输出,低
17、8位输出低电平。*P_Timer_Data=TIMER_DATA_FOR_4KHz;/0.5S,2048次分频*P_Timer_Ctrl=C_SourceA_4096Hz/0004H(P_Timer_Ctrl)*P_INT_Ctrl=C_IRQ4_1KHz|C_IRQ2_TMB;/允许中断 asm(“INT IRQ”);while(1)/死循环。*P_Watchdog_Clear=C_WDTCLR,#include“SPCE061.H”unsigned int g_uiIOA_LED=0 xFF,g_uiIOB_LED=0 xFF;unsigned int g_uiClockCnt=0;voi
18、d IRQ2(void)_attribute_(ISR);void IRQ2(void)/TimerB 中断*P_IOB_Data=g_uiIOB_LED;g_uiIOB_LED=0 xFFFF;/取反*P_INT_Clear=0 x0400;/清该中断标志void IRQ4(void)_attribute_(ISR);void IRQ4(void)IF*P_INT_Ctrl/计数器加1,ELSE*P_IOA_Data=g_uiIOA_LED;/显示 g_uiIOA_LED=0 xFFFF;/取反,下一个显示状态 g_uiClockCnt=0/计数器清0*P_INT_Clear=C_IRQ4_
19、1KHz;/清该中断标志 IF*P_INT_Ctrl/清IRQ4_4KHz中断标志,【实验六】IRQ4中断实验,LED闪亮,1KHz中断控制,2KHz中断控制,4KHz中断控制,1KHz中断,1mS中断。2KHz中断,0.5mS中断。4KHz中断,0.25mS中断。,寄存器组压栈保护,1KHz中断?,0.25S?,TIME4+1,LED亮,TIME4清0,清4KHz中断标志,LED灭,0.5S?,寄存器组出栈恢复,1S?,TIME1+1,LED亮,TIME1清0,清1KHz中断标志,2S?,中断返回,Y,Y,Y,Y,IRQ4中断程序,Y,2KHz中断?,0.5S?,TIME2+1,LED亮,T
20、IME2清0,清2KHz中断标志,LED灭,1S?,Y,Y,LED灭,否则,4KHz中断,Y,N,IOA0,1,IOA4,7,IOA2,3,程序设计,.DEFINE P_IOA_DATA 0 x7000.DEFINE P_IOA_DIR 0 x7002.DEFINE P_IOA_ATTRI 0 x7003.DEFINE P_INT_Ctrl 0 x7010.DEFINE P_INT_CLEAR 0 x7011.DEFINE P_WATCHDOG_CLEAR 0 x7012.RAM.VAR TIME1/1KHz计数器.VAR TIME2/2KHz计数器.VAR TIME4/4KHz计数器.COD
21、E.PUBLIC _main/主程序 _main:INT offR1=0 xffff/r1的值为0 xffff P_IOA_ATTRI=r1/IOA口设置为同相低电平输出P_IOA_DIR=r1;R1=0P_IOA_DATA=r1;,R1=0 x0070P_INT_Ctrl=R1R1=0TIME1=R1TIME2=R1TIME4=R1INT onLoop:R1=1P_WATCHDOG_CLEAR=R1JMP Loop/=.TEXT.PUBLIC _IRQ4_IRQ4:PUSH R1,R5 TO SPR1=0 x0010;/中断识别TEST R1,P_INT_Ctrl;/是1KHz?JNZ L_
22、IRQ4_1KHz;R1=0 x0020;TEST R1,P_INT_Ctrl;/是2KHz?JNZ L_IRQ4_2KHz;,L_IRQ4_4KHz:R2=TIME4/TIME4计数器+1R2+=1TIME4=R2R1=P_IOA_DATA/读A引脚状态CMP R2,0 x0400/是否0.25S?JBE LED4KHz_OFF/是,LED灭R1|=0 x00F0/否,47 LED亮P_IOA_DATA=R1CMP R2,0 x0800/是否 0.5S?JBE LED4KHz_RET/是,47 LED继续亮中断返回R2=0/否,清TIME4,在下一次中断灭TIME4=R2JMP LED4KH
23、z_RETLED4Hz_OFF:R1,L_IRQ4_1KHz:R2=TIME1/计数器+1R2+=1TIME1=R2R1=P_IOA_DATACMP R2,0 x0400/是否1S?JBE LED1KHz_OFF/小于等于,LED灭R1|=0 x0003/否则,01LEDP_IOA_DATA=R1CMP R2,0800/是否2S?JBE LED1KHz_RET/小于等于,01LED继续亮,中断返回R2=0/是,清TIME1,在下一次中断灭TIME1=R2JMP LED2Hz_RETLED1KHz_OFF:R1,L_IRQ4_2KHz:R2=TIME2/计数器+1R2+=1TIME2=R2R1=
24、P_IOA_DATACMP R2,0 x0400/是否0.5S?JBE LED2KHz_OFF/小于等于,LED灭R1|=0 x000C/否则,23 LED亮P_IOA_DATA=R1CMP R2,0800/是否小于等于1S?JBE LED2KHz_RET/小于等于,23 LED继续亮,中断返回R2=0/否则,清TIME2,在下一次中断灭TIME2=R2JMP LED2KHz_RETLED2KHz_OFF:R1,本章小结,掌握中断系统的一般概念CPU对I/O设备管理的方式中断优先级及中断嵌套开中断与关中断中断响应过程掌握nSPTM中断系统的基本特点中断源、中断事件、中断优先级、中断向量、中断响
25、应nSPTM系统的中断程序设计,作业:P106 3、6、,寄存器组压栈保护,TMB2中断?,1S?,计数器+1,LED亮,计数器清0,清Timer2中断标志,LED灭,2S?,寄存器组出栈恢复,0.5S?,计数器+1,LED亮,计数器清0,清Timer4中断标志,LED灭,1S?,中断返回,Y,Y,Y,Y,IRQ6中断程序,例6.3,例6.3程序,.include hardware.inc.DEFINE timea_clk 0 x020d;.DEFINE timeb_clk 0 x0004;.RAM.VAR TA_Flag.VAR TB_Flag.PUBLIC _main/主程序_main:I
26、NT offR1=0 xffffP_IOA_ATTRI=r1/IOA口设置为同相低电平输出P_IOA_DIR=r1R1=0P_IOA_DATA=r1;R1=0 xffffP_IOB_ATTRI=r1/IOB口设置为同相高电平输出P_IOB_DIR=r1R1=0P_IOB_DATA=r1;,R1=0 xFF9FP_TimerA_Data=R1P_TimerB_Data=R1R1=timea_clk P_TimerA_Ctrl=R1R2=0000000000000100BR1|=R2P_INT_Ctrl=R1INT IRQ,FIQL_LOOP:NOPNOPNOPJMP L_LOOP,.TEXT.P
27、UBLIC _FIQ_FIQ:PUSH R1,R5 TO SPR1=0 x0800;/中断识别TEST R1,P_INT_Ctrl;/是FIQ_TMB?JNZ L_FIQ_TMB;R1=0 x0200;/否,TEST R1,P_INT_Ctrl;/是FIQ_TMA?JNZ L_FIQ_TMA;L_FIQ_PWM:/否,是FIQ_PWMR1=0 x8000P_INT_Clear=R1POP R1,R5 FROM SPRETI,L_FIQ_TMA:R1=TA_FlagR1=0 xFFFFP_IOA_Data=R1TA_Flag=R1R1=0 x0200P_INT_Clear=R1POP R1,R5 FROM SPRETIL_FIQ_TMB:R1=0 x0800P_INT_Clear=R1POP R1,R5 FROM SPRETI,主程序,初始化A、B口关中断,S1键按下吗?,松开S1键吗?,LED灯灭,设置中断开中断,系统进入睡眠状态,循环点亮LED,有键按下吗?,Y,Y,Y,N,N,N,IOA0,IOA1,IOA2,IOA3,IOA4,IOA5,IOA6,IOA7,R8,
链接地址:https://www.31ppt.com/p-5659129.html