多个按键控制LED程序
1 KEY_COMMON bit P3.2 ;按键公共端 2 KEY_1 bit p0.5 ;三个按键 3 KEY_2 bit p0.4 4 KEY_3 bit p0.3 5 6 BEEP bit p2.0 ;蜂鸣器 7 8 LED_1 bit p1.7 ;六个LED灯 9 LED_2 bit p1.6 10 LED_3 bit p1.5 11 LED_4 bit p1.4 12 LED_5 bit p1.3 13 LED_6 bit p1.2 14 15 org 0000H 16 jmp MAIN 17 18 ;******************************************* 19 org 0030H ;Start program from 0030H 20 MAIN: 21 mov P0,#0FFH ;Initialize the port,初始化端口 22 mov P1,#0FFH 23 mov P2,#0FFH 24 mov P3,#0FFH 25 26 clr KEY_COMMON ;Clear the common line 27 MAIN_LOOP: 28 jb KEY_1,LAB_KEY_2 ;Wait for key down,按下按键1条跳到LAB_KEY_2,一个都没按做循环直到按了按键
29 call DELAY 30 cpl LED_1 ;Change the state of LED8,(累加器求反) 31 cpl LED_2 ;Change the state of LED7 32 jmp NEXT_LOOP 33 34 LAB_KEY_2: 35 jb KEY_2,LAB_KEY_3 ;Wait for key down 36 call DELAY 37 cpl LED_3 ;Change the state of LED6 38 cpl LED_4 ;Change the state of LED5 39 jmp NEXT_LOOP 40 41 LAB_KEY_3: 42 jb KEY_3,NEXT_LOOP ;Wait for key down 43 call DELAY 44 cpl LED_5 ;Change the state of LED4 45 cpl LED_6 ;Change the state of LED3 46 jmp NEXT_LOOP 47 48 NEXT_LOOP: 49 50 jmp MAIN_LOOP 51 52 ret 53 ;**************************************************** 54 DELAY: 55 mov r7,#200 56 DELAY_LOOP: 57 mov r6,#200 58 djnz r6,$ 59 mov r6,#200 60 djnz r6,$ 61 62 djnz r7,DELAY_LOOP 63 ret 64 ;**************************************************** 65 END