UpDown EQU 00H ; Up and down flag StartEnd EQU 01H ; Start and stop flag LAMPCODE EQU 21H ;Store the flow data code ORG 0000H AJMP MAIN ORG 30H MAIN: MOV SP,#5FH MOV P1,#0FFH CLR UpDown ; Starts in the upward state CLR StartEnd ;Start in stopped state MOV LAMPCODE,#0FEH ; Single lamp flow code LOOP: ACALL KEY ;Call keyboard program JNB F0,LNEXT ;If no key is pressed, continue ACALL KEYPROC ; otherwise call the keyboard handler LNEXT: ACALL LAMP; Call the lamp display program AJMP LOOP; Repeated loop, the main program ends here ;--------------------------------------- DELAY: MOV R7,#100 D1: MOV R6,#100 DJNZ R6,$ DJNZ R7,D1 RET ;----------------------------------------Delay program, called during keyboard processing KEYPROC: MOV A,B ; Get the key value from register B JB ACC.2,KeyStart ; Analyze the key code. If a bit is pressed, the bit is 1 (because it has been inverted in the keyboard program) JB ACC.3,KeyOver JB ACC.4,KeyUp JB ACC.5,KeyDown AJMP KEY_RET KeyStart: SETB StartEnd ; Processing after the first key is pressed AJMP KEY_RET KeyOver: CLR StartEnd ; Processing after the second key is pressed AJMP KEY_RET KeyUp: SETB UpDown ; Processing after the third key is pressed AJMP KEY_RET KeyDown: CLR UpDown ; Processing after the fourth key is pressed KEY_RET:RET KEY: CLR F0; Clear F0, indicating that no key is pressed. ORL P3,#00111100B ; Set the four bits connected to the key of port P3 to 1 MOV A,P3; Get the value of P3 ORL A,#11000011B ; Set the remaining 4 positions to 1 CPL A ; Negate JZ K_RET ; if it is 0, no key is pressed ACALL DELAY; otherwise delay to remove key jitter ORL P3,#00111100B MOV A,P3 ORL A,#11000011B CPL A JZ K_RET MOV B,A ; If a key is pressed, store the key value in B SETB F0 ; Set the key pressed flag K_RET: ORL P3,#00111100B ; This loop waits for the key to be released MOV A,P3 ORL A,#11000011B CPL A JZ K_RET1 ; Return from the keyboard processing program until the read data is inverted and becomes 0, indicating that the key has been released AJMP K_RET K_RET1: RET ;---------------------------------- D500MS: ;Delay time of running light PUSH PSW SETB RS0 MOV R7,#200 D51: MOV R6,#250 D52: NOP NOP NOP NOP DJNZ R6,D52 DJNZ R7,D51 POP PSW RET ;---------------------------------- LAMP: JB StartEnd, LampStart ; If StartEnd=1, start MOV P1,#0FFH AJMP LAMPRET ; otherwise close all displays and return LampStart: JB UpDown, LAMPUP; If UpDown=1, flow upward MOV A,LAMPCODE RL A; Actually it is just a left shift MOV LAMPCODE,A MOV P1,A LCALL D500MS AJMP LAMPRET LAMPUP: MOV A,LAMPCODE RR A; Flowing downward is actually moving right MOV LAMPCODE,A MOV P1,A LCALL D500MS LAMPRET: RET END |