Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

switch debouncing problem

Status
Not open for further replies.
Hey there,

I'm making the switch debouncing thing to work, but however, I end up the whole LED blinking instead of one touch switch, LED ON, and then another one, LED OFF.

Here's the code I've squeezed out so far. Wow, ASM is really tough!

Code:
main
btfsc    PORTA, 3
call    LEDOFF
call     LEDON

;btfss    PORTA, 3
;call    LEDON
;call    LEDOFF

goto    main

;switch1
;call    Delay
;btfsc    PORTA,3
;return
;btfss    PORTC,0
;goto    LEDON
;goto    LEDOFF

LEDON
bsf        PORTC,0
call    Delay
btfsc    PORTA,3
goto    LEDON
retlw    0x00

LEDOFF
bcf        PORTC,0
call    Delay
btfsc    PORTA,3
retlw    0x00
goto    LEDOFF

Anything missing here? :)
 
The thing that is missing is some comments:D. We have no idea if A3 is high or low when a key is pressed or if C0 low makes the LED light.

However, the best way to detect a key press is to wait for it to be released and then wait for it to be pressed. I've assumed that the key makes A3 <edit>low</edit> and C0 high is LED on.
Code:
WaitNoKey	btfs[COLOR="Red"]s[/COLOR]	PORTA,3		;key released?
		goto	WaitNoKey	;no,so wait
		call	Delay		;short delay for debounce
WaitKey		btfs[COLOR="red"]c[/COLOR]	PORTA,3		;key pressed?
		goto	WaitKey		;Edit, changed this. no,so wait
		call	Delay		;short delay for debounce

;now toggle LED

		btfss	PORTC,0		;is LED on
		goto	TurnOn		;no, so go turn it on
		bcf	PORTC,0		;yes, so turn it off
		goto	DoneLED
TurnOn		bsf	PORTC,0		
DoneLED		goto	WaitNoKey	;go do it all again
Note, the delay should be very short - around 10mS.

Mike.
 
Last edited:
Ok, sorry for the lack of information.

I'm playing with the Demo Board from the Pic Kit 2, the PORTA,0 must be in ground when the switch is activated. :)
 
Ok, sorry for the lack of information.

I'm playing with the Demo Board from the Pic Kit 2, the PORTA,0 must be in ground when the switch is activated. :)

The code should have worked but with the LED changing when the key is released. I've edited it now so it is active low.

A little tip when writing code, try to keep the different sections separate. If you look at the code above it is in two sections, key reading and LED toggling. By replacing the key section with a 1 second delay will be able to tell if the LED section is working before you go onto debugging the key code.

Mike.
 
The code should have worked but with the LED changing when the key is released. I've edited it now so it is active low.

A little tip when writing code, try to keep the different sections separate. If you look at the code above it is in two sections, key reading and LED toggling. By replacing the key section with a 1 second delay will be able to tell if the LED section is working before you go onto debugging the key code.

Mike.

Ok I got it! Thanks!

But, however when I press the switch, the LED turns on only. Pressing back the switch, it wouldn't budge, and it remains on.

I got the switch debouncing part already, but I love to make those toggle switch like the one in many new electrical appliances where you can turn on the thing and off the thing using the same button. :)
 
Are you talking about the code I posted or your original code? Whoops, had a typo in that code I posted, edited it now.

Mike.
 
Last edited:
Yeah I mean the code you have posted. Like any R-S flip-flop (or bistable multivibrator), this LED only switches on once the button is pressed. Pressing the same button will not turn it off.

What I mean is, turning the LED on and off using the same button. I kinda figured that one for hours, but couldn't get it. :)
 
What is the code for your delay routine ? It may not be returning, or maybe the active pullup is not selected ?
 
What is the code for your delay routine ? It may not be returning, or maybe the active pullup is not selected ?

Ok, I got it done, but without the debouncing check. Something's frizzled inside the Delay subroutine which I 'composed' it using Picloops software.

Here's the code, basically it's working, except the switch can be very freaky. The delay is really needed there.

Code:
main
btfsc    PORTA,3
goto    main
goto    TurnOnLED

TurnOnLED
bsf        PORTC,0   ; turn on LED @ bit 0
btfsc    PORTA,3    
goto    TurnOnLED   ; if 3rd bit not clear
goto    TurnOffLED   ; if 3rd bit clear

TurnOffLED
bcf        PORTC,0
btfsc    PORTA,3
goto    TurnOffLED
goto    TurnOnLED

Delay    movlw    D'13'
        movwf    count2
        movlw    D'251'
        movwf    count1
loop    decfsz    count1,1
        goto    loop
        decfsz    count2,1
        goto    loop
        return

(active low input) :)
 
I would change this to

Delay movlw D'13'
movwf count2
movlw D'251'
movwf count1
loop decfsz count1,f
goto loop
decfsz count2,f
goto loop
return

Both decfsz instructions have been changed.
 
Question....

Solving a problem your own way is important, so I don't mean to undermine anything, but solving a debounce in software has always seems silly to me. Why not just throw a resistor and capacitor in parallel to ground? Chose values such as ~1k and .1uF, or whatever suits your cause.

Seems easier to me, but if solving it with software is the point, then nevermind.
 
If you want hardware solution in a small 4 pin package look at the MAX6816/MAX6817/MAX6818...
Use a 1k & 10uf for a 10mS delay
1K * 10uf = 10mS
1000 * .000010 = .010 seconds
 
Last edited:
Ok, got this thing working, but it's only for one bit operation.

What if I have to call a function using this switch ? I figured it out but i couldn't get the thing working if I use the switch to call a function. :)
 
Commercial designs

but solving a debounce in software has always seems silly to me.

Commercial designs do, saving space and components, simplifying the PCB.

In line with that, most of the "software" functions could be implemented in hardware. That would be silly!

By the way, once you know what debouncing means, implementing it in software is conceptually simple. I had to use up to 30 ms with bad quality pushbuttons.

There is an interesting article by Jack Gansle (I think that is the right name) and a lot of routines by Scot Datalo where all that is addressed in detail. Read them. Worth your time.
 
Commercial designs do, saving space and components, simplifying the PCB.

In line with that, most of the "software" functions could be implemented in hardware. That would be silly!
We do all the time. We push port pins through buffers and static protection, doing the debounce and keyboard scanning in software for 64+ button membrane switches.

It is worth noting that computer keyboards are scanned and debounced in software as well... it would be VERY prohibitive to do it all in hardware.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top