问题如下: 检测键盘,要求按下后,只输入一个值,直至松开才继续下一检测。 思路:以一状态标致过程,名字:key_status,当值为0时说明前面没键按下,此时如果有键按下,可以输入一个键盘值;当值为1时,说明此时正为输入值; 当值为2时说明此时键按下,但前面已输入过一次键盘的值,不再输入。
说明:键盘分布如下: 12个键,P3控制的扫描检测。 分别为:列 P3.5 P3.6 P3.7 行 P3.0 P3.1 P3.2 P3.3
开始调试时,代码如下: delay_time equ 7fh led1 equ 7eh led2 equ 7dh led3 equ 7ch led4 equ 7bh Key_number equ 7ah key_status equ 79h; 00h无键按下,等待按键。01h有键按下,返回数值。02h有键按下,但已返回过数值,不再返回.
org 0000h ajmp start org 0100h start: mov led1,#01 mov led2,#00 mov led3,#00 mov led4,#00 acall LED_Print main: acall LED_Print mov delay_time,#01h acall Delay acall Key_Test mov A,key_status cjne A,#01h,main mov led4,led3 mov led3,led2 mov led2,led1 mov led1,key_number ajmp main Key_Test: mov P3,#0ffh clr P3.0 mov dptr,#Key_table1 mov A,P3 anl A,#11100000b mov R0,A mov A,#11100000b subb A,R0 rl A rl A rl A movc A,@A+dptr mov Key_number,A cjne A,#0ffh,key_end mov P3,#0ffh clr P3.1 mov dptr,#Key_table2 mov A,P3 anl A,#11100000b mov R0,A mov A,#11100000b subb A,R0 rl A rl A rl A movc A,@A+dptr mov Key_number,A cjne A,#0ffh,key_end mov P3,#0ffh clr P3.2 mov dptr,#Key_table3 mov A,P3 anl A,#11100000b mov R0,A mov A,#11100000b subb A,R0 rl A rl A rl A movc A,@A+dptr mov Key_number,A cjne A,#0ffh,key_end mov P3,#0ffh clr P3.3 mov dptr,#Key_table4 mov A,P3 anl A,#11100000b mov R0,A mov A,#11100000b subb A,R0 rl A rl A rl A movc A,@A+dptr mov Key_number,A cjne A,#0ffh,key_end mov key_status,#00h ret key_end: ;have press mov A,key_status cjne A,#00,no_return mov key_status,#01h ret no_return: mov key_status,#02h ret
LED_Print: mov A,Led1 mov DPTR,#Led_table movc A,@A+DPTR mov p3,A mov A,Led2 mov DPTR,#Led_table movc A,@A+DPTR mov p2,A mov A,Led3 mov DPTR,#Led_table movc A,@A+DPTR mov p1,A mov A,Led4 mov DPTR,#Led_table movc A,@A+DPTR mov p0,A ret Key_table1: db 0ffh,000h,001h,000h,002h Key_table2: db 0ffh,003h,004h,000h,005h Key_table3: db 0ffh,006h,007h,000h,008h Key_table4: db 0ffh,009h,00ah,000h,00bh
Led_table: db 0c0h,0f9h,0A4h,0B0h,099h,092H,082h,0F8h,080h,090h,0ffh,00h Delay: mov R0,delay_time delay_loop: mov R1,#0ffh djnz R1,$ djnz R0,delay_loop ret END
下贴继续 |