go_away

Author Topic: How to check AVR pin status?  (Read 7477 times)

0 Members and 1 Guest are viewing this topic.

Offline AKnickolaiTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
How to check AVR pin status?
« on: February 20, 2010, 01:18:55 PM »
I am working on a maze navigation robot that moves through the maze using switches to make sure the robot keeps the wall of the maze on the right side of it at all times.  Basically, all the switches are acting as is a push button that requires an action.

Here is my test code that lights an LED if the button is pushed

Code: [Select]
int temp;
while(1)
{
temp = (PINB & 0x01);
if(temp == 0)
{
PORT_ON(PORTB, 7);
}
else
{
PORT_OFF(PORTB, 7);
}
}

That's great if you want one switch, I'm planning on at least 3 or 4.  The problems is this statement:

Code: [Select]
temp = (PINB & 0x01);
Is actually comparing the status of all B pins to my condition 0x01.  As I add more switches, that logic comparison is going to get ugly unless I change my approach.

Is there any way I can check the status of an individual pin to see if it is on/off instead of the whole row?  I'm using an atmega16 and modified $50 robot code.

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: How to check AVR pin status?
« Reply #1 on: February 20, 2010, 01:22:22 PM »
Use
Code: [Select]
if(bit_is_set(PINB,0))
{
//do stuff
}

the 0 can be replaced by whichever pin number, 0 through 7. The bit is set returns true if the pin is high. bit_is_clear will return true if the pin is low. You can combine this for multiple switches by using the AND operator (&&), or the OR operator (||).

Offline AKnickolaiTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: How to check AVR pin status?
« Reply #2 on: February 20, 2010, 01:33:05 PM »
Perfect!  Thanks, that will get me rolling  ;D

 


Get Your Ad Here

data_list