機電之家資源網(wǎng)
單片機首頁|單片機基礎(chǔ)|單片機應(yīng)用|單片機開發(fā)|單片機文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
合泰單片機矩陣鍵盤程序
合泰單片機矩陣鍵盤程序
 更新時間:2008-7-26 20:05:08  點擊數(shù):2
【字體: 字體顏色

這個程序是用合泰HOLTEK單片機寫的,對初學(xué)者來說里面的分析思路很有幫助。

#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

 

  • 上一篇: HT48RA0單片機的時鐘程序
  • 下一篇: HT46R23控制的智能電飯鍋源程序
  • 發(fā)表評論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機電之家 | 會員助手 | 免費鏈接

    點擊這里給我發(fā)消息66821730(技術(shù)支持)點擊這里給我發(fā)消息66821730(廣告投放) 點擊這里給我發(fā)消息41031197(編輯) 點擊這里給我發(fā)消息58733127(審核)
    本站提供的機電設(shè)備,機電供求等信息由機電企業(yè)自行提供,該企業(yè)負責(zé)信息內(nèi)容的真實性、準確性和合法性。
    機電之家對此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請聯(lián)系機電之家,機電之家將及時作出處理。
    Copyright 2007 機電之家 Inc All Rights Reserved.機電之家-由機電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機電一體化學(xué)會
    中國行業(yè)電子商務(wù)100強網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1