Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 2nd August 2007, 02:49 AM   (permalink)
Experienced Member
Suraj143 is on a distinguished road
Default Button Debounce Coding help

Hi I want to debounce my button without a flag bit.Here is the coding but its not working well.

SHOW contains displaying digit stuff in the segments.

Code:
	btfsc	PORTA,0		;check the button is pressed?
	goto	SHOW		;NO its not pressed then display the digits
	call	DEL50mS		;YES its pressed then add a small debounce delay
	btfss	PORTA,0		;check the button has it released?
	goto	SHOW		;NO then again show the digits
	goto	COUNTUP		;YES it has released then count up
Suraj143 is offline   Reply With Quote
Old 2nd August 2007, 03:55 AM   (permalink)
Experienced Member
 
bananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of light
Default

You are using active low, aren't you?
I always use 100 ms for debouncing without any problem, with tact switch.
What switch are you using?
What happen when don't press anything and what happen when you press only once?
__________________
Superman returns..


Please vote.
bananasiong is offline   Reply With Quote
Old 2nd August 2007, 04:11 AM   (permalink)
Experienced Member
Suraj143 is on a distinguished road
Default

Yes I’m using active low.

When I press nothing it shows the “0000” in the segments.
But when I press its counting in a messy way.

I’m using micro switches

Is my code right?
Suraj143 is offline   Reply With Quote
Old 2nd August 2007, 05:28 AM   (permalink)
Experienced Member
 
bananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of light
Default

Maybe you can't release the switch fast enough? Try to increase the delay and see whether it helps.
I do this way usually:
Code:
	btfsc	PORTA,0		;check the button is pressed?
	goto	SHOW		;NO its not pressed then display the digits
	call	DEL50mS		;YES its pressed then add a small debounce delay
	btfss	PORTA,0		;check the button has it released?
	goto	$-1		;nothing happen unless it is released
	goto	COUNTUP		;YES it has released then count up
__________________
Superman returns..


Please vote.
bananasiong is offline   Reply With Quote
Old 2nd August 2007, 05:38 AM   (permalink)
Experienced Member
Suraj143 is on a distinguished road
Default

Is the displays getting off while the button is press & hold time?

It's displaying after the button released isn't it?
Suraj143 is offline   Reply With Quote
Old 2nd August 2007, 05:46 AM   (permalink)
Experienced Member
 
bananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of light
Default

Quote:
Originally Posted by Suraj143
Is the displays getting off while the button is press & hold time?
Are you using multiplexing?
Quote:
It's displaying after the button released isn't it?
When the button is released, it just goes to the label COUNTUP.
__________________
Superman returns..


Please vote.
bananasiong is offline   Reply With Quote
Old 2nd August 2007, 05:49 AM   (permalink)
Experienced Member
Suraj143 is on a distinguished road
Default

Yes I'm using multiplexing the label SHOW contains multiplexing.
Suraj143 is offline   Reply With Quote
Old 2nd August 2007, 06:01 AM   (permalink)
Experienced Member
 
bananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of light
Default

So, the label SHOW is not in the interrupt vector? Have you tried increasing the delay?
__________________
Superman returns..


Please vote.
bananasiong is offline   Reply With Quote
Old 2nd August 2007, 06:08 AM   (permalink)
Experienced Member
Suraj143 is on a distinguished road
Default

Quote:
Originally Posted by bananasiong
So, the label SHOW is not in the interrupt vector? Have you tried increasing the delay?
Hi I will try to increase the delay & see.

Can you tell a 62uS Delay is it a good or a bad delay for button debouncing.
Suraj143 is offline   Reply With Quote
Old 2nd August 2007, 06:16 AM   (permalink)
Experienced Member
 
Oznog is just really niceOznog is just really niceOznog is just really nice
Send a message via AIM to Oznog
Default

Here's one thing to keep in mind:
Say your button could bounce for 62uS. What if the person holds down the button for 100mS, then lets go? Typically off-bounce is not the same issue but I've never ruled it out- what if the delay expires, he lets off the button, and it goes off then on again then finally off for real, that would end up erroneously being detected as a second press.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is offline   Reply With Quote
Old 2nd August 2007, 06:24 AM   (permalink)
Experienced Member
Suraj143 is on a distinguished road
Default

Oh I see 62uS is too low value.Because it expires as soon if the user has press & hold on the button too long.I must try with a 100mS delay.

But for a 62uS delay I'm already checking the button has released so the small delay wont expire.

Am I right?
Suraj143 is offline   Reply With Quote
Old 2nd August 2007, 04:30 PM   (permalink)
Experienced Member
 
Oznog is just really niceOznog is just really niceOznog is just really nice
Send a message via AIM to Oznog
Default

Quote:
Originally Posted by Suraj143
Oh I see 62uS is too low value.Because it expires as soon if the user has press & hold on the button too long.I must try with a 100mS delay.

But for a 62uS delay I'm already checking the button has released so the small delay wont expire.

Am I right?
Not quite. The user might hold it for 500mS or any number really.

The procedure I've done:
Wait for button press, then take the action you take for a press and start the on-debounce counter. During this period further transitions will not be counted.
At the end of the debounce count if the button is still pressed, wait for it to be released.
When the release is detected, start the off-debounce counter.
At the end of the off-debounce period, if it's pressed take the actions you take for a press again. Otherwise wait for a new button press.

Debounce time varies greatly with the quality of the switch.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is offline   Reply With Quote
Old 3rd August 2007, 02:19 AM   (permalink)
Experienced Member
 
donniedj has a spectacular aura about
Default

I agree with Oznog. Its good to wait till button is released either before or after calling a function. The debounce time is switch dependent and environmental. I always filter and debounce, though filtering is not needed for tact switches. Generic mouse button type tacts produce good clean signals.

By the way, calling a button press function first and then processing the button release afterwards is a sign of incredible natural ability or years of good experience. So Oznog, which one are you?


If you truely wish to understand what is going one, take an observation of the switch signal with an O-scope.
Below are captures of a regular 4-pin pushbutton mouse style tact switch. Notice the very fast and relatively clean rise time.
There is actualy more noise after the button is pressed than during the act of pressing and releasing (not captured) yet still very clean.

Animated GIF file above. Must "Instruct" Internet Explorer 7 to "Allow" animation if you use IE 7 and do not see the animation.
__________________
I can still log in! Delete my account and all my post now.

Last edited by donniedj; 3rd August 2007 at 02:53 AM.
donniedj is offline   Reply With Quote
Old 3rd August 2007, 02:54 AM   (permalink)
Experienced Member
Suraj143 is on a distinguished road
Default

Wow what a video thanks donniedj I now understand.I applied Oznog codings it comes like this

Code:
	btfsc	PORTA,0		;check the button is pressed?
	goto	SHOW		;NO its not pressed then display the digits
	call	DEL500mS	;YES its pressed then add a small debounce delay
	btfss	PORTA,0		;check the button has it released?
	goto	SHOW		;NO then again show the digits
	goto	COUNTUP		;YES it has released then count up
But the problem is when increase the debounce delay time when i press the button & hold the segment displays going dim.When i reduce the debounce delay it counts up in a messy way.

can you tell me is my coding right & what modifications to be done?
Suraj143 is offline   Reply With Quote
Old 3rd August 2007, 02:57 AM   (permalink)
Experienced Member
 
Oznog is just really niceOznog is just really niceOznog is just really nice
Send a message via AIM to Oznog
Default

Quote:
Originally Posted by donniedj
I agree with Oznog. Its good to wait till button is released either before or after calling a function. The debounce time is switch dependent and environmental. I always filter and debounce, though filtering is not needed for tact switches. Generic mouse button type tacts produce good clean signals.

By the way, calling a button press function first and then processing the button release afterwards is a sign of incredible natural ability or years of good experience. So Oznog, which one are you?
I guess it's natural. Say, do you have some scope captures of a button turning off?

Incidentally, SOME applications take action on the button release, for good reason, and the user almost never notices.
For example, on an iPod if you press the (I think this is what it is) play button once it plays the track you're on, but if you hold it for 1 sec then it turns on the backlight and does NOT take that as a "play" instruction. Hmmm... how does it know not to play when you press the button? Like I say, it starts a counter when it detects a button press (debounces) counts the pressed time and if the counter reaches 1 sec then it turns on the backlight and forgets the play instruction. If the button is released before 1 sec then it'll take it as a play instruction and forget the backlight instruction. Turns out nobody holds down a button that long without a specific reason and pressing under 1 sec is so short that you won't notice the track starts on the release rather the the press.

This also gets used for some "soft reset" conditions. If your device gets in a funky state but the ISRs are still working, there's often like 2 buttons you hold down together for 3 sec to force a reset. Yet you may not want the device to take the actions those 2 buttons normally cause.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
odd project, 1 button adds a light 1 button takes a light away with sound. KevinAlaska Electronic Projects Design/Ideas/Reviews 15 6th June 2007 04:38 AM
Please help me in writing code abdosat2000 Micro Controllers 9 5th June 2007 01:40 PM
1 Hour Timer PIC 16F628A (Set Button) Suraj143 Micro Controllers 11 26th April 2007 03:54 AM
button midi keyboard jjjjjj General Electronics Chat 2 20th March 2006 04:10 PM
Push Button On/Off power to PIC eblc1388 Micro Controllers 8 17th February 2006 01:31 PM



All times are GMT. The time now is 05:36 AM.


Electronic Circuits  |  Radio Controlled
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.