合泰单片机矩阵键盘程序
扫描二维码
随时随地手机看文章
合泰HOLTEK单片机写的,对初学者来说里面的分析思路很有帮助。
#includeht48c10-1.inc
;-------------------------------------------------------
data.section’data’;==datasection==
tempdb?;temporarydataregister
dispdb?;keydisplayregister
count1db?;delayloopcounter
maskdb?;maskregister
matrixdb?;keymatrixregister
;-------------------------------------------------------
code.sectionat0’code’;==programsection==
org00h;
jmpstart;
org04h;externalinterruptsubroutine
reti;forguarantee
org08h;timer/event0interruptsubroutine
reti;forguarantee
org0ch;timer/event1interruptsubroutine
reti;forguarantee
start:;
clriNTC;initializeregisters
clrtmrc;toguaranteeperformance
clrtmr;(interrupts)
setPAC;(ports)
setpbc;(inputmode)
setpCC;
main:
setpac;(1);setportAtoinputmode
clrpbc;setportBtooutputmode
clrpa;zeroportA(latch=0)
setpb;offLEDs
keyloop:;
MOV a,0feh;(2);sCANfirstrowofkeys
MOV matrix,a;holdscancode
MOV pac,a;pa.0output0(latch)
MOV a,pa;readinputstate
cplacc;;distillinputstate
anda,0f0h;;
szacc;ifanyinput?
jmpget_key;yes.getinputinfo
MOV a,0fdh;(2);no.scansecondrow
MOV matrix,a;holdscancode
MOV pac,a;pa.1output0(latch)
MOV a,pa;readinputstate
cplacc;;distillinputstate
anda,0f0h;;
szacc;ifanyinput?
jmpget_key;yes.getinputinfo
MOV a,0fbh;(2);no.scanthirdrow
MOV matrix,a;holdscancode
MOV pac,a;pa.2output0(latch)
MOV a,pa;readinputstate
cplacc;;distillinputstate
anda,0f0h;;
szacc;ifanyinput?
jmpget_key;yes.getinputinfo
MOV a,0f7h;(2);no.scanfourthrow
MOV matrix,a;holdscancode
MOV pac,a;outputpa.30(latch)
MOV a,pa;readinputstate
cplacc;;distillinputstate
anda,0f0h;;
szacc;ifanyinput?
jmpget_key;yes.getinputinfo
jmpkeyloop;repeatfromkeyloop
get_key:;getinputkeyinfo
calldelays;debounce
MOV a,pa;testportA
ora,0fh;
cplacc;
szacc;anykeyhold?
jmpgo_on;yes.goon(somekeyispressed)
jmpkeyloop;no.returntoscankeyagain
go_on:
callkey_in;(3);calculatetableindex
tabrdldisp;(10);loaddisplaydata
MOV a,disp;;outputdatatoportB
MOV pb,a;(11);;
jmpkeyloop;repeatkeyloop
key_inproc;getkeynumber
MOV a,pa;;holdportAstate
MOV temp,a;(4);;
get_release:;waitforthekeytobereleased
MOV a,pa;;testportAstate
cplacc;;
anda,0f0h;;
szacc;(6);ifrelease?
jmpget_release;no.keepupwaiting
MOV a,0fh;yes.calculatekeynumber
andma,matrix;(7);masklownibbleofscancode
MOV a,0;keeptableindexatregisterA
get_row:;calculaterownumber
rrcmatrix;;checkeachbittogetrownumber
snzstatus.0;;
jmpget_next;ifbingogotoget_next
clrc;
adda,4h;(8);tableindex+4(4keysarow)
jmpget_row;continuecalculating
get_next:;
MOV tblp,a;holdtableindexatregisterTBLP
MOV a,0efh;
MOV mask,a;;mask=01111111
MOV a,0fh;
orma,temp;;temp=XXXX1111
get_column:;calculatecolumnnumber
MOV a,temp;loadtemp
xora,mask;;testcolumnnumber
snzz;;
jmpindex;no.testnextcolumn
ret;yes.return(TBLP)
index:;nextcolumn
inctblp;(9);tableindex+1
setc;
rlcmask;shiftmaskleft(LSB=1)
jmpget_column;repeatget_column
key_inendp
delaysproc;delaysubroutine
MOV a,0ffh;loadcounter
MOV count1,a;
d1:
sdzcount1;countdowncount1
jmpd1
ret
delaysendp