Orangutan SVP-324 Robot Controller

can any one pls tell me how to read a pin value of atmel 324 using c language.
I try PINx & _BV(n) code. bt it didnt work.

eg. if((PIND & _BV(3))==1) //read PD3 pin
what is the correct command???

Hi,

Your close but the “== 1” is wrong. PIND & _BV(3) equates to _BV(3) which is 8. It should be:

if((PIND & _BV(3)) == _BV(3)) //read PD3 pin

This is another way to do it:

if (PIND & _BV(3))
{
    // This executes if bit 3 is high
}
else
{
    // This executes if bit 3 is low
}

Mike

I still have the problem
I program the robot board using AVR studio (gcc compiler). this is my code

#include <pololu/orangutan.h>
#include <avr/io.h>
int main()
{
clear(); // clear the LCD
set_digital_output(IO_D3,HIGH);
if(PIND & _BV(3))
{
print(“high”); // display if pin PD3 is high
}
else{
print(“low”);
}
}

but I always get the output as low in the LCD. Can u pls tell me what is the wrong in this code

Hi,

First please use the code tag (click Code when entering your reply code text) to surround your code, it makes it easier to read.

The call to set_digital_output configures D3 as an output pin. I don’t think reading PIND will show the state of the output pin. Try using PORTD to read the state of the output pin.

Mike

Hello.

You should be able to read the input value of an I/O pin even when it is configured as an output (though it’s a little strange to do this since your program should know the voltage on the pin without having to measure it). Do you have anything connected to pin PD3?

There are a few potential problems I see with your program. First, you should never let program execution reach the end of your program because unpredictable things can happen. The main body of your program should contain an infinite while loop, even if the loop doesn’t have any code in it. Second, there’s a remote chance that you are checking the pin value too quickly after driving the pin high, and that the voltage doesn’t have time to rise enough before you perform this check. This problem tends to show up more when performing checks after enabling the internal pull-up.

Also, I notice you are using the Pololu AVR library to set the state of the pin, but you aren’t using it to read the input value of the pin. Is there a reason you don’t want to use the is_digital_input_high() function?

Can you try running the following code (I didn’t compile or test it, so there could be some minor syntax errors):

#include <pololu/orangutan.h>
int main()
{
  set_digital_output(IO_D3,HIGH);

  while (1)
  {
    delay(20);
    clear(); // clear the LCD
    if (is_digital_input_high(IO_D3))
      print("high");
    else
      print("low");
  }
}

Note that you should make sure there is nothing connected to pin PD3 when you run this program! If you want to see something more interesting, change the first line of main() to set IO_D3 as a digital input and then use a wire to connect PD3 to ground or Vcc. You should see the LCD text change depending on what it’s connected to.

Please let me know if this program works for you.

- Ben

Thank you for your reply,
but i cant use is_digital_input_high(IO_D3) function in the pololu avr library because i want to check more than one condition in the if statement

eg. if(pin D3==1 && pin D2==0 && …)

are there any function to check whether the input or output is low. That’s why i try to use PINx command. So can you please give me a code to solve this problem.

Why do you think “PINx commands” are any better/different than a call to is_digital_input_high()? For example, there’s no reason why you can’t do something like the following:

if (is_digital_input_high(IO_D3) && !is_digital_input_high(IO_D2))

Note that is_digital_input_high() returns 0 when the input is low and 1 when the input is high, so it can be used to detect both conditions. The above if-statement is carried out if PD3 is high and PD2 is low.

If it makes you feel any better, you can always make your macro that equates to 1 when the pin is low:

#define is_digital_input_low(x)  (!is_digital_input_high(x))

However, the more pressing concern should first be to get something simple working. Does the code from my previous post work for you?

- Ben

yes code is working quite well. thank you for your help :slight_smile: