I am trying to read the pin PB0 which is connected to internal pull-up resistor. In my code logic,
- if PB0 is HIGH then execute if-block
- if PB0 is LOW then execute else-block
But when I connect PB0 to GND pin, it is still executing the if-block.
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
//For testing
DDRD = 0b11111111;
PORTD = 0b00000000;
/*
OLD CODE
DDRB |= (0<<PB0|1<<PB1);// PB0 -input, PB1 -output
PORTB |= (1<<PB0|0<<PB1);//activated pullup in PB0
*/
//CORRECTED CODE
DDRB |= (1 << PB1); // PB1 -output
DDRB &= ~(1 << PB0); // PB0 -input
PORTB |= (1 << PB0); // Activated pullup in PB0
PORTB &= ~(1 << PB1); // PB1 low
while(1)
{
_delay_ms(100);
if( PINB & (1<<PB0))
{
PORTD = 0x00;
}
else
{
PORTD = 0xFF;
}
_delay_ms(500);
}
}
EDIT : There was an error in my bitwise operation. Corrected and mentioned as NEWCODE. Kept the old code as OLD CODE for reference. But, the code is still not working as it should when I connect PB0 to GND.