I am trying to get a simple button (GPIO shortcircuit) to work. I have chosen Pin D0 as input and Pin D7 as output. It works fine in an Arduino sketch but I can't get it to run in pure C. What am I doing wrong? I bet I got the bits all wrong...
My code:
DDRD = 0b11111110; // set all port D pins to Output besides pin 0, which is set to input
PORTD = 0b11111111; // set all D pins to WRITE / PULL-UP
int a = PIND & 1; // a is 0 or 1, depending on whether pins D0 and D7 are physically connected
It doesn't work though, a
is never zero, whether the pins are connected or not.
Here is my complete code sample, where the on board LED depends on my pins connection state:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= _BV(DDB5);
DDRD = 0b11111110;
PORTD = 0b11111111;
while(1) {
int a = PIND & 1;
if(a != 0)
PORTB |= _BV(PORTB5); // blink on
else
PORTB &= ~_BV(PORTB5); // blink off
_delay_ms(1000);
}
}