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
MASNSN
PostPosted: Feb 29, 2012 - 08:46 PM
Wannabe


Joined: Dec 31, 2011
Posts: 87


Hi
I'm using Atmega8 as a processor and one of the tasks is to control a relay. For that I used two pins PINB0 and PINB1. The PINB0 is configured as output and used to control the relay and the PINB1 is configured as input to recieve signal from a button.
What I want is that when I press and release the button the PINB0 is set to 1 when I press and release the button again then PINB0 is set to 0 when I press and release the button for the third time then PINB0=1 and so on. In the other words the toogle is not following the button status(pressed or relased) but is following the number of times of pressing and releasing.

Thank you
 
 View user's profile Send private message  
Reply with quote Back to top
meslomp
PostPosted: Feb 29, 2012 - 08:57 PM
Raving lunatic


Joined: May 02, 2007
Posts: 3154
Location: Nieuwegein, Netherlands

and what is the question here?
the magic word is 'debounce'

and keep in mind that when you de-activate the relay that its voltage will invert and thus might destroy your chip if you switch the relay directly. not mentioning too much current drawn, so you will need a flyback switch to handle the relay.

if you had no clue what to do, you now have plenty of reading to do Wink

_________________
1)Datasheet and application notes checked?
2)tutorial forum
3)Newbie start here
 
 View user's profile Send private message  
Reply with quote Back to top
lincoln
PostPosted: Feb 29, 2012 - 09:53 PM
Hangaround


Joined: Jul 22, 2011
Posts: 146
Location: San jose, CA, USA

http://www.ganssle.com/debouncing.htm

_________________
link

i am a NOOO00B!!

Don’t let that undermine what I just said.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
LDEVRIES
PostPosted: Feb 29, 2012 - 10:19 PM
Raving lunatic


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

I am not sure what a "toogle" is?
After you either operate or release the relay, just execute a 10 ms. delay should fix your problem. Then read the references above!

_________________
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
chartman
PostPosted: Feb 29, 2012 - 10:24 PM
Resident


Joined: May 01, 2003
Posts: 580


Simlpy compliment PB0 on every (debounced) button push..
 
 View user's profile Send private message  
Reply with quote Back to top
kk6gm
PostPosted: Feb 29, 2012 - 10:52 PM
Raving lunatic


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

chartman wrote:
Simlpy compliment PB0 on every (debounced) button push..


And a button push is defined as "was inactive last time I looked, and is active this time I looked".
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 01, 2012 - 12:24 AM
10k+ Postman


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

This is a 'software toggle'. It converts a momentary button to pushon-pushoff.
The moment of learning needs a code example to light the light bulb.
pa0 is the button, pb0 is the output (set them up at power on)
Code:

char but;     //state of button
char butonos; //button pressed oneshot
char butl;    //button last pass
char relayon; //state of relay
.
.
.
but=((PINA & 0x01)==0x00); //read button
butonos=but && !butl;      //button pressed one shot
butl=but;                  //remember last pass
relay ^= butonos;          //toggle relay var on button oneshot
if(relay) PORTB |= 0x01; else PORTB &= ~0x01; //copy relay var state to output pin
.
.
.
//dont come back till 10ms are used

_________________
Imagecraft compiler user


Last edited by bobgardner on Mar 02, 2012 - 02:12 PM; edited 1 time in total
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
meslomp
PostPosted: Mar 01, 2012 - 06:38 AM
Raving lunatic


Joined: May 02, 2007
Posts: 3154
Location: Nieuwegein, Netherlands

Quote:
I am not sure what a "toogle" is?

after reading the thread I interpreted it as 'toggle'

OP please change the thread title if this is the case, then it will be clearer for other people what the topic is about.

regards

_________________
1)Datasheet and application notes checked?
2)tutorial forum
3)Newbie start here
 
 View user's profile Send private message  
Reply with quote Back to top
MASNSN
PostPosted: Mar 01, 2012 - 11:07 AM
Wannabe


Joined: Dec 31, 2011
Posts: 87


meslomp wrote:
and what is the question here?
the magic word is 'debounce'

and keep in mind that when you de-activate the relay that its voltage will invert and thus might destroy your chip if you switch the relay directly. not mentioning too much current drawn, so you will need a flyback switch to handle the relay.

if you had no clue what to do, you now have plenty of reading to do Wink


Thank you for the response. The english language is not my native language thank you for teaching me the new word "debounce" this will help me to google the issue with more precision. About switching the relay directly I found the solution I did a protection through a combination using resistors and diodes to prevent that anyway this is not the issue. The issue is the C code that I did is not performing what I really need to do, I used the famous bit_is_set(PINX,Y) where X is the PORT and Y is the pin number but it is toogling and not debouncing. About the documentation I'm now taking a look on similar projects in PIC and try to find the idea every problem has a solution
Laughing
 
 View user's profile Send private message  
Reply with quote Back to top
MASNSN
PostPosted: Mar 01, 2012 - 11:19 AM
Wannabe


Joined: Dec 31, 2011
Posts: 87


LDEVRIES wrote:
I am not sure what a "toogle" is?
After you either operate or release the relay, just execute a 10 ms. delay should fix your problem. Then read the references above!


Really sorry I mean toggle, I miss writed the word toggle. Thanks to some people in the forum here I will use the world debounce insead of toggle.
I need a program of button debouncing.

First press -> the relay close the circuit
Second press -> the relay release the ciscuit
Third press -> the relay close the ciscuit

And so on Of Corse the intervals between pressing the button is up to the user
 
 View user's profile Send private message  
Reply with quote Back to top
MASNSN
PostPosted: Mar 01, 2012 - 11:27 AM
Wannabe


Joined: Dec 31, 2011
Posts: 87


chartman wrote:
Simlpy compliment PB0 on every (debounced) button push..


I tried that but this will lead to another behaviour. For more explanation here is what you proposed
For example

1. You press the button a led is on
2. Once you release the button the led is off
3. You press the button and the led is on once again
and so on

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
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 01, 2012 - 11:41 AM
10k+ Postman


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

Quote:

Simlpy compliment PB0 on every (debounced) button push..

Notice he said "push". A button has two states push and release. In IBM PC keyboard terms this is also known as "make and "break". When you press/make a button it is the act of it transitioning from the "up" to the "down" state. That is when you compliment PB0. When you release/break a button it goes from the "down" to the "up" state - you simply ignore that transition (in this case).

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
kpanchamia
PostPosted: Mar 01, 2012 - 11:43 AM
Rookie


Joined: Jul 20, 2009
Posts: 48


Quote:
chartman wrote:
Simlpy compliment PB0 on every (debounced) button "push"..


MASNSN, Read again
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 01, 2012 - 12:29 PM
10k+ Postman


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

You guys have convinced this fellow that he didn't state his desire for a push-on push-off button with the correct word. He said it was like a toggle switch... two states... They have buttons like this on guitar pedals. But with software, you don't need the expensive hardware switch. You write the toggle in software. This has just about no relation to switch bounce. I posted what I hope is a clarifying example of how to make an output go on and off by repeated button presses. In summary, a software toggle is not the same as switch bounce suppresion.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Mar 01, 2012 - 12:48 PM
10k+ Postman


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

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)

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


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

You got it toyota. The last sw costs $4. The first one cost $.25. With clever software (courtesy of one AVRfreak BobGardner), the cheap sw behaves as the expensive one. SW approach has the advantage of being able to preset the switch in software... it can start out on or off as needed.

_________________
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 - 03:23 PM
Resident


Joined: May 01, 2003
Posts: 580


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.
 
 View user's profile Send private message  
Reply with quote Back to top
kk6gm
PostPosted: Mar 01, 2012 - 03:29 PM
Raving lunatic


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

bobgardner wrote:
In summary, a software toggle is not the same as switch bounce suppresion.

Right, but proper switch-triggered software toggling requires switch debouncing as a first step.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 01, 2012 - 03:53 PM
10k+ Postman


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

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

_________________
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 01, 2012 - 04:01 PM
10k+ Postman


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

You are both right. Doing the Bob-delay is one technique for switch debouncing".

Quote:

Not if the interval between switch reads is longer than the bounce interval

I'm a little uncomfortable with the wording "interval" here (as it could be interpreted as "the interval between two bounces". I think "duration" would be better.

So: If the interval between switch reads is longer than the longest possible duration of bouncing then that is a valid debouncing technique. Taking this to far, i.e. making the interval between switch reads veeery long you might end up missing actual switch closings. (E.g. reading a switch once per second you will have a substantial probability to miss presses with durations shorter than 0.5 seconds. Pick your delay time wisely.)
 
 View user's profile Send private message Visit poster's website 
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