?? dpjjx27.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>
</HEAD>
<BODY background="../back.jpg">
<FONT FACE="宋體" LANG="ZH-CN" SIZE=2>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">鍵盤接口與編程</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">鍵盤是由若干按鍵組成的開關矩陣,它是微型計算機最常用的輸入設備,用戶可以通過鍵盤向計算機輸入指令、地址和數(shù)據(jù)。一般單片機系統(tǒng)中采和非編碼鍵盤,非編碼鍵盤是由軟件來識別鍵盤上的閉合鍵,它具有結構簡單,使用靈活等特點,因此被廣泛應用于單片機系統(tǒng)。</P>
</FONT>
<OL>
<FONT FACE="宋體" LANG="ZH-CN" SIZE=3>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
</font>
<FONT FACE="宋體" LANG="ZH-CN" SIZE=2>
<LI>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">按鍵開關的抖動問題</LI>
</font>
</p>
<p ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT FACE="宋體" LANG="ZH-CN" SIZE=2>組成鍵盤的按鍵有觸點式和非觸點式兩種,單片機中應用的一般是由機械觸點構成的。在下圖中,當開</font></p>
<table width="300" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font face="宋體" lang="ZH-CN" size=2><img src="dpj271.gif" width=93 height=60 align="baseline"></font></p>
<p align="center" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">圖1</font></p>
</td>
<td>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font face="宋體" lang="ZH-CN" size=2><img src="dpj272.gif" width="213" height="66"></font></p>
<p align="center" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font face="宋體" size="2">圖2</font></p>
</td>
</tr>
</table>
<p ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT FACE="宋體" LANG="ZH-CN" SIZE=2>關</font><FONT SIZE=2>S</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>未被按下時,</FONT><FONT SIZE=2>P1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>。</FONT><FONT SIZE=2>0</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>輸入為高電平,</FONT><FONT SIZE=2>S</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>閉合后,</FONT><FONT SIZE=2>P1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>。</FONT><FONT SIZE=2>0</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>輸入為低電平。由于按鍵是機械觸點,當機械觸點斷開、閉合時,會有抖動動,</FONT><FONT SIZE=2>P1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>。</FONT><FONT SIZE=2>0</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>輸入端的波形如圖</FONT><FONT SIZE=2>2</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>所示。這種抖動對于人來說是感覺不到的,但對計算機來說,則是完全可以感應到的,因為計算機處理的速度是在微秒級,而機械抖動的時間至少是毫秒級,對計算機而言,這已是一個“漫長”的時間了。前面我們講到中斷時曾有個問題,就是說按鍵有時靈,有時不靈,其實就是這個原因,你只按了一次按鍵,可是計算機卻已執(zhí)行了多次中斷的過程,如果執(zhí)行的次數(shù)正好是奇數(shù)次,那么結果正如你所料,如果執(zhí)行的次數(shù)是偶數(shù)次,那就不對了。</font></p>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT FACE="宋體" LANG="ZH-CN" SIZE=2>為使</font><FONT SIZE=2>CPU</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>能正確地讀出</FONT><FONT SIZE=2>P1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>口的狀態(tài),對每一次按鍵只作一次響應,就必須考慮如何去除抖動,常用的去抖動的方法有兩種:硬件方法和軟件方法。單片機中常用軟件法,因此,對于硬件方法我們不介紹。軟件法其實很簡單,就是在單片機獲得</FONT><FONT SIZE=2>P1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>。</FONT><FONT SIZE=2>0</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>口為低的信息后,不是立即認定</FONT><FONT SIZE=2>S1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>已被按下,而是延時</FONT><FONT SIZE=2>10</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>毫秒或更長一些時間后再次檢測</FONT><FONT SIZE=2>P1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>。</FONT><FONT SIZE=2>0</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>口,如果仍為低,說明</FONT><FONT SIZE=2>S1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>的確按下了,這實際上是避開了按鍵按下時的抖動時間。而在檢測到按鍵釋放后(</FONT><FONT SIZE=2>P1</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>。</FONT><FONT SIZE=2>0</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>為高)再延時</FONT><FONT SIZE=2>5-10</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>個毫秒,消除后沿的抖動,然后再對鍵值處理。不過一般情況下,我們通常不對按鍵釋放的后沿進行處理,實踐證明,也能滿足一定的要求。當然,實際應用中,對按鍵的要求也是千差萬別,要根據(jù)不同的需要來編制處理程序,但以上是消除鍵抖動的原則。</font></P>
<FONT FACE="宋體" LANG="ZH-CN" SIZE=3>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
</font>
<FONT FACE="宋體" LANG="ZH-CN" SIZE=2>
<LI>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">鍵盤與單片機的連接 </p>
</font>
<FONT FACE="宋體" LANG="ZH-CN" SIZE=3>
<table width="552" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="187">
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font face="宋體" lang="ZH-CN" size=2><img src="dpj273.gif" width="179" height="246"></font></p>
<p align="center" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">圖3</font></p>
</td>
<td width="351">
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2"><img src="DPJ274.gif" width="287" height="181"></font></p>
<p align="center" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<font size="2">圖4</font></p>
</td>
</tr>
</table>
</LI>
</font>
</OL>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0"><FONT FACE="宋體" LANG="ZH-CN" SIZE=3>
<font face="宋體" lang="ZH-CN" size=2>1、通過</font><font size=2>1/0</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>口連接。將每個按鍵的一端接到單片機的</FONT><FONT SIZE=2>I/O</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>口,另一端接地,這是最簡單的方法,如圖</FONT><FONT SIZE=2>3</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>所示是實驗板上按鍵的接法,四個按鍵分別接到</FONT><FONT SIZE=2>P3.2
</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>、</FONT><FONT SIZE=2>P3.3</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>、</FONT><FONT SIZE=2>P3.4</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>和</FONT><FONT SIZE=2>P3.5</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>。對于這種鍵各程序可以采用不斷查詢的方法,功能就是:檢測是否有鍵閉合,如有鍵閉合,則去除鍵抖動,判斷鍵號并轉入相應的鍵處理。下面給出一個例程。其功能很簡單,四個鍵定義如下:</font></font><font size="2">
</font>
</p>
<OL>
<FONT FACE="宋體" LANG="ZH-CN" SIZE=3>
<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>P3.2</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>:開始,按此鍵則燈開始流動(由上而下)</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>P3.3</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>:停止,按此鍵則停止流動,所有燈為暗</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>P3.4</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>:上,按此鍵則燈由上向下流動</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>P3.5</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>:下,按此鍵則燈由下向上流動</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>UpDown 	EQU	00H	;</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>上下行標志</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>StartEnd	EQU 	01H	;</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>起動及停止標志</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>LAMPCODE	EQU	21H	;</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>存放流動的數(shù)據(jù)代碼</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>ORG	0000H</font></P><FONT SIZE=2>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">	AJMP	MAIN</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">	ORG	30H</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">MAIN:</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">	MOV	SP,#5FH</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">	MOV	P1,#0FFH</P>
</FONT>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>	CLR	UpDown		;</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>啟動時處于向上的狀態(tài)</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>CLR	StartEnd	;</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>啟動時處于停止狀態(tài)</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>MOV	LAMPCODE,#0FEH	;</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>單燈流動的代碼	</font></P>
<FONT SIZE=2>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">LOOP:</P>
</FONT>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>	ACALL	KEY		;</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>調用鍵盤程序</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>JNB	F0,LNEXT	;</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>如果無鍵按下,則繼續(xù)</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>ACALL	KEYPROC		;</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>否則調用鍵盤處理程序</font></P>
<FONT SIZE=2>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">LNEXT:</P>
</FONT>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>	ACALL	LAMP		;</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>調用燈顯示程序</font></P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>AJMP	LOOP		;</FONT><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>反復循環(huán),主程序到此結束</font></P>
<FONT SIZE=2>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">;---------------------------------------</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">DELAY:</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">	MOV	R7,#100</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">D1:	MOV	R6,#100</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">	DJNZ	R6,$</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">	DJNZ	R7,D1</P>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">	RET</P>
</FONT>
<P ALIGN="JUSTIFY" style="line-height: 150%; margin-top: 0; margin-bottom: 0">
<FONT SIZE=2>;----------------------------------------</font><FONT FACE="宋體" LANG="ZH-CN" SIZE=2>延時程序,鍵盤處理中調用</font></P>
<FONT SIZE=2>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -