×
嵌入式 > 技术百科 > 详情

合泰单片机矩阵键盘程序

发布时间:2024-10-15 发布时间:
|

合泰HOLTEK 单片机 写的,对初学者来说里面的分析思路很有帮助。 
#include ht48c10-1.inc 
;------------------------------------------------------- 
data .section ’data’            ;== data section ==


temp    db      ?               ;temporary data register 
disp    db      ?               ;key display register 
count1  db      ?               ;delay loop counter 
mask    db      ?               ;mask register 
matrix  db      ?               ;key matrix register 
;------------------------------------------------------- 
code .section at 0 ’code’       ;== program section == 
        org     00h             ; 
        jmp     start           ; 
        org     04h             ;external interrupt subroutine 
        reti                    ;for guarantee 
        org     08h             ;timer/event 0 interrupt subroutine 
        reti                    ;for guarantee 
        org     0ch             ;timer/event 1 interrupt subroutine 
        reti                    ;for guarantee 
         
start:                          ; 
        clr     iNTC            ;initialize registers 
        clr     tmrc            ;to guarantee performance 
        clr     tmr             ;(interrupts) 
        set     PAC             ;(ports) 
        set     pbc             ;(input mode) 
        set     pCC             ; 
main: 
        set     pac         ;(1);set port A to input mode 
        clr     pbc             ;set port B to output mode 
        clr     pa              ;zero port A (latch=0) 
        set     pb              ;off LEDs 
keyloop:                        ; 
         MOV      a,0feh      ;(2);sCAN first row of keys 
         MOV      matrix,a        ;hold scan code 
         MOV      pac,a           ;pa.0 output 0 (latch) 
         MOV      a,pa            ;read input state 
        cpl     acc             ;;distill input state 
        and     a,0f0h          ;; 
        sz      acc             ;if any input? 
        jmp     get_key         ;yes. get input info 
         MOV      a,0fdh      ;(2);no. scan second row 
         MOV      matrix,a        ;hold scan code 
         MOV      pac,a           ;pa.1 output 0 (latch) 
         MOV      a,pa            ;read input state 
        cpl     acc             ;;distill input state 
        and     a,0f0h          ;; 
        sz      acc             ;if any input? 
        jmp     get_key         ;yes. get input info 
         MOV      a,0fbh      ;(2);no. scan third row 
         MOV      matrix,a        ;hold scan code 
         MOV      pac,a           ;pa.2 output 0 (latch) 
         MOV      a,pa            ;read input state 
        cpl     acc             ;;distill input state 
        and     a,0f0h          ;; 
        sz      acc             ;if any input? 
        jmp     get_key         ;yes. get input info 
         MOV      a,0f7h      ;(2);no. scan fourth row 
         MOV      matrix,a        ;hold scan code 
         MOV      pac,a           ;output pa.3 0 (latch) 
         MOV      a,pa            ;read input state 
        cpl     acc             ;;distill input state 
        and     a,0f0h          ;; 
        sz      acc             ;if any input? 
        jmp     get_key         ;yes. get input info 
        jmp     keyloop         ;repeat from keyloop 
get_key:                        ;get input key info 
        call    delays          ;debounce 
         MOV      a,pa            ;test port A 
        or      a,0fh           ; 
        cpl     acc             ; 
        sz      acc             ;any key hold? 
        jmp     go_on           ;yes. go on (some key is pressed) 
        jmp     keyloop         ;no. return to scan key again 
go_on: 
        call    key_in     ;(3) ;calculate table index 
        tabrdl  disp       ;(10);load display data 
         MOV      a,disp          ;;output data to port B 
         MOV      pb,a       ;(11);; 
        jmp     keyloop         ;repeat keyloop 
key_in proc                     ;get key number 
         MOV      a,pa            ;;hold port A state 
         MOV      temp,a     ;(4) ;; 
   get_release:                 ;wait for the key to be released 
         MOV      a,pa            ;;test port A state 
        cpl     acc             ;; 
        and     a,0f0h          ;; 
        sz      acc        ;(6) ;if release? 
        jmp     get_release     ;no. keep up waiting 
         MOV      a,0fh           ;yes. calculate key number 
        andm    a,matrix   ;(7) ;mask low nibble of scan code 
         MOV      a,0             ;keep table index at register A 
   get_row:                     ;calculate row number 
        rrc     matrix          ;;check each bit to get row number 
        snz     status.0        ;; 
        jmp     get_next        ;if bingo goto get_next 
        clr     c               ; 
        add     a,4h       ;(8) ;table index +4 (4 keys a row) 
        jmp     get_row         ;continue calculating 
   get_next:                    ; 
         MOV      tblp,a          ;hold table index at register TBLP 
         MOV      a,0efh          ; 
         MOV      mask,a          ;; mask = 0111 1111 
         MOV      a,0fh           ; 
        orm     a,temp          ;; temp = XXXX 1111 
   get_column:                  ;calculate column number 
         MOV      a,temp          ;load temp 
        xor     a,mask          ;;test column number 
        snz     z               ;; 
        jmp     index           ;no. test next column 
        ret                     ;yes. return (TBLP) 
   index:                       ;next column 
        inc     tblp       ;(9) ;table index +1 
        set     c               ; 
        rlc     mask            ;shift mask left (LSB=1) 
        jmp     get_column      ;repeat get_column 
key_in endp 
delays proc                     ;delay subroutine 
         MOV      a,0ffh          ;load counter 
         MOV      count1,a        ; 
d1: 
        sdz     count1          ;count down count1 
        jmp     d1 
        ret 
delays  endp 



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
常用的AD和DA芯片汇总.选择困难症必看