51单片机中断总结

合集下载

MCS-51单片机的中断系统

MCS-51单片机的中断系统

MCS-51单⽚机的中断系统单⽚机中断技术概述在任何⼀款事件驱动型的CPU⾥⾯都应该会有中断系统,因为中断就是为响应某种事件⽽存在的。

中断的灵活应⽤不仅能够实现想要的功能,⽽且合理的中断安排可以提⾼事件执⾏的效率,因此中断在单⽚机应⽤中的地位是⾮常重要的。

单⽚机中断(Interrupt)是硬件驱动事件,它使得CPU暂停当前的主程序,转⽽去执⾏⼀个中断服务⼦程序。

为了更形象地理解中断,下⾯以学⽣上⾃习时接电话为例阐述⼀下中断的概念。

单⽚机的中断系统有5个中断源、2个中断优先级,可实现两级中断服务程序嵌套。

如果单⽚机没有中断系统,单⽚机的⼤量时间可能会浪费在查询是否有服务请求发⽣的定时査询操作上。

采⽤中断技术完全消除了单⽚机在査询⽅式中的等待现象,⼤⼤地提⾼了单⽚机的⼯作效率和实时性。

单⽚机中断系统结构及中断控制中断系统结构图如图5-2所⽰。

由图5-2可见,MCS-51中断系统共有5个中断请求源:INT0——外部中断请求0,中断请求信号由INT0引脚输⼊。

定时/计数器T0计数溢出发出的中断请求。

INT1——外部中断请求1,中断请求信号由INT1引脚输⼊。

定时/计数器T1计数溢出发出的中断请求。

串⾏⼝中断请求。

中断优先级从⾼到底排列。

单⽚机如何知道有中断请求信号?是否能够响应该中断?若5个中断源请求信号同时到来,单⽚机如何响应?这些问题都可以由中断寄存器来解决。

单⽚机中断寄存器有中断标志寄存器TCON和SCON、中断使能寄存器IE和中断优先级寄存器IP,这些寄存器均为8位。

中断标志寄存器5个中断请求源的中断请求标志分别由TCON和SCON的相应位锁存,单⽚机通过这些中断标志位的状态便能知道具体是哪个中断源正在申请中断。

TCON寄存器TCON寄存器为定时/计数器的控制寄存器,字节地址为88H,可位寻址。

特殊功能寄存器TCON的格式如图5-3所⽰。

TCON各标志位功能如下。

TF1——定时/计数器T1的溢出中断请求标志位。

第05章 MCS-51单片机的中断与定时(1-4)

第05章 MCS-51单片机的中断与定时(1-4)

2
1
TH0
;P1.0输出“0” ;P1.0输出“1”
5.2 MCS-51单片机的中断系统
五、外中断应用举例
1. 中断初始化程序
设置外中断源的触发方式 设置中断允许寄存器IE 设置中断优先级寄存器IP
2. 中断服务程序
保护现场 中断处理 恢复现场
23/65
5.2 MCS-51单片机的中断系统
【例5-3】 设外部中断0为下降沿触发方 式,高优先级,试编写中断初始化程序
5.2 MCS-51单片机的中断系统
【例5-4】 将单脉冲接到外中断0(INT0)引脚,利 用P1.0作为输出,经反相器接发光二极管。编写程 序,每按动一次按钮,产生一个外中断信号,使发 光二极管的状态发生变化,由亮变暗,或反之
P1.0 单脉冲 发生器 INT0
1
+5V
8031
26/65
5.2 MCS-51单片机的中断系统
串口:0023H
20/65
5.2 MCS-51单片机的中断系统
四、中断请求的撤除
1.定时/计数器中断请求标志TF0/TF1会自动撤除 2.串行口中断请求标志TI/RI要用指令撤除
CLR TI ;清TI标志位 CLR RI ;清RI标志位
3.负脉冲触发的外中断请求标志IE0/IE1会自动撤除 4.低电平触发的外中断请求信号需要外加电路撤除
下次课前请预习5.3节
30/65
5.3 51单片机的定时器/计数器
MCS-51单片机内部有两个16位定时/计数器 T0和T1,简称定时器0和定时器1
在特殊功能寄存器TMOD和TCON的控制下, 它们既可以设定成定时器使用,也可以设定 成计数器使用
定时/计数器有4种工作方式,具有中断功能, 可以完成定时、计数、脉冲输出等任务

单片机中断系统总结

单片机中断系统总结

单片机中断系统总结1.其中当IE0,TF0,IE1,TF1,它们在TCON中的值为1时就会向CPU提出中断申请。

2.IT0和IT1是电平选择有效方式,1时是下降沿有效,0时是低电平有效。

3.INT0引脚接的是外面的中断信号,当其为有效电平时,IE0在TCON的值置1,向CPU 申请中断。

4.TCON是中断请求标志:5.IE是中断允许寄存器:6.中断源:7.8.中断处理过程9.中断初始化主要就是设定上面的三个问题以外部为中断源的外部中断的初始化EA=1;EX0=1;IT0=0;Void external () interrupt0{}定时中断的初始化TMOD=0x01TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;Void timer() interrupt 1{}TMOD定时特殊控制寄存器GATE:是门控信号,用于控制定时器的启动是否受外部中断源信号的影响。

当GATE=0时只要使TCON种TR0或者TR1为1就可以选择那个定时器工作了。

当GATE=1时,不仅要设置TR0或TR1的值,外部引脚INT0/1也为高电平是才可以申请中断。

C/T:定时/计数模式选择位。

C/T=0为定时模式;C/T=1时为计数模式。

M1和M0:工作方式设置位。

定时/计数器有四种工作方式。

TCONTF1:T1溢出中断请求标志位。

T1计数溢出时由硬件自动置TF1为1。

CPU响应中断后TF1由硬件自动清零。

T1工作时,CPU可随时查询TF1的状态。

TR1:T1运行控制位,TR1置1时,T1开始工作;TR1置0是T1停止工作。

TR1由软件置1或者置0。

所以用软件可以控制定时/计数器的启动与停止。

TF0和TR与上类似。

定时器的中断方式以串口行为中断源的串口中断1.SM0和SM1为工作选择方式,有四种工作方式。

2.SM23.REN是控制串行的接收位,当REN=1是,则启动串行口的接收。

51单片机外部中断详解-(最新版)

51单片机外部中断详解-(最新版)

一.外部中断相关寄存器1.定时器/计数器控制寄存器控制寄存器(TCON)IT0:外部中断0触发方式控制位当IT0=0时,为电平触发方式(低电平有效)当IT0=1时,为边沿触发方式(下降沿有效)IT1:外部中断1触发方式控制位当IT1=0时,为电平触发方式(低电平有效)当IT1=1时,为边沿触发方式(下降沿有效)2.中断允许控制寄存器(IE)EX0:外部中断0允许位;EX1:外部中断1允许位;EA :CPU中断允许(总允许)位。

二.外部中断的处理过程1、设置中断触发方式,即IT0=1或0,IT1=1或02、开对应的外部中断,即EX0=1或EX1=1;3、开总中断,即EA=1;4、等待外部设备产生中断请求,即通过,口连接外部设备产生中断5、中断响应,执行中断服务函数三.程序编写要求:通过两位按键连接外部中断0和1,设定外部中断0为下降沿触发方式,外部中断1为低电平触发方式,按键产生中断使数字加减,用一位共阳极数码管来显示数值。

目的:感受外部中断对程序的影响,体会低电平触发和下降沿触发的区别。

#include<>#define uint unsigned int #define uchar unsigned char uchar code dat[] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};uint num;void main(){EA=1; //开总中断IT0=1; //下降沿触发IT1=0; //低电平触发EX0=1; //外部中断0允许EX1=1; //外部中断1允许while(1){P0=dat[num%10];}}void plus() interrupt 0//外部中断0 {EX0=0;num++;EX0=1;}void minus() interrupt 2//外部中断1{EX1=0;num--;EX1=1;}。

51单片机的中断控制

51单片机的中断控制

AEH 0
ADH 0
ACH ES 0
ABH ET1
1
AAH EX1
0
A9H ET0
1
A8H EX0
0
用位操作指令
CLR ES CLR EX0 CLR EX1 SETB ET0 SETB ET1 SETB EA
; ES=0,禁止串行口中断 ; EX0=0,禁止外部中断0 ; EX1=0,禁止外部中断1 ; ET0=1,允许定时/计数中断0 ; ET1=1,允许定时/计数中断0 ; CPU开中断
TI SCON
IE EX0 EA ET0 EX1
ET1 ES
IP
PX0 1
0
PX1 1
0
PT0 1
0
PT1 1
0
PS 1
0

0003H 000BH 0013H 001BH 0023H
CPU
0003H 000BH 0013H 001BH 0023H

2. 51单片机的中断控制
中断的开放与禁止
51系列单片机的5个中断源都是可屏蔽中断,由中断系统内部的专用寄 存器IE负责控制各中断源的开放或屏蔽。
;EA位置 “1” ,CPU开中断 ;EX0位置 “1” ,允许外部中断0产生中断 ;PX1位置 “1” ,外部中断1为高级中断 ;IT1位置 “0” ,外部中断1为电平触发
3. 51单片机中断程序设计
采用中断的程序结构:
主程序起始地址0000H执行,在0000H处用无条件转移指令; 各中断入口地址,用无条件转移指令
IE A8H
7
6
EA
位地址
AF
5
4
3
2
1
0

简述51单片机各种中断源的中断请求原理

简述51单片机各种中断源的中断请求原理

基于我所了解的51单片机各种中断源的中断请求原理,我将根据深度和广度要求撰写一篇全面评估的文章,以帮助你更深入地理解这一主题。

让我们简要回顾一下51单片机中断系统的基本原理。

在51单片机中,中断请求是通过外部设备或内部事件来触发的,当中断源满足触发条件时,会向中断控制器发送中断请求信号,中断控制器会根据优先级和中断允许标志位来确定是否接受中断请求,并在合适的时机响应中断。

中断请求原理是指各种中断源触发中断请求的机制,包括外部中断、定时器中断、串口中断等。

1. 外部中断源的中断请求原理外部中断源是指外部设备通过外部中断引脚向51单片机发送中断请求信号。

当外部中断引脚检测到一个由低电平变为高电平(上升沿)或由高电平变为低电平(下降沿)的信号时,会触发外部中断请求。

这种中断请求原理适用于外部开关、传感器等外部设备向单片机发送中断信号的场景。

2. 定时器中断源的中断请求原理定时器中断源是指定时器溢出或达到设定值时向单片机发送中断请求信号。

定时器会在设定的时间间隔内不断递增计数,当计数值达到设定的溢出值时,会触发定时器中断请求。

这种中断请求原理适用于需要定时检测或定时执行任务的场景。

3. 串口中断源的中断请求原理串口中断源是指串口接收到数据或发送完成时向单片机发送中断请求信号。

当串口接收到数据或发送完成时,会触发串口中断请求。

这种中断请求原理适用于串口通信中需要实时处理数据的场景。

51单片机各种中断源的中断请求原理涵盖了外部中断、定时器中断和串口中断等多种情况。

理解和掌握这些中断请求原理,对于合理地设计中断服务程序和提高系统的实时性具有重要意义。

在个人观点和理解方面,我认为深入理解各种中断源的中断请求原理,可以帮助我们更好地设计和优化单片机系统的中断服务程序,提高系统的实时性和稳定性。

合理地利用中断请求原理,可以更好地利用单片机资源,提高系统的响应速度和效率。

在实际应用中,我们需要根据具体的需求和硬件环境,灵活运用各种中断源的中断请求原理,确保系统的稳定性和可靠性。

51单片机中断原理

51单片机中断原理

51单片机中断原理在单片机的世界里,中断就像是一个随时待命的“紧急事务处理员”。

当单片机正在执行主程序,忙得不可开交时,突然来了一些紧急情况,比如外部设备发来的数据需要立刻处理,或者定时时间到了需要执行特定的操作,这时候中断就发挥作用了。

要理解 51 单片机的中断原理,咱们得先从几个基本概念说起。

首先是中断源。

这可以想象成是引起中断的“源头”。

在 51 单片机中,常见的中断源有外部中断 0、外部中断 1、定时器/计数器 0 溢出中断、定时器/计数器 1 溢出中断,还有串行口中断。

这些中断源就像是不同的“紧急事务”,各自有着特定的触发条件。

比如说外部中断 0 和 1,通常是由外部引脚的电平变化引起的。

当设定的引脚从高电平变为低电平(或者反过来),就会触发相应的外部中断。

定时器/计数器的溢出中断呢,则是当定时器/计数器累计到设定的值时产生的。

这就好比一个闹钟,设定的时间一到,就会响铃提醒。

串行口中断则是在串行通信过程中,出现特定的通信事件时触发。

接下来是中断允许控制寄存器 IE。

它就像是一个“总开关”,决定哪些中断源被允许响应。

如果某个中断源对应的位被设置为 1,就表示允许这个中断源产生中断;如果是 0,就表示禁止。

然后是中断优先级控制寄存器 IP。

在多个中断源同时请求中断时,中断优先级就决定了哪个中断先被处理。

优先级高的中断会先得到响应,处理完后再处理优先级低的中断。

当一个中断发生时,单片机可不是手忙脚乱地随便处理。

它有着一套严格的中断响应流程。

首先,单片机在执行主程序时,会不断检测是否有中断请求。

一旦检测到有中断请求,并且中断是被允许的,单片机会暂停当前正在执行的主程序,把当前主程序的断点地址(也就是接下来要继续执行主程序的位置)保存起来。

这就像是在一张纸上记下当前做到哪一步了,等处理完中断回来还能接着做。

然后,单片机就会跳转到相应的中断服务程序去执行。

中断服务程序就像是专门处理紧急事务的“小分队”,有着特定的任务和处理逻辑。

51单片机中断系统详解

51单片机中断系统详解

51单片机中断系统详解51 单片机中断系统详解(定时器、计数器)51 单片机中断级别中断源INT0---外部中断0/P3.2 T0---定时器/计数器0 中断/P3.4 INT1---外部中断1/P3.3 T1----定时器/计数器1 中断/P3.5 TX/RX---串行口中断T2---定时器/计数器 2 中断第5 最低4 5 默认中断级别最高第2 第3 第4 序号(C 语言用) 0 1 2 3 intrrupt 0中断允许寄存器IE位序号符号位EA/0 ------ET2/1 ES ET1 EX1 ET0 EX0 DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 EA---全局中允许位。

EA=1,打开全局中断控制,在此条件下,由各个中断控制位确定相应中断的打开或关闭。

EA=0,关闭全部中断。

-------,无效位。

ET2---定时器/计数器2 中断允许位。

ET2=1, 打开T2 中断。

ET2=0,关闭T2 中断。

关,。

ES---串行口中断允许位。

关,。

ES=1,打开串行口中断。

关,。

ES=0,关闭串行口中断。

关,。

ET1---定时器/计数器1 中断允许位。

关,。

ET1=1,打开T1 中断。

ET1=0,关闭T1 中断。

EX1---外部中断1 中断允许位。

EX1=1,打开外部中断1 中断。

EX1=0,关闭外部中断1 中断。

ET0---定时器/计数器0 中断允许位。

ET0=1,打开T0 中断。

EA 总中断开关,置1 为开;EX0 为外部中断0 (INT0) 开关,。

ET0 为定时器/计数器0(T0)开EX1 为外部中断1(INT1)开ET1 为定时器/计数器1(T1)开ES 为串行口(TX/RX)中断开ET2 为定时器/计数器2(T2)开ET0=0,关闭T0 中断。

EX0---外部中断0 中断允许位。

EX0=1,打开外部中断0 中断。

EX0=0,关闭外部中断0 中断。

中断优先级寄存器IP位序号位地址------PS/0 PT1/0 PX1/0 PT0/0 PX0/0 DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 -------,无效位。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

51单片机中断总结:1. 查询优先级为固定的(外部中断0>定时器0>外部中断1>定时器1>串行中断)。

2. 执行优先级可以通过IP寄存器进行设置(高/低)。

3. CPU同时收到多个中断请求时,首先响应优先级较高者,然后相应优先级较低者;如果优先级相同,则按照查询优先级顺序依次响应。

4. 正在执行的中断服务,不能被同级或更低级的中断请求打断,但会被更高级的中断请求打断。

推论(1)高优先级的中断不能被任何其它中断所打断(2)低优先级的中断只能在没有任何中断服务运行时得到响应。

5. 对于定时器和外部中断,在进入中断服务后,其中断标志位会自动清零;对于串行中断,由于有两个中断源,需要手动查询并清零RI或/和TI。

if (RI) {// processingRI = 0;}if (TI) {// processingTI = 0;}6. 如果是使用汇编写中断服务,需要保护累加器、状态寄存器、寄存器组等8051 Tutorial: Interrupts/tutint.phpAs the name implies, an interrupt is some event which interrupts normal program execution.As stated earlier, program flow is always sequential, being altered only by those instructions which expressly cause program flow to deviate in some way. However, interrupts give us a mechanism to "put on hold" the normal program flow, execute a subroutine, and then resume normal program flow as if we had never left it. This subroutine, called an interrupt handler, is only executed when a certain event (interrupt) occurs. The event may be one of the timers "overflowing," receiving a character via the serial port, transmitting a character via the serialport, or one of two "external events." The 8051 may be configured so that when any of these events occur the main program is temporarily suspended and control passed to a special section of code which presumably would execute some function related to the event that occured. Once complete, control would be returned to the original program. The main program never even knows it was interrupted.The ability to interrupt normal program execution when certain events occur makes it much easier and much more efficient to handle certain conditions. If it were not for interrupts we would have to manually check in our main program whether the timers had overflown, whether we had received another character via the serial port, or if some external event had occured. Besides making the main program ugly and hard to read, such a situation would make our program inefficient since wed be burning precious "instruction cycles" checking for events that usually dont happen.For example, lets say we have a large 16k program executing many subroutines performing many tasks. Lets also suppose that we want our program to automatically toggle the P3.0 port every time timer 0 overflows. The code to do this isnt too difficult:JNB TF0,SKIP_TOGGLECPL P3.0CLR TF0SKIP_TOGGLE: ...Since the TF0 flag is set whenever timer 0 overflows, the above code will toggle P3.0 every time timer 0 overflows. This accomplishes what we want, but is inefficient. The JNB instruction consumes 2 instruction cycles to determine that the flag is not set and jump over the unnecessary code. In the event that timer 0 overflows, the CPL and CLR instruction require 2 instruction cycles to execute. To make the math easy, lets say the rest of the code in the program requires 98 instruction cycles. Thus, in total, our code consumes 100 instruction cycles (98 instruction cycles plus the 2 that are executed every iteration to determine whether or not timer 0 has overflowed). If were in 16-bit timer mode, timer 0 will overflow every 65,536 machine cycles. In that time we would have performed 655 JNB tests for a total of 1310 instruction cycles, plus another 2 instruction cycles to perform the code. So to achieve our goal weve spent 1312 instruction cycles. So 2.002% of our time is being spent just checking when to toggle P3.0. And our code is ugly because we have to make that check every iteration of our main program loop.Luckily, this isnt necessary. Interrupts let us forget about checking for the condition. The microcontroller itself will check for the condition automatically and when the condition is met will jump to a subroutine (called an interrupt handler), execute the code, then return. In this case, our subroutine would be nothing more than:CPL P3.0RETIFirst, youll notice the CLR TF0 command has disappeared. Thats because when the 8051 executes our "timer 0 interrupt routine," it automatically clears the TF0 flag. Youll also notice that instead of a normal RET instruction we have a RETI instruction. The RETI instruction does the same thing as a RET instruction, but tells the 8051 that an interrupt routine has finished. You must always end your interrupt handlers with RETI.Thus, every 65536 instruction cycles we execute the CPL instruction and the RETI instruction. Those two instructions together require 3 instruction cycles, and weve accomplished the same goal as the first example that required 1312 instruction cycles. As far as the toggling of P3.0 goes, our code is 437 times more efficient! Not to mention its much easier to read and understand because we dont have to remember to always check for the timer 0 flag in our main program. We just setup the interrupt and forget about it, secure in the knowledge that the 8051 will execute our code whenever its necessary.The same idea applies to receiving data via the serial port. One way to do it is to continuously check the status of the RI flag in an endless loop. Or we could check the RI flag as part of a larger program loop. However, in the latter case we run the risk of missing characters--what happens if a character is received right after we do the check, the rest of our program executes, and before we even check RI a second character has come in. We will lose the first character. With interrupts, the 8051 will put the main program "on hold" and call our special routine to handle the reception of a character. Thus, we neither have to put an ugly check in our main code nor will we lose characters.What Events Can Trigger Interrupts, and where do they go?We can configure the 8051 so that any of the following events will cause an interrupt:Timer 0 Overflow.Timer 1 Overflow.Reception/Transmission of Serial Character.External Event 0.External Event 1.In other words, we can configure the 8051 so that when Timer 0 Overflows or when a character is sent/received, the appropriate interrupt handler routines are called.Obviously we need to be able to distinguish between various interrupts and executing different code depending on what interrupt was triggered. This is accomplished by jumping to a fixed address when a given interrupt occurs.Interrupt Flag Interrupt Handler AddressExternal 0 IE0 0003hTimer 0 TF0 000BhExternal 1 IE1 0013hTimer 1 TF1 001BhSerial RI/TI 0023hBy consulting the above chart we see that whenever Timer 0 overflows (i.e., the TF0 bit is set), the main program will be temporarily suspended and control will jump to 000BH. It is assumed that we have code at address 000BH that handles the situation of Timer 0 overflowing.Setting Up InterruptsBy default at powerup, all interrupts are disabled. This means that even if, for example, the TF0 bit is set, the 8051 will not execute the interrupt. Your program must specifically tell the 8051 that it wishes to enable interrupts and specifically which interrupts it wishes to enable.Your program may enable and disable interrupts by modifying the IE SFR (A8h):Bit Name Bit Address Explanation of Function7 EA AFh Global Interrupt Enable/Disable6 - AEh Undefined5 - ADh Undefined4 ES ACh Enable Serial Interrupt3 ET1 ABh Enable Timer 1 Interrupt2 EX1 AAh Enable External 1 Interrupt1 ET0 A9h Enable Timer 0 Interrupt0 EX0 A8h Enable External 0 InterruptAs you can see, each of the 8051s interrupts has its own bit in the IE SFR. You enable a given interrupt by setting the corresponding bit. For example, if you wish to enable Timer 1 Interrupt, you would execute either:MOV IE,#08horSETB ET1Both of the above instructions set bit 3 of IE, thus enabling Timer 1 Interrupt. Once Timer 1 Interrupt is enabled, whenever the TF1 bit is set, the 8051 will automatically put "on hold" the main program and execute the Timer 1 Interrupt Handler at address 001Bh.However, before Timer 1 Interrupt (or any other interrupt) is truly enabled, you must also set bit 7 of IE. Bit 7, the Global Interupt Enable/Disable, enables or disables all interrupts simultaneously. That is to say, if bit 7 is cleared then no interrupts will occur, even if all the other bits of IE are set. Setting bit 7 will enable all the interrupts that have been selected by setting other bits in IE. This is useful in program execution if you have time-critical code that needs to execute. In this case, you may need the code to execute from start to finish without any interrupt getting in the way. To accomplish this you can simply clear bit 7 of IE (CLR EA) and then set it after your time-criticial code is done.So, to sum up what has been stated in this section, to enable the Timer 1 Interrupt the most common approach is to execute the following two instructions:SETB ET1SETB EAThereafter, the Timer 1 Interrupt Handler at 01Bh will automatically be called whenever the TF1 bit is set (upon Timer 1 overflow).Polling SequenceThe 8051 automatically evaluates whether an interrupt should occur after every instruction. When checking for interrupt conditions, it checks them in the following order:External 0 InterruptTimer 0 InterruptExternal 1 InterruptTimer 1 InterruptSerial InterruptThis means that if a Serial Interrupt occurs at the exact same instant that an External 0 Interrupt occurs, the External 0 Interrupt will be executed first and the Serial Interrupt will be executed once the External 0 Interrupt has completed.Interrupt PrioritiesThe 8051 offers two levels of interrupt priority: high and low. By using interrupt priorities you may assign higher priority to certain interrupt conditions.For example, you may have enabled Timer 1 Interrupt which is automatically called every time Timer 1 overflows. Additionally, you may have enabled the Serial Interrupt which is called every time a character is received via the serial port. However, you may consider that receiving a character is much more important than the timer interrupt. In this case, if Timer 1 Interrupt is already executing you may wish that the serial interrupt itself interrupts the Timer 1 Interrupt. When the serial interrupt is complete, control passes back to Timer 1 Interrupt and finally back to the main program. You may accomplish this by assigning a high priority to the Serial Interrupt and a low priority to the Timer 1 Interrupt.Interrupt priorities are controlled by the IP SFR (B8h). The IP SFR has the following format:Bit Name Bit Address Explanation of Function7 - - Undefined6 - - Undefined5 - - Undefined4 PS BCh Serial Interrupt Priority3 PT1 BBh Timer 1 Interrupt Priority2 PX1 BAh External 1 Interrupt Priority1 PT0 B9h Timer 0 Interrupt Priority0 PX0 B8h External 0 Interrupt PriorityWhen considering interrupt priorities, the following rules apply:Nothing can interrupt a high-priority interrupt--not even another high priority interrupt.A high-priority interrupt may interrupt a low-priority interrupt.A low-priority interrupt may only occur if no other interrupt is already executing.If two interrupts occur at the same time, the interrupt with higher priority will execute first. If both interrupts are of the same priority the interrupt which is serviced first by polling sequence will be executed first.What Happens When an Interrupt Occurs?When an interrupt is triggered, the following actions are taken automatically by the microcontroller:The current Program Counter is saved on the stack, low-byte first.Interrupts of the same and lower priority are blocked.In the case of Timer and External interrupts, the corresponding interrupt flag is cleared.Program execution transfers to the corresponding interrupt handler vector address.The Interrupt Handler Routine executes.Take special note of the third step: If the interrupt being handled is a Timer or External interrupt, the microcontroller automatically clears the interrupt flag before passing control to your interrupt handler routine. This means it is not necessary that you clear the bit in your code.What Happens When an Interrupt Ends?An interrupt ends when your program executes the RETI (Return from Interrupt) instruction. When the RETI instruction is executed the following actions are taken by the microcontroller:Two bytes are popped off the stack into the Program Counter to restore normal program execution.Interrupt status is restored to its pre-interrupt status.Serial InterruptsSerial Interrupts are slightly different than the rest of the interrupts. This is due to the fact that there are two interrupt flags: RI and TI. If either flag is set, a serial interrupt is triggered. As you will recall from the section on the serial port, the RI bit is set when a byte is received by the serial port and the TI bit is set when a byte has been sent.This means that when your serial interrupt is executed, it may have been triggered because the RI flag was set or because the TI flag was set--or because both flags were set. Thus, your routine must check the status of these flags to determine what action is appropriate. Also, since the 8051 does not automatically clear the RI and TI flags you must clear these bits in your interrupt handler.A brief code example is in order:INT_SERIAL: JNB RI,CHECK_TI ;If the RI flag is not set, we jump to check TIMOV A,SBUF ;If we got to this line, its because the RI bit *was* setCLR RI ;Clear the RI bit after weve processed itCHECK_TI: JNB TI,EXIT_INT ;If the TI flag is not set, we jump to the exit pointCLR TI ;Clear the TI bit before we send another characterMOV SBUF,#A ;Send another character to the serial portEXIT_INT: RETIAs you can see, our code checks the status of both interrupts flags. If both flags were set, both sections of code will be executed. Also note that each section of code clears its corresponding interrupt flag. If you forget to clear the interrupt bits, the serial interrupt will be executed over and over until you clear the bit. Thus it is very important that you always clear the interrupt flags in a serial interrupt.Important Interrupt Consideration: Register ProtectionOne very important rule applies to all interrupt handlers: Interrupts must leave the processor in the same state as it was in when the interrupt initiated.Remember, the idea behind interrupts is that the main program isnt aware that they are executing in the "background." However, consider the following code:CLR C ;Clear carryMOV A,#25h ;Load the accumulator with 25hADDC A,#10h ;Add 10h, with carryAfter the above three instructions are executed, the accumulator will contain a value of 35h.But what would happen if right after the MOV instruction an interrupt occured. During this interrupt, the carry bit was set and the value of the accumulator was changed to 40h. When the interrupt finished and control was passed back to the main program, the ADDC would add 10h to 40h, and additionally add an additional 1h because the carry bit is set. In this case, the accumulator will contain the value 51h at the end of execution.In this case, the main program has seemingly calculated the wrong answer. How can 25h + 10h yield 51h as a result? It doesnt make sense. A programmer that was unfamiliar with interrupts would be convinced that the microcontroller was damaged in some way, provoking problems with mathematical calculations.What has happened, in reality, is the interrupt did not protect the registers it used. Restated: An interrupt must leave the processor in the same state as it was in when the interrupt initiated.What does this mean? It means if your interrupt uses the accumulator, it must insure that the value of the accumulator is the same at the end of the interrupt as it was at the beginning. This is generally accomplished with a PUSH and POP sequence. For example:PUSH ACCPUSH PSWMOV A,#0FFhADD A,#02hPOP PSWPOP ACCThe guts of the interrupt is the MOV instruction and the ADD instruction. However, these two instructions modify the Accumulator (the MOV instruction) and also modify the value of the carry bit (the ADD instruction will cause the carry bit to be set). Since an interrupt routine must guarantee that the registers remain unchanged by the routine, the routine pushes the original values onto the stack using the PUSH instruction. It is then free to use the registers it protected to its hearts content. Once the interrupt has finished its task, it pops the original values back into the registers. When the interrupt exits, the main program will never know the difference because the registers are exactly the same as they were before the interrupt executed.In general, your interrupt routine must protect the following registers:PSWDPTR (DPH/DPL)PSWACCBRegisters R0-R7Remember that PSW consists of many individual bits that are set by various 8051 instructions. Unless you are absolutely sure of what you are doing and have a complete understanding of what instructions set what bits, it is generally a good idea to always protect PSW by pushing and popping it off the stack at the beginning and end of your interrupts.Note also that most assemblers (in fact, ALL assemblers that I know of) will not allow you to execute the instruction:PUSH R0This is due to the fact that depending on which register bank is selected, R0 may refer to either internal ram address 00h, 08h, 10h, or 18h. R0, in and of itself, is not a valid memory address that the PUSH and POP instructions can use.Thus, if you are using any "R" register in your interrupt routine, you will have to push that registers absolute address onto the stack instead of just saying PUSH R0. For example, instead of PUSH R0 you would execute:PUSH 00hOf course, this only works if youve selected the default register set. If you are using an alternate register set, you must PUSH the address which corresponds to the register you are using.Common Problems with InterruptsInterrupts are a very powerful tool available to the 8051 developer, but when used incorrectly they can be a source of a huge number of debugging hours. Errors in interrupt routines are often very difficult to diagnose and correct.If you are using interrupts and your program is crashing or does not seem to be performing as you would expect, always review the following interrupt-related issues:Register Protection: Make sure you are protecting all your registers, as explained above. If you forget to protect a register that your main program is using, very strange results may occur. In our example above we saw how failure to protect registers caused the main program to apparently calculate that 25h + 10h = 51h. If you witness problems with registers changing values unexpectedly or operations producing "incorrect" values, it is very likely that you've forgotten to protect registers. ALWAYS PROTECT YOUR REGISTERS.Forgetting to restore protected values: Another common error is to push registers onto the stack to protect them, and then forget to pop them off the stack before exiting the interrupt. For example, you may push ACC, B, and PSW onto the stack in order to protect them and subsequently pop only ACC and PSW off the stack before exiting. In this case, since you forgot to restore the value of "B", an extra value remains on the stack. When you execute the RETI instruction the 8051 will use that value as the return address instead of the correct value. In this case, your program will almost certainly crash. ALWAYS MAKE SURE YOU POP THE SAME NUMBER OF VALUES OFF THE STACK AS YOU PUSHED ONTO IT.Using RET instead of RETI: Remember that interrupts are always terminated with the RETI instruction. It is easy to inadvertantly use the RET instruction instead. However, the RETinstruction will not end your interrupt. Usually, using a RET instead of a RETI will cause the illusion of your main program running normally, but your interrupt will only be executed once. If it appears that your interrupt mysteriously stops executing, verify that you are exiting with RETI.11。

相关文档
最新文档