单片机原理与应用-基于汇编、C51及混合编程1-11章习题解答

LOOP:MOV A,P1 CJNE A,#0FFH,LOOP1 SJMP LOOP

LOOP1:LCALL DELAY_15ms ;延时去抖动 MOV A,P1 CJNE A,#0FFH,LOOP2 SJMP LOOP

LOOP2: ;确实有按键按下 JNB ACC.0,KEY1 JNB ACC.1,KEY2

JNB ACC.2,KEY3 JNB ACC.3,KEY4

JNB ACC.4,KEY5 JNB ACC.5,KEY6 JNB ACC.6,KEY7 JNB ACC.7,KEY8 LJMP LOOP KEY1: MOV P3,#0F9H LJMP LOOP KEY2: MOV P3,#0A4H LJMP LOOP KEY3: MOV P3,#0B0H LJMP LOOP KEY4: MOV P3,#99H LJMP LOOP KEY5: MOV P3,#92H LJMP LOOP KEY6: MOV P3,#82H AJMP LOOP KEY7: MOV P3,#0F8H LJMP LOOP KEY8: MOV P3,#80H LJMP LOOP

DELAY_15ms:MOV R6,#177 DL0: MOV R5,#0 DJNZ R5,$ DJNZ R6,DL0 RET END

C51程序:

#include

unsigned char temp,temp1; void delay_15ms();

49

void KEY1(); ……

void KEY8(); void main() {

P1=0xFF; while(1) {

temp=P1;

if(temp!=0xFF) {

delay_15ms(); temp=P1;

if(temp!=0xFF) {

switch(temp) {

case 0xFE:KEY1();break; case 0xFD:KEY2();break; case 0xFB:KEY3();break; case 0XF7:KEY4();break; case 0xEF:KEY5();break; case 0xDF:KEY6();break; case 0xBF:KEY7();break; case 0x7F:KEY8();break; } } } } }

void delay_15ms() {

unsigned char a,b,c; for(c=3;c>0;c--)

for(b=212;b>0;b--) for(a=140;a>0;a--); }

void KEY1() {

P3=0xF9; }

void KEY2() {

P3=0xA4;

50

联系客服:779662525#qq.com(#替换为@)