Author Topic: AVR input trouble  (Read 3795 times)

0 Members and 1 Guest are viewing this topic.

Offline tristantechTopic starter

  • Jr. Member
  • **
  • Posts: 34
  • Helpful? 0
AVR input trouble
« 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???
Hey! Visit my website @ http://www.tristantech.net

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: AVR input trouble
« Reply #1 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.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,166
  • Helpful? 111
Re: AVR input trouble
« Reply #2 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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: AVR input trouble
« Reply #3 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
      }

Offline tristantechTopic starter

  • Jr. Member
  • **
  • Posts: 34
  • Helpful? 0
Re: AVR input trouble
« Reply #4 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)
{

}
Hey! Visit my website @ http://www.tristantech.net

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: AVR input trouble
« Reply #5 on: January 14, 2009, 11:23:25 AM »
'=' and '==' are two different things!


!=0 and ==1 are not the same

Offline tristantechTopic starter

  • Jr. Member
  • **
  • Posts: 34
  • Helpful? 0
Re: AVR input trouble
« Reply #6 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
Hey! Visit my website @ http://www.tristantech.net

 


Get Your Ad Here

data_list