實習九              點矩陣鍵盤練習   

目的    :   
   
                 瞭解鍵盤掃描原理 

 

使用材料    :    
                                PC    *    1         8051模擬板    *    1

功能說明    :    
                      程式執行後按4*4鍵盤的任意鍵(0~F),則七段顯示器顯示該鍵代表的值(0~F)。

 

程式流程圖    :

電路圖    :

程式及說明    :

;****************************************************************

                                    .RSECT                                     ;define register section

                                    .ABSOLUTE                             ;absolute RAM address

                                    .ORG    20H                              ;offset address = 20h

BUFFER                      .DS       1                                   ;buffer for store key code

BUFFER1                    .DS       1                                   ;same as buffer,can change

STACK_BOTTOM    EQU      $                                    ;stack bottom pointer

;=============================================================

                      .CODE                                                      ;program code

                      .RELATIVE                                              ;relative address

                    ORG     000H                                             ;reset offset address

COLUMN0:                                                                   ;scan first column keys

                    MOV     A,#01111111B                            ;set column voltage = 0 v

NEXT_COL:                                                                 ;

                    MOV     P3,A                                            ;output scan code

                    MOV     BUFFER,P3                                ;read keyin code into buffer

                    MOV     BUFFER1,P3                              ;keyin code save to buffer1

                    ANL     BUFFER1,#00001111B               ;set high 4 bits = 0

                     XRL     BUFFER1,#00001111B               ;inverse low 4 bits

                    MOV     R0,BUFFER1                              ;load R0 with last code

                    CJNE    R0,#0,KEYIN                              ;if code <> 0,a key be hit

                    RR      A                                                    ;if code=0,rotate A with right

                    CJNE    A,#11110111B,NEXT_COL       ;not scan over,scan next

                     JMP     COLUMN0                                  ;scan 4 columns,rescan column0

                     KEYIN:                                                     ;any key be hit,jmp here

                     MOV     R4,#0                                          ;load R4 with 0(index number)

                     CHECK:  CALL    CHECK_CODE         ;get check_code of key,-> A

                     CJNE    A,BUFFER,NEXT_CHECK       ;A <> buffer,jmp to next

                     CALL    DIS_CODE                                 ;A = buffer,get display code

                     MOV     P1,A                                            ;display number

                     JMP     COLUMN0                                   ;re-scan keyboard's column0

NEXT_CHECK:                                                             ;

                     INC     R4                                                  ;increment index number

                     CJNE    R4,#16,CHECK                           ;check all code ? if no,next

                     JMP     COLUMN0                                   ;if yes,re-sacn.

;------------------------------------------------------

;    input : R4 = number 0-15                         |

;   output :  A = 7_segment display code of 0-15      |

;------------------------------------------------------

DIS_CODE:                                                                    ;get 7_seg display code

                      MOV     A,R4                                          ;load A with index number

                      INC     A                                                  ;A = A + 1 = R4 + 1

                      MOVC    A,@A+PC                               ;get display code

                      RET                                                         ;return,(1 byte instruction)

                      DB      10000001B,11001111B,10010010B,10000110B ;0-3

                      DB      11001100B,10100100B,10100000B,10001111B ;4-7

                      DB      10000000B,10000100B,10001000B,11100000B ;8-c

                      DB      10110001B,11000010B,10110000B,10111000B ;d-f

;------------------------------------------------------

;    input : R4 = number 0-15                         |

;   output :  A = check code of key0 - key15          |

;------------------------------------------------------

CHECK_CODE:                                                              ;get check code = scan code + return code

                       MOV     A,R4                                           ;load A with index number

                       INC     A                                                   ;A = A + 1 = R4 + 1

                       MOVC    A,@A+PC                                ;get display code

                       RET                                                           ;return,(1 byte instruction)

                       DB      01111110B,01111101B,01111011B,01110111B ;column0

                       DB      10111110B,10111101B,10111011B,10110111B ;column1

                       DB      11011110B,11011101B,11011011B,11010111B ;column2

                       DB      11101110B,11101101B,11101011B,11100111B ;column3

                       END                                                          ;end of program

1.  4~7行 : 設變數 Buffer 定只在 8051 內部 RAM 20H 位址,儲存按鍵碼。因鍵碼比較會破壞其值 ,故複製

                   同  值到 Buffer 中。

2.  第8行 : 推疊指標值定義。

3.  10~11行 : 記得用過 ABSOLUTE 虛擬指令後,程式段中要再宣告 RELATIVE ,組譯才不會產生錯誤碼。

4.  14~25行 : 輸出鍵盤掃描碼,讀入返回碼;若沒有鍵被按下,則繼續掃描鍵盤;若有鍵被案,則跳到    

                       KEYIN 去比較之。

5.  26~32行 : 將鍵碼與鍵盤檢查碼比對,先比第 "0" Key;若相等則表示鍵入 " 0" ,就輸出顯示碼到七段顯示

                       器上。不等則再比下一筆。

6.  33~36行 : 繼續比完 16 次,之後就回去重頭掃描續鍵。

7.  37~49行 : 取得相對應七段顯示器碼之副程式。"0" 使該段亮。

8.  50~62行 : 取得相對應鍵盤檢查碼,共 16 個鍵,對應 16 個bytes,如按了第 0 列第 0 行,其碼為 

                       01111110B,比處將 16個標準碼列出,以程式中用來查對是否相符。