Prime Electronics,Printable Electronics,Quantum Electronics Blog - zexscomputers.com

Independent key illuminates LED light program design

The independent button type directly uses an I/O line to create a single-button circuit. Each button occupies its own I/O line, and the operation of one button does not affect the status of other I/O lines. This design offers flexible configuration and a simple software structure, but it also means that each button requires a dedicated I/O port. As a result, when there are many buttons, this approach can lead to unnecessary use of I/O resources and is generally not recommended for large-scale applications.

In terms of software implementation, independent buttons often rely on a polling mechanism. The system continuously checks the status of the I/O line. If a low signal is detected on a particular I/O line, it indicates that the corresponding button has been pressed. At this point, the program branches to the specific function handler for that key, allowing for immediate response to user input.

When a button is pressed, the ideal waveform should remain low. However, in practice, mechanical or electrical noise causes contact bounce, resulting in rapid transitions between high and low states during the button press and release. 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 ensure the signal stabilizes before processing the input.

Program:

006.asm

This program reads the state of an independent keyboard and lights 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 in interrupt mode.

************************************************** *************************/

ORG 0000H

LJMP MAIN

ORG 000BH

LJMP INT_T0

ORG 0100H

MAIN: ; Main program entry

MOV SP, #50H ; Set stack pointer

MOV TMOD, #01H ; Set timer mode

MOV TH0, #15H ; Timer initial value

MOV TL0, #9FH

CLR 20H.0 ; Initialize button flag

MOV R2, #0FFH ; Initialize key value

MOV R3, #01H ; Initialize flashing pattern

MOV R4, #00H ; Initialize timer counter

SETB TR0 ; Start timer

MOV IE, #82H ; Enable 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

MOV A, R4

CJNE A, #0C8H, LOOP ; Check if time is up

LCALL LEED ; Cycle LEDs

LJMP L1

;/* Light the corresponding LED */

LIGH:

MOV A, R2 ; Get key value

MOV P0, A ; Output to LEDs

RET

;/* Cycle LEDs */

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 scanner */

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

;/* Interrupt service routine */

INT_T0:

CLR EA

PUSH ACC

MOV TH0, #15H ; Reload timer

MOV TL0, #9FH

INC R4

POP ACC

SETB EA

RETI

;/* Delay subroutine */

DELAY:

D1: MOV R1, #0FFH

D0: DJNZ R1, D0

DJNZ R0, D1

RET

German Warehouse

Longhua Manxueling Trading Company , https://www.mxlvape.com