Society of Robots - Robot Forum

Software => Software => Topic started by: tristantech on January 01, 2009, 01:42:19 PM

Title: AVR input trouble
Post 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:

Code: [Select]
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???
Title: Re: AVR input trouble
Post by: pomprocker on January 01, 2009, 03:14:03 PM
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.
Title: Re: AVR input trouble
Post by: Webbot on January 01, 2009, 04:51:07 PM
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.
Title: Re: AVR input trouble
Post by: Admin on January 09, 2009, 10:06:24 PM
yea, probably a debounce problem as Webbot said . . . i was originally thinking compiler optimization was just ignoring it . . . either way, make this change:

Quote
while( (PINC & _BV(PC1)) == 1 )
      {
      delay_ms(15);//a button bounce is usually never more than a few ms
      }
Title: Re: AVR input trouble
Post by: tristantech on January 10, 2009, 10:37:17 PM
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:

Code: [Select]
while((PINC & _BV(PC1)) != 0)
{

}
Title: Re: AVR input trouble
Post by: pomprocker on January 14, 2009, 11:23:25 AM
'=' and '==' are two different things!


!=0 and ==1 are not the same
Title: Re: AVR input trouble
Post by: tristantech on January 15, 2009, 08:05:57 PM
oops. I made a typo I meant to say not = 0. But the code I posted is still correct