1

I'm using an Atmega328p SMD with 32 pins. I'm trying to get several pins on Port D to function as inputs. As outputs, they work fine, but for some reason, not as inputs. Here's a simple example of what I'm dealing with.

void setup() {
DDRB |= (1<<PB3); //Set up an output on PB3 for an LED
DDRD = B00000000; //All of Port D set as inputs
DDRC = B00000000; //All of Port C set as inputs
}
void loop() {
int Power = (PIND & (1 << PORTD5)); //Read input
 if(Power == 1){
PORTB |= (1 << PORTB3); //set high
 }
 else{
 PORTB &= ~(1 << PORTB3); // set low 
} 

I get no response from my LED like this, no matter which pin on Port D I use as an input. But when I simply change over to Port C, it works fine.

int Power = (PINC & (1 << PORTC5)); //Read input

Ideas?

asked Aug 31, 2016 at 17:40

1 Answer 1

1

You check if power is 1, but it will never be 1. Instead it will be 1<<5 or 32. Better use:

if(Power != 0){
 PORTB |= (1 << PORTB3); //set high
}
else{
 PORTB &= ~(1 << PORTB3); // set low 
}
answered Aug 31, 2016 at 17:45
3
  • That looks like it worked. But now I have to ask why would it work on Port C as the way I had it? Commented Aug 31, 2016 at 17:52
  • 1
    Sorry, I figured that out. I was using PC0, so I guess that would equal 1. Sorry for the confusion in the example by using PC5, I didn't think that was an issue. Commented Aug 31, 2016 at 18:25
  • I was puzzled by that too. Glad you figured it out. Best of luck, Commented Aug 31, 2016 at 18:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.