Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
bobgardner
PostPosted: Mar 01, 2012 - 05:44 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

My personal research shows that it is exceedingly difficult to peck a pushbutton with a duration of less than 10ms, and it is just as hard to repeat the pecking faster than 4 or 5 times a sec... 200ms? if your program runs 100 times a sec (every 10ms) and the switch might bounce a couple of times in a couple of ms, you might be unlucky and catch it when its open from a bounce on the first read, so you wont see the switch closure till 10ms later. Whether this meets your 'hard real time' requirement for switch read within one program pass needs further analysis. But the reading will just be delayed one pass, not missed.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
LDEVRIES
PostPosted: Mar 01, 2012 - 07:40 PM
Raving lunatic


Joined: May 04, 2007
Posts: 3529
Location: Geelong Australia, Home of the "Cats"

In my earlier post I suggested, as Bob did, a 10 ms. debounce of 10 ms. I picked 10 ms. on the basis that any switch that bounces for more then 10 ms. is a really bad switch, which should be considered faulty and thus thrown out.
So toggling the relay as soon as detecting a press and then doing a 10 ms. locks out any decision making until it is safely after the debounce period.
My experience is that contact bounce rarely occurs in releases, but it has happened on rare occasions! Thus it would be prudent to execute another delay of say 2-3 ms. on the release to once again lock out decision making for another short period.

I doubt whether quick successive pulse by human hand will ever be missed and if they do, it would be because the presses would be considered as "not normal operation".

Having said all that, I really hate delays in any form, and thus would execute it as a state machine, where the make & break delays are implemented by lockout states.

There are many ways to skin a cat! (Sorry to the pussy lovers!)

_________________
Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
(If you haven't already done so, edit your PostNuke profile and let let us know where you are, what you do & what your interests are.)
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
kk6gm
PostPosted: Mar 01, 2012 - 08:36 PM
Raving lunatic


Joined: Sep 12, 2009
Posts: 2476
Location: Sacramento, CA

bobgardner wrote:
Nope. Not if the interval between switch reads is longer than the bounce interval.

That IS debouncing.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 01, 2012 - 08:43 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Naaah. Its just a delay. Debouncing is a big complicated interrupt handler that shifts bits into a register and counts how many are hi and lo. Byzantine.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
chartman
PostPosted: Mar 01, 2012 - 10:59 PM
Resident


Joined: May 01, 2003
Posts: 580


sometimes putting a cap across a switch will remove the need for debouncing.There's probably as many solutions as there are questions...
 
 View user's profile Send private message  
Reply with quote Back to top
LDEVRIES
PostPosted: Mar 01, 2012 - 11:09 PM
Raving lunatic


Joined: May 04, 2007
Posts: 3529
Location: Geelong Australia, Home of the "Cats"

IMHO Anything done to overcome bouncing, is de-bouncing!

_________________
Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
(If you haven't already done so, edit your PostNuke profile and let let us know where you are, what you do & what your interests are.)
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
kk6gm
PostPosted: Mar 02, 2012 - 01:23 AM
Raving lunatic


Joined: Sep 12, 2009
Posts: 2476
Location: Sacramento, CA

LDEVRIES wrote:
IMHO Anything done to overcome bouncing, is de-bouncing!

Sounds reasonable to me. Smile
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 02, 2012 - 06:30 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Can anyone post a program that proves bouncing exists? This might be 'Much Ado About Nothing'

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 02, 2012 - 06:52 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

Bob. I don't have code to post right now, but I've seen bounce effecs many times on the STK-500.

It exists.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LDEVRIES
PostPosted: Mar 02, 2012 - 07:52 AM
Raving lunatic


Joined: May 04, 2007
Posts: 3529
Location: Geelong Australia, Home of the "Cats"

Quote:
Can anyone post a program that proves bouncing exists?

Piece of pie.
1. Set up a port for 8 outputs & 8 LEDS
2. Set up an external interrupt with a SPST switch of some sort
3. In the ISR execute
Code:
 PORTx+=1;


Press the button. It wont be long before you have multiple LED increments.
QED

This is the basic technique that I used to show students contact bounce.
Some buttons are noisier than others of course and occasionally some really crappy switches will bounce on release as well.

_________________
Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
(If you haven't already done so, edit your PostNuke profile and let let us know where you are, what you do & what your interests are.)
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Visovian
PostPosted: Mar 02, 2012 - 10:21 AM
Posting Freak


Joined: Aug 07, 2007
Posts: 1505
Location: Czech

MASNSN:
Try this
Code:
//Atmega8, 8 MHz
#include <avr/io.h>
#include <avr/interrupt.h>     

ISR(TIMER0_OVF_vect)        //ovf 8 ms
{
static uint8_t state;       

   //if pinb.0=0 in 8 subsequent interrupts
   //then toggle portb.1
   if(bit_is_clear(PINB,0)) //if buton pressed
   {
      state++;
      if(state == 8) PORTB ^= (1<<1); //toggle Led
   }       
   else state = 0;
}

//===============================================================================
int main(void)
{
   DDRB   = 0b00000010; //portb.1 Led
   PORTB  = 0b00000001; //portb.0 buton, pull-up
   TCCR0  = (1<<CS02);  //prescaler 256
   TIMSK  = (1<<TOIE0); //enable timer0_ovf 
   sei();

   for(;;)
   {
   }
}
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 02, 2012 - 10:23 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

Can anyone post a program that proves bouncing exists? This might be 'Much Ado About Nothing'

I can't believe I just read that - can someone pinch me?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 02, 2012 - 01:55 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

My point was if we have to read a switch that bounces a couple of times over a several ms interval and the switch actuation interval is also in the ms range, then there is a problem. If a human is pressing the switch, program the computer to not read the switch faster than the human can press it, then the problem goes away. I guess there are machines that have limit switches that can be actuated at 200Hz (5 ms). A couple of ms of bounce would be a problem. Needs a filter or some help.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
meteor
PostPosted: Mar 02, 2012 - 06:25 PM
Hangaround


Joined: Dec 11, 2010
Posts: 183


bobgardner wrote:
Can anyone post a program that proves bouncing exists? This might be 'Much Ado About Nothing'
Shocked
With all of the talk about debouncing on this forum over the years, I'm surprised that few people seem to have actually run Jack Ganssle's well-known experiment.

I did just that several months ago after having purchased several different types of switches for a project. Some were ordinary normally-open, momentary-contact pushbutton switches and others were SPST toggle switches and one was an old-style pull-on, push-off automotive type.

All were hooked up to my oscilloscope and tested semi-extensively.

What I found:
  • There was a wide variety of bouncing behavior. One pushbutton (Mouser P/N 112-R13-502A-G/B) had virtually no bounce on closure while a SPST toggle switch (bought at a local store) had up to 12 distinct bounces after closure.
  • The worst case for all switches that I tested was 2320 usec (2.3 milliseconds) of bouncing. Yes, that was the same 12-bouncer (a baton-style SPST toggle switch) mentioned above.
  • Bouncing on switch opening was rarer, but it certainly did happen, most frequently with that same SPST 12-bouncer toggle switch.
What I saw was not totally unexpected, but it was in some ways still an eye-opening experience. It wasn't too long after my Ganssle-style experimentation that I finally got motivated to write up a test app based on the well-known, efficient Peter Dannegger ('danni') debounce algorithm. Shortly thereafter, that test code was incorporated into all pertinent project code of mine.
 
 View user's profile Send private message  
Reply with quote Back to top
MASNSN
PostPosted: Mar 03, 2012 - 11:43 AM
Wannabe


Joined: Dec 31, 2011
Posts: 87


clawson wrote:
I think we maybe need OP to take a step back and describe exactly what kind of button/switch he is talking about. I was assuming something like:



or maybe:



but not:



or



(which is actually a pushbutton that toggles)



It is the first kind of button clawson. Anyway I found the solution
 
 View user's profile Send private message  
Reply with quote Back to top
MASNSN
PostPosted: Mar 03, 2012 - 11:47 AM
Wannabe


Joined: Dec 31, 2011
Posts: 87


chartman wrote:
Quote:
This is not what I need. What I exactly need is debouncing

1. You press the button a led is on
2. You release the buttont the led remains on
3. You press the button again the led is turned off
4. You release the buttont the led remains off
5. You press the button again and led is once again on and so on


Poor old MASNSN he must be really confused now. He was right all along in asking about TOGGLE.The 'quote' above is still wrong my friend.... what you are describing is a 'toggle' implementation of a switch, not 'debounce'.
To get a debounce you will need to insert a delay between steps 1&3 (or 2&3) somewhere in your software.A suggestion would be a minimum of 10milliseconds.
I hope that helps you understand a bit clearer.


People in forum said that this behaviour is 'debouncing' and not 'toggling' but anyway finally I reached my goal and found the solution by myself
 
 View user's profile Send private message  
Reply with quote Back to top
MASNSN
PostPosted: Mar 03, 2012 - 11:56 AM
Wannabe


Joined: Dec 31, 2011
Posts: 87


Folks
Thank you all, I found the solution, I wrote the code that helps me toggle two leds at once one green led for the mode ON in PINB0 and one another red led for the OFF mode in the PINB2.
Code:

   DDRB |= 1<<PINB0;//assigning pin0 as output
   DDRB &= ~(1<<PINB1); //PINB1 as input
   DDRB |= 1<<PINB2;//assigning pin0 as output
   //Set the initial statuts of the both leds
   PORTB|=1<<PINB0;
   PORTB&=~(1<<PINB2);
   //This is a flag to keep the status
   int  pressed = 0;
   while(1)
   { 
          /*bit_clear is a simple routine to check whether a pin is high it is simple to write as a function or a macro*/
          if(bit_clear(PINB,1))
     {
          if(pressed ==0)
          {
          PORTB^=1<<PINB0;
           PORTB^=1<<PINB2;
          pressed = 1;
          }
     }
     else
     {
       pressed = 0;
     }
   }


Finally it is important or place a ceramic 22pF capacitor in the legs of the button to prevent the oscillation effect when pressing and releasing. I did the experience and it works fine.
 
 View user's profile Send private message  
Reply with quote Back to top
MASNSN
PostPosted: Mar 03, 2012 - 12:11 PM
Wannabe


Joined: Dec 31, 2011
Posts: 87


Visovian wrote:
MASNSN:
Try this
Code:
//Atmega8, 8 MHz
#include <avr/io.h>
#include <avr/interrupt.h>     

ISR(TIMER0_OVF_vect)        //ovf 8 ms
{
static uint8_t state;       

   //if pinb.0=0 in 8 subsequent interrupts
   //then toggle portb.1
   if(bit_is_clear(PINB,0)) //if buton pressed
   {
      state++;
      if(state == 8) PORTB ^= (1<<1); //toggle Led
   }       
   else state = 0;
}

//===============================================================================
int main(void)
{
   DDRB   = 0b00000010; //portb.1 Led
   PORTB  = 0b00000001; //portb.0 buton, pull-up
   TCCR0  = (1<<CS02);  //prescaler 256
   TIMSK  = (1<<TOIE0); //enable timer0_ovf 
   sei();

   for(;;)
   {
   }
}


Thank you for the code, really appreciate that however I will let the interrupt for another task. I explain more.
The atmega8 will control three tasks
1.Task 1 is switchinq a relay ON/OFF
2.Task 2 after x minutes it will switch the relay OFF and I will preserve the timer interrupt to this task
3.Task 3 I will use an LCD to display informations to the user countdown until the condition to switch the relay off is met

Any Idea
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 03, 2012 - 12:28 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

But you have no form of debounce in that code? And a "tact switch" (that's what the first picture is called) are notorious for contact bounce.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Mar 03, 2012 - 12:58 PM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16547
Location: Wormshill, England

Quote:
Finally it is important or place a ceramic 22pF capacitor in the legs of the button to prevent the oscillation effect when pressing and releasing. I did the experience and it works fine.


You do not appear to be using an internal pull-up on your switch.
Do you have an external pull-up resistor?

A typical internal pull-up is 30k. Your RC time-constant (with 22pF) is 660ns.
A 1M0 external pull-up would still be 22us.

I would be very surprised if any real-life switches would stop bouncing this quickly.

In practice, there are many tricks for detecting a single button-press.

A 22nF capacitor would probably work fine with a 30k internal pull-up or even 1M.

Even Uncle Bob can only press his guitar picking fingers at a super-human speed.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits