Independent key illuminates LED light program design
The independent button type directly uses an I/O line to form a single button circuit. Each button occupies its own I/O line, and the operation of one button does not affect the status of others. This configuration offers flexibility in design and a simple software structure, but it requires a dedicated I/O port for each button. As a result, when there are many buttons, this approach can be inefficient in terms of I/O usage and is generally not recommended for large-scale applications.
In most cases, the software for independent buttons uses a polling mechanism. The system continuously checks the status of the I/O lines. If a low signal is detected on a particular I/O line, it indicates that the corresponding button has been pressed. The program then jumps to the associated function handler to process the key press.
Ideally, a button should remain low when pressed, but in reality, mechanical or electrical noise causes signal jitter during the rising and falling edges of the signal. To address this issue, two common methods are used: hardware debouncing with a capacitor, or software debouncing by adding a short delay (typically around 10ms) to stabilize the signal before processing the input.
Program:
File: 006.asm
Description: Read the button pressed on an independent keyboard and light the corresponding LED. If no button is pressed for a while, the LEDs will cycle through a pattern.
Wiring: P0 port is connected to eight LEDs.
P2 port is connected to eight independent buttons.
Timer T0 is used for timing, operating in interrupt mode.
************************************************** *************************/
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP INT_T0
ORG 0100H
MAIN: ; Main program entry point
MOV SP, #50H ; Set stack pointer
MOV TMOD, #01H ; Set timer 0 to mode 1 (16-bit)
MOV TH0, #15H ; Load initial value for timer high byte
MOV TL0, #9FH ; Load initial value for timer low byte
CLR 20H.0 ; Initialize button flag
MOV R2, #0FFH ; Initialize key value
MOV R3, #01H ; Initialize LED flashing pattern
MOV R4, #00H ; Initialize timer counter
SETB TR0 ; Start timer 0
MOV IE, #82H ; Enable global and timer 0 interrupts
LOOP:
LCALL KEY_READ ; Scan the keyboard
L1: JB 20H.0, LIGH ; If a key is pressed, light the corresponding LED
JBC 20H.0, LOOP ; Clear the flag and continue
MOV A, R4
CJNE A, #0C8H, LOOP ; Check if time is up
LCALL LEED ; Cycle the LEDs
LJMP L1
/* Light the corresponding LED */
LIGH:
MOV A, R2 ; Get the key value
MOV P0, A ; Output to the LED port
RET
/* LED cycling function */
LEED:
MOV A, R3
LOOP1:
CPL A
MOV P0, A
LCALL KEY_READ
JB 20H.0, RETUN0
MOV R0, #0FFH
LCALL DELAY
MOV A, R3
RL A
MOV R3, A
CJNE A, #80H, LOOP1
LOOP2:
CPL A
MOV P0, A
LCALL KEY_READ
JB 20H.0, RETUN0
MOV R0, #0FFH
LCALL DELAY
MOV A, R3
RR A
MOV R3, A
CJNE A, #01H, LOOP2
SJMP LOOP1
RETUN0:
RET
/* Keyboard scanning routine */
KEY_READ:
CLR EA ; Disable interrupts during scanning
MOV A, P2
CJNE A, #0FFH, DE
LJMP RETUN
DE:
MOV R0, #8AH
LCALL DELAY
MOV A, P2
CJNE A, #0FFH, DE0
LJMP RETUN
DE0:
MOV R2, A
SETB 20H.0
MOV R4, #00H
DE1:
MOV A, P2
CJNE A, #0FFH, DE1
RETUN:
SETB EA ; Re-enable interrupts
RET
/* Timer 0 interrupt service routine */
INT_T0:
CLR EA ; Disable interrupts
PUSH ACC ; Save accumulator
MOV TH0, #15H ; Reload timer high byte
MOV TL0, #9FH ; Reload timer low byte
INC R4 ; Increment timer counter
POP ACC ; Restore accumulator
SETB EA ; Re-enable interrupts
RETI ; Return from interrupt
/* Delay subroutine */
DELAY:
D1:
MOV R1, #0FFH
D0:
DJNZ R1, D0
DJNZ R0, D1
RET
Bang Bc5000 Einweg Vape Pen,Cheap Bang Energy Drink,Bang Bc5000 Box 5000 Puffs
Longhua Manxueling Trading Company , https://www.mxlvape.com