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?
1 Answer 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
}
-
That looks like it worked. But now I have to ask why would it work on Port C as the way I had it?EE_David– EE_David08/31/2016 17:52:07Commented Aug 31, 2016 at 17:52
-
1Sorry, 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.EE_David– EE_David08/31/2016 18:25:10Commented Aug 31, 2016 at 18:25
-
I was puzzled by that too. Glad you figured it out. Best of luck,Gerben– Gerben08/31/2016 18:53:56Commented Aug 31, 2016 at 18:53