Society of Robots - Robot Forum
Software => Software => Topic started by: tristantech on January 01, 2009, 01:42:19 PM
-
Hi,
I'm using an atmega88 microcontroller. I have a pushbutton switch connected to pin 24, which is PC1. the button's other side is wired to ground am i'm using the avr's internal pullup resistors. Here is my code:
while( (PINC & _BV(PC1)) == 1 )
{
}
This while loop pauses the program until the button is pressed, but the program does not pause. The hardware is correct and the inputs are configured correctly.
Can anyone tell me what I'm doing wrong???
-
1 is usually High, while 0 is Low. When you push the button, the pin connected to the button will go low.
Also, put your code that you want to happen the the button is pushed inside the while loop.
-
You're problem is contact bounce!
When you close the switch - the contact in the switch (being mechanical) bounces up and down for a while before settling so you get a pseudo random of highs and lows whilst the switch is bouncing before it settles down. So you need to keep reading Pin:C1 to make sure you keep getting high values - say 255 high values.
-
yea, probably a debounce problem as Webbot said . . . i was originally thinking compiler optimization was just ignoring it . . . either way, make this change:
while( (PINC & _BV(PC1)) == 1 )
{
delay_ms(15);//a button bounce is usually never more than a few ms
}
-
Here is the problem: (I eventually figured it out)
The problem wasn't contact bounce. (I left out the delay code for simplicity) It was in the software, specifically the "&" bitwise operator.
I had another button wired to PC0, which worked fine.
In the code, the input had to be 1 to keep the loop going... but it should have been not = 1 . like this:
while((PINC & _BV(PC1)) != 0)
{
}
-
'=' and '==' are two different things!
!=0 and ==1 are not the same
-
oops. I made a typo I meant to say not = 0. But the code I posted is still correct