?? dpjjx21.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="D:\MICROSOFT OFFICE\OFFICE\html.dot">
</HEAD>
<BODY LINK="#0000FF" VLINK="#800080" background="../back.jpg">
<FONT FACE="宋體" LANG="ZH-CN" size="2">
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">定時、中斷練習一</P>
</FONT><P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2" FACE="System" LANG="ZH-CN">1</font><font size="2" FACE="宋體" LANG="ZH-CN">、利用定時器實現燈的閃爍</font></P>
<FONT FACE="宋體" LANG="ZH-CN" size="2">
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">在學單片機時我們第一個例子就是燈的閃爍,那是用延時程序做的,現在回想起來,這樣做不很恰當,為什么呢?我們的主程序做了燈的閃爍,就不能再干其它的事了,難道單片機只能這樣工作嗎?當然不是,我們可以用定時器來實現燈的閃爍的功能。</P>
</FONT><P 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">1</FONT><font size="2" FACE="宋體" LANG="ZH-CN">:查詢方式</font></P>
<TABLE CELLSPACING=0 BORDER=1 CELLPADDING=7 WIDTH=638>
<TR><TD VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN">
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">ORG	0000H</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">AJMP	START</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">ORG	30H</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">START:</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	P1,#0FFH ;關所 燈</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TMOD,#00000001B ;定時/計數器0工作于方式1</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TH0,#15H	</font>	</P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TL0,#0A0H ;即數5536</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">SETB	TR0	 ;定時/計數器0開始運行</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">LOOP:JBC	TF0,NEXT ;如果TF0等于1,則清TF0并轉NEXT處</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">AJMP	LOOP ;否則跳轉到LOOP處運行</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">NEXT:CPL	P1.0</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TH0,#15H</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TL0,#9FH;重置定時/計數器的初值</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">AJMP	LOOP</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">END	AJMP	LOOP</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">END</font></P>
</FONT></TD>
</TR>
</TABLE>
<FONT FACE="宋體" LANG="ZH-CN">
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"> </P>
</FONT>
<P 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">JBC</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">/</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">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">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">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">JBC</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">0</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">。</font></P>
<P 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">LOOP</FONT><FONT FACE="宋體" LANG="ZH-CN">:</FONT><FONT FACE="System" LANG="ZH-CN">……</FONT><FONT FACE="宋體" LANG="ZH-CN">和</FONT><FONT FACE="System" LANG="ZH-CN">AJMP	LOOP</FONT><FONT FACE="宋體" LANG="ZH-CN">指令之間插入一些指令來做其他的事情,只要保證執行這些指令的時間少于定時時間就行了。那我們在用軟件延時程序的時候不是也可以用一些指令來替代</FONT><FONT FACE="System" LANG="ZH-CN">DJNZ</FONT><FONT FACE="宋體" LANG="ZH-CN">嗎?是的,但是那就要求你精確計算所用指令的時間,然后再減去相應的</FONT><FONT FACE="System" LANG="ZH-CN">DJNZ</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">循環次數,很不方便,而現在只要求所用指令的時間少于定時時間就行,顯然要求低了。當然,這樣的方法還是不好,所以我們常用以下的方法來實現。</font></P>
<P 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>
<TABLE CELLSPACING=0 BORDER=1 CELLPADDING=7 WIDTH=641>
<TR>
<TD VALIGN="TOP"> <FONT FACE="System" LANG="ZH-CN">
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">ORG	0000H</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">AJMP	START</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">ORG	000BH ;定時器0的中斷向量地址</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">AJMP	TIME0	;跳轉到真正的定時器程序處</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">ORG	30H</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">START:</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	P1,#0FFH ;關所 燈</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TMOD,#00000001B ;定時/計數器0工作于方式1</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TH0,#15H	</font>	</P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TL0,#0A0H ;即數5536</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">SETB	EA	;開總中斷允許</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">SETB	ET0	;開定時/計數器0允許</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">SETB	TR0	#9; ;定時/計數器0開始運行</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">LOOP:	AJMP	LOOP	;真正工作時,這里可寫任意程序</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">TIME0:			;定時器0的中斷處理程序</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">PUSH	ACC</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">PUSH	PSW	;將PSW和ACC推入堆棧保護</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">CPL	P1.0	</font>	</P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TH0,#15H</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">MOV	TL0,#0A0H	;重置定時常數</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">POP	PSW</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">POP	ACC</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">	
<font size="2">RETI</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">END</font></P>
</FONT></TD>
</TR>
</TABLE>
<FONT FACE="宋體" LANG="ZH-CN">
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"> </P>
</FONT><P 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">TF0</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">CPU</FONT><FONT FACE="宋體" LANG="ZH-CN">將自動轉至</FONT><FONT FACE="System" LANG="ZH-CN">000B</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">000B</FONT><FONT FACE="宋體" LANG="ZH-CN">處安排一條跳轉指令,轉到實際處理中斷的程序處,這樣,中斷程序可以寫在任意地方,也可以寫任意長度了。進入定時中斷后,首先要保存當前的一些狀態,程序中只演示了保存存</FONT><FONT FACE="System" LANG="ZH-CN">ACC</FONT><FONT FACE="宋體" LANG="ZH-CN">和</FONT><FONT FACE="System" LANG="ZH-CN">PSW</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">,實際工作中應該根據需要將可能會改變的單元的值都推入堆棧進行保護(本程序中實際不需保存護任何值,這里只作個演示)。</font></P>
<P 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">5536</FONT><FONT FACE="宋體" LANG="ZH-CN">,所以每計</FONT><FONT FACE="System" LANG="ZH-CN">60000</FONT><FONT FACE="宋體" LANG="ZH-CN">個脈沖就是定時時間到,這</FONT><FONT FACE="System" LANG="ZH-CN">60000</FONT><FONT FACE="宋體" LANG="ZH-CN">個脈沖的時間是多少呢?我們的晶振是</FONT><FONT FACE="System" LANG="ZH-CN">12M</FONT><FONT FACE="宋體" LANG="ZH-CN">,所以就是</FONT><FONT FACE="System" LANG="ZH-CN">60000</FONT><FONT FACE="宋體" LANG="ZH-CN">微秒,即</FONT><FONT FACE="System" LANG="ZH-CN">60</FONT><FONT FACE="宋體" LANG="ZH-CN">毫秒,因此速度是非常快的。如果我想實現一個</FONT><FONT FACE="System" LANG="ZH-CN">1S</FONT><FONT FACE="宋體" LANG="ZH-CN">的定時,該怎么辦呢?在該晶振瀕率下,最長的定時也就是</FONT><FONT FACE="System" LANG="ZH-CN">65</FONT><FONT FACE="宋體" LANG="ZH-CN">。</FONT><FONT FACE="System" LANG="ZH-CN">536</FONT></font><font size="2" FACE="宋體" LANG="ZH-CN">個毫秒啊!上面給出一個例子。</font></P>
<TABLE CELLSPACING=0 BORDER=1 CELLPADDING=7 WIDTH=631>
<TR><TD VALIGN="TOP">
<FONT FACE="System" LANG="ZH-CN">
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">ORG	0000H</font></P>
<P style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2">AJMP	START</font></P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -