Saturday, April 30, 2016

8051 Program – detect switch debounce

; detect switch debounce
; 8051 with 12Mhz crystal
; led1 is connected to P1.0
; led1 is connected to P1.1
; switch is connected to P1.2
; turn on led1 for proper switch press
; toggle led2 for debounce error
ORG 0H
led1 EQU P1.0
led2 EQU P1.1
switch EQU P1.2
MAIN:
init:
SETB switch ;enable interrupt
CLR led1 ;turn off led1
CLR led2 ;turn off led2
wait:
JB switch,wait ;wait till switch has been pressed
ACALL DEBOUNCE_DELAY
JB switch,error
success:;after debouncing period, switch is low hence switch succesfully debounced
SETB led1 ;turn on led1
JNB switch,$ ;wait till switch has been released
CLR led1 ;turn off led1
AJMP wait
error:;switch goes high after debounce period so error in debouncing
CPL led2
AJMP wait
DEBOUNCE_DELAY: ;subroutine for generating 20ms delay
MOV R7,#245
l1_debounce_delay:
MOV R6,#40
DJNZ R6,$
DJNZ R7,l1_debounce_delay
RET
END



Related topics:
8051 Program - send ascii to port   |   8051 Program - serializing data   |   8051 Program - simple serial transmission   |   8051 Program - timer based led blinking   |   8051 Program - timer interrupt based led blinking   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment