P0口定义为发光二极管,用于直观显示,P1口定义为发光二极管,用于按键,它们一一对应.
org 0000h
buf1 equ 30h/*定义数据显示缓冲区*/
buf2 equ 31h
buf3 equ 32h
buf4 equ 33h
buf5 equ 34h
buf6 equ 35h
led1 bit p0.0/*定义灯位,使程序易于看懂*/
led2 bit p0.1
led3 bit p0.2
led4 bit p0.3
led5 bit p0.4
led6 bit p0.6
ajmp main
org 0030h/*主程序从0030H开始*/
main: mov p1,#0ffh
mov a,#00h/*显示缓冲区送0*/
mov buf1,a
mov buf2,a
mov buf3,a
mov buf4,a
mov buf5,a
mov buf6,a
key: acall disp/*调显示子程序*/
key0: mov a,p1/*读P1口*/
cpl a/*内容取反*/
anl a,#3fh/*屏蔽高二位*/
jz key/*内容为0,无键按下,转KEY*/
acall disp/*调用显示程序,作为延时去抖*/
mov a,p1/*再读键*/
cpl a
anl a,#3fh
jz key
jb acc.0,key1/*是KEY1键,转KEY1*/
jb acc.1,key2/*是KEY2键,转KEY2*/
jb acc.2,key3/*是KEY3键,转KEY3*/
jb acc.3,key4/*是KEY4键,转KEY4*/
jb acc.4,key5/*是KEY5键,转KEY5*/
jb acc.5,key6/*是KEY6键,转KEY6*/
ajmp key/*转KEY*/
key1: mov buf1,#1/*显示缓冲区送1*/
cpl led1/*取反灯,即当前灯亮则灭,灯灭则亮*/
cf0: jnb p1.0,xs0/*判别按键是否释放,若未始终调用显示程序,直到按键松开*/
ajmp key/*转KEY*/
key2: mov buf2,#2
cpl led2
cf1: jnb p1.1,xs1
ajmp key
key3: mov buf3,#3
cpl led3
cf2: jnb p1.2,xs2
ajmp key
key4: mov buf4,#4
cpl led4
cf3: jnb p1.3,xs3
ajmp key
key5: mov buf5,#5
cpl led5
cf4: jnb p1.4,xs4
ajmp key
key6: mov buf6,#6
cpl led6
cf5: jnb p1.5,xs5
ajmp key
xs0: acall disp/*调显示程序*/
ajmp cf0
xs1: acall disp
ajmp cf1
xs2: acall disp
ajmp cf2
xs3: acall disp
ajmp cf3
xs4: acall disp
ajmp cf4
xs5: acall disp
ajmp cf5
/*数据显示子程序,显示数据存放在BUF1-BUF6中,其数据形式为BCD码,存放在低四位*/
disp: mov a,buf6
add a,#50h/*数据位值和数据段值合并存于累加器*/
mov p2,a/*送十万位数据*/
acall delay
mov a,buf5
add a,#40h
mov p2,a/*送万位数据*/
acall delay
mov a,buf4
add a,#30h
mov p2,a /*送千位数据*/
acall delay
mov a,buf3
add a,#20h
mov p2,a/*送百位数据*/
acall delay
mov a,buf2
add a,#10h
mov p2,a /*送十位数据*/
acall delay
mov a,buf1
add a,#00h
mov p2,a/*送个位数据*/
acall delay
ret
delay: mov r7,#06h/*3ms*/
dl1: mov r6,#0fah
djnz r6,$
djnz r7,dl1
ret
end