?? dpjjx20.htm
字號:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb_2312-80">
<META NAME="Generator" CONTENT="Microsoft FrontPage 5.0">
<TITLE>單片機第二十課:中斷系統</TITLE>
<META NAME="Template" CONTENT="C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE\html.dot">
</HEAD>
<BODY LINK="#0000ff" VLINK="#800080" background="../back.jpg">
<FONT FACE="宋體" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">單片機第二十課:中斷系統</P>
</FONT><OL><FONT FACE="宋體" LANG="ZH-CN">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><LI>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">有關中斷的概念</LI></font></p>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0"></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">什么是中斷,我們從一個生活中的例子引入。你正在家中看書,突然電話鈴響了,你放下書本,去接電話,和來電話的人交談,然后放下電話,回來繼續(xù)看你的書。這就是生活中的“中斷”的現象,就是正常的工作過程被外部的事件打斷了。</font></P>
</font>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="宋體" LANG="ZH-CN">仔細研究一下生活中的中斷,對于我們學習單片機的中斷也很有好處。第一、什么可經引起中斷,生活中很多事件可以引起中斷:有人按了門鈴了,電話鈴響了,你的鬧鐘鬧響了,你燒的水開了</font><font size="2"><FONT FACE="System" LANG="ZH-CN">….</FONT><FONT FACE="宋體" LANG="ZH-CN">等等諸如此類的事件,我們把可以引起中斷的稱之為中斷源,單片機中也有一些可以引起中斷的事件,</FONT><FONT FACE="System" LANG="ZH-CN">8031</FONT><FONT FACE="宋體" LANG="ZH-CN">中一共有</FONT><FONT FACE="System" LANG="ZH-CN">5</FONT><FONT FACE="宋體" LANG="ZH-CN">個:兩個外部中斷,兩個計數</FONT><FONT FACE="System" LANG="ZH-CN">/</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">定時器中斷,一個串行口中斷。</font></P>
<FONT FACE="宋體" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">第二、中斷的嵌套與優(yōu)先級處理:設想一下,我們正在看書,電話鈴響了,同時又有人按了門鈴,你該先做那樣呢?如果你正是在等一個很重要的電話,你一般不會去理會門鈴的,而反之,你正在等一個重要的客人,則可能就不會去理會電話了。如果不是這兩者(即不等電話,也不是等人上門),你可能會按你通常的習慣去處理。總之這里存在一個優(yōu)先級的問題,單片機中也是如此,也有優(yōu)先級的問題。優(yōu)先級的問題不僅僅發(fā)生在兩個中斷同時產生的情況,也發(fā)生在一個中斷已產生,又有一個中斷產生的情況,比如你正接電話,有人按門鈴的情況,或你正開門與人交談,又有電話響了情況。考慮一下我們會怎么辦吧。</P>
</FONT>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="宋體" LANG="ZH-CN">第三、中斷的響應過程:當有事件產生,進入中斷之前我們必須先記住現在看書的第幾頁了,或拿一個書簽放在當前頁的位置,然后去處理不同的事情(因為處理完了,我們還要回來繼續(xù)看書):電話鈴響我們要到放電話的地方去,門鈴響我們要到門那邊去,也說是不同的中斷,我們要在不同的地點處理,而這個地點通常還是固定的。計算機中也是采用的這種方法,五個中斷源,每個中斷產生后都到一個固定的地方去找處理這個中斷的程序,當然在去之前首先要保存下面將執(zhí)行的指令的地址,以便處理完中斷后回到原來的地方繼續(xù)往下執(zhí)行程序。具體地說,中斷響應可以分為以下幾個步驟:</font><font size="2"><FONT FACE="System" LANG="ZH-CN">1</FONT><FONT FACE="宋體" LANG="ZH-CN">、保護斷點,即保存下一將要執(zhí)行的指令的地址,就是把這個地址送入堆棧。</FONT><FONT FACE="System" LANG="ZH-CN">2</FONT><FONT FACE="宋體" LANG="ZH-CN">、尋找中斷入口,根據</FONT><FONT FACE="System" LANG="ZH-CN">5</FONT><FONT FACE="宋體" LANG="ZH-CN">個不同的中斷源所產生的中斷,查找</FONT><FONT FACE="System" LANG="ZH-CN">5</FONT><FONT FACE="宋體" LANG="ZH-CN">個不同的入口地址。以上工作是由計算機自動完成的,與編程者無關。在這</FONT><FONT FACE="System" LANG="ZH-CN">5</FONT><FONT FACE="宋體" LANG="ZH-CN">個入口地址處存放有中斷處理程序(這是程序編寫時放在那兒的,如果沒把中斷程序放在那兒,就錯了,中斷程序就不能被執(zhí)行到)。</FONT><FONT FACE="System" LANG="ZH-CN">3</FONT><FONT FACE="宋體" LANG="ZH-CN">、執(zhí)行中斷處理程序。</FONT><FONT FACE="System" LANG="ZH-CN">4</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">、中斷返回:執(zhí)行完中斷指令后,就從中斷處返回到主程序,繼續(xù)執(zhí)行。</font></P>
<FONT FACE="宋體" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">究竟單片機是怎么樣找到中斷程序所在位置,又怎么返回的呢?我們稍后再談。</P>
</FONT><FONT FACE="System" LANG="ZH-CN">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0"></FONT><LI>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">MCS-51</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">中斷系統的結構:</font></LI></font><FONT FACE="宋體" LANG="ZH-CN"></p>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0"></P></font></OL>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="宋體" LANG="ZH-CN">如圖(抱歉,本圖請找本</font><font size="2"><FONT FACE="System" LANG="ZH-CN">51</FONT><FONT FACE="宋體" LANG="ZH-CN">書看一下)所示,由與中斷有關的特殊功能寄存器、中斷入口、順序查詢邏輯電路等組成,包括</FONT><FONT FACE="System" LANG="ZH-CN">5</FONT><FONT FACE="宋體" LANG="ZH-CN">個中斷請求源,</FONT><FONT FACE="System" LANG="ZH-CN">4</FONT><FONT FACE="宋體" LANG="ZH-CN">個用于中斷控制的寄存器</FONT><FONT FACE="System" LANG="ZH-CN">IE</FONT><FONT FACE="宋體" LANG="ZH-CN">、</FONT><FONT FACE="System" LANG="ZH-CN">IP</FONT><FONT FACE="宋體" LANG="ZH-CN">、</FONT><FONT FACE="System" LANG="ZH-CN">ECON</FONT><FONT FACE="宋體" LANG="ZH-CN">和</FONT><FONT FACE="System" LANG="ZH-CN">SCON</FONT><FONT FACE="宋體" LANG="ZH-CN">來控制中斷</FONT>
</font><font size="2" FACE="宋體" LANG="ZH-CN">類弄、中斷的開、關和各種中斷源的優(yōu)先級確定。</font></P><FONT FACE="宋體" LANG="ZH-CN">
<OL>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><LI>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">中斷請求源:</LI></font></p>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0"></P></OL>
</FONT><DIR>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="宋體" LANG="ZH-CN">(</font><font size="2"><FONT FACE="System" LANG="ZH-CN">1</FONT><FONT FACE="宋體" LANG="ZH-CN">)外部中斷請求源:即外中斷</FONT><FONT FACE="System" LANG="ZH-CN">0</FONT><FONT FACE="宋體" LANG="ZH-CN">和</FONT><FONT FACE="System" LANG="ZH-CN">1</FONT><FONT FACE="宋體" LANG="ZH-CN">,經由外部引腳引入的,在單片機上有兩個引腳,名稱為</FONT><FONT FACE="System" LANG="ZH-CN">INT0</FONT><FONT FACE="宋體" LANG="ZH-CN">、</FONT><FONT FACE="System" LANG="ZH-CN">INT1</FONT><FONT FACE="宋體" LANG="ZH-CN">,也就是</FONT><FONT FACE="System" LANG="ZH-CN">P3.2</FONT><FONT FACE="宋體" LANG="ZH-CN">、</FONT><FONT FACE="System" LANG="ZH-CN">P3.3</FONT><FONT FACE="宋體" LANG="ZH-CN">這兩個引腳。在內部的</FONT><FONT FACE="System" LANG="ZH-CN">TCON</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">中有四位是與外中斷有關的。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">IT0</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">:</FONT><FONT FACE="System" LANG="ZH-CN">INT0</FONT><FONT FACE="宋體" LANG="ZH-CN">觸發(fā)方式控制位,可由軟件進和置位和復位,</FONT><FONT FACE="System" LANG="ZH-CN">IT0=0</FONT><FONT FACE="宋體" LANG="ZH-CN">,</FONT><FONT FACE="System" LANG="ZH-CN">INT0</FONT><FONT FACE="宋體" LANG="ZH-CN">為低電平觸發(fā)方式,</FONT><FONT FACE="System" LANG="ZH-CN">IT0=1</FONT><FONT FACE="宋體" LANG="ZH-CN">,</FONT><FONT FACE="System" LANG="ZH-CN">INT0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">為負跳變觸發(fā)方式。這兩種方式的差異將在以后再談。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">IE0</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">:</FONT><FONT FACE="System" LANG="ZH-CN">INT0</FONT><FONT FACE="宋體" LANG="ZH-CN">中斷請求標志位。當有外部的中斷請求時,這位就會置</FONT><FONT FACE="System" LANG="ZH-CN">1</FONT><FONT FACE="宋體" LANG="ZH-CN">(這由硬件來完成),在</FONT><FONT FACE="System" LANG="ZH-CN">CPU</FONT><FONT FACE="宋體" LANG="ZH-CN">響應中斷后,由硬件將</FONT><FONT FACE="System" LANG="ZH-CN">IE0</FONT><FONT FACE="宋體" LANG="ZH-CN">清</FONT><FONT FACE="System" LANG="ZH-CN">0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">IT1</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">、</FONT><FONT FACE="System" LANG="ZH-CN">IE1</FONT><FONT FACE="宋體" LANG="ZH-CN">的用途和</FONT><FONT FACE="System" LANG="ZH-CN">IT0</FONT><FONT FACE="宋體" LANG="ZH-CN">、</FONT><FONT FACE="System" LANG="ZH-CN">IE0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">相同。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="宋體" LANG="ZH-CN">(</font><FONT FACE="System" LANG="ZH-CN" size="2">2</FONT><font size="2" FACE="宋體" LANG="ZH-CN">)內部中斷請求源</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">TF0</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">:定時器</FONT><FONT FACE="System" LANG="ZH-CN">T0</FONT><FONT FACE="宋體" LANG="ZH-CN">的溢出中斷標記,當</FONT><FONT FACE="System" LANG="ZH-CN">T0</FONT><FONT FACE="宋體" LANG="ZH-CN">計數產生溢出時,由硬件置位</FONT><FONT FACE="System" LANG="ZH-CN">TF0</FONT><FONT FACE="宋體" LANG="ZH-CN">。當</FONT><FONT FACE="System" LANG="ZH-CN">CPU</FONT><FONT FACE="宋體" LANG="ZH-CN">響應中斷后,再由硬件將</FONT><FONT FACE="System" LANG="ZH-CN">TF0</FONT><FONT FACE="宋體" LANG="ZH-CN">清</FONT><FONT FACE="System" LANG="ZH-CN">0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">TF1</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">:與</FONT><FONT FACE="System" LANG="ZH-CN">TF0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">類似。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">TI</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">、</FONT><FONT FACE="System" LANG="ZH-CN">RI</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">:串行口發(fā)送、接收中斷,在串口中再講解。</font></P></DIR>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">2</font><FONT FACE="宋體" LANG="ZH-CN" size="2">、中斷允許寄存器</FONT><font size="2" FACE="System" LANG="ZH-CN">IE</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="宋體" LANG="ZH-CN">在</font><font size="2"><FONT FACE="System" LANG="ZH-CN">MCS</FONT><FONT FACE="宋體" LANG="ZH-CN">-</FONT><FONT FACE="System" LANG="ZH-CN">51</FONT><FONT FACE="宋體" LANG="ZH-CN">中斷系統中,中斷的允許或禁止是由片內可進行位尋址的</FONT><FONT FACE="System" LANG="ZH-CN">8</FONT><FONT FACE="宋體" LANG="ZH-CN">位中斷允許寄存器</FONT><FONT FACE="System" LANG="ZH-CN">IE</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">來控制的。見下表</font></P>
<TABLE BORDER CELLSPACING=1 CELLPADDING=7 WIDTH=607>
<TR><TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">EA</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">X</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">X</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">ES</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">ET1</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">EX1</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">ET0</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">EX0</FONT></TD>
</TR>
</TABLE>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="宋體" LANG="ZH-CN">其中</font><font size="2"><FONT FACE="System" LANG="ZH-CN">EA</FONT><FONT FACE="宋體" LANG="ZH-CN">是總開關,如果它等于</FONT><FONT FACE="System" LANG="ZH-CN">0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">,則所有中斷都不允許。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">ES</font><font size="2" FACE="宋體" LANG="ZH-CN">-串行口中斷允許</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">ET1</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">-定時器</FONT><FONT FACE="System" LANG="ZH-CN">1</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">中斷允許</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">EX1</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">-外中斷</FONT><FONT FACE="System" LANG="ZH-CN">1</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">中斷允許。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">ET0</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">-定時器</FONT><FONT FACE="System" LANG="ZH-CN">0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">中斷允許</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">EX0</font><font size="2"><FONT FACE="宋體" LANG="ZH-CN">-外中斷</FONT><FONT FACE="System" LANG="ZH-CN">0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">中斷允許。</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="宋體" LANG="ZH-CN">如果我們要設置允許外中斷</font><font size="2"><FONT FACE="System" LANG="ZH-CN">1</FONT><FONT FACE="宋體" LANG="ZH-CN">,定時器</FONT><FONT FACE="System" LANG="ZH-CN">1</FONT><FONT FACE="宋體" LANG="ZH-CN">中斷允許,其它不允許,則</FONT><FONT FACE="System" LANG="ZH-CN">IE</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">可以是</font></P>
<TABLE BORDER CELLSPACING=1 CELLPADDING=7 WIDTH=607>
<TR><TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="CENTER" style="line-height: 150%; margin-top: 0; margin-bottom: 0">EA</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="CENTER" style="line-height: 150%; margin-top: 0; margin-bottom: 0">X</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="CENTER" style="line-height: 150%; margin-top: 0; margin-bottom: 0">X</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="CENTER" style="line-height: 150%; margin-top: 0; margin-bottom: 0">ES</FONT></TD>
<TD WIDTH="13%" VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN" size="2">
<P ALIGN="CENTER" style="line-height: 150%; margin-top: 0; margin-bottom: 0">ET1</FONT></TD>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -