您的位置:首页单片机合泰单片机
内容搜索:
阅读内容
背景:#EDF0F5 #FAFBE6 #FFF2E2 #FDE6E0 #F3FFE1 #DAFAF3 #EAEAEF 默认  

合泰单片机矩阵键盘程序

[日期:2008-04-12 ] [来源:网络 作者:佚名] [字体: (投递新闻)

这个程序是用合泰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

 

阅读:
录入:JETTA

推荐 】 【 打印
本文评论
      全部评论
    不错 于14日评论道: 查看全部评论  
不错 参考链接:http://www.picavr.com/n5024c39.aspx
发表评论


点评: 字数
姓名:
站长推荐