Unable to read the input pin in AVR ATMEGA328p

Thread Starter

praveenmax

Joined Apr 26, 2016
14
Hello all,
I am trying to read the pin PB0 which is an internal pull-up resistor input pin. 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. Since I have connected some LEDs in PORTD which wont turn ON in IF block, I am sure that it is executing IF block.

Code:
#include<avr/io.h>
#include<util/delay.h>

int main(void)
{
     //For testing
     DDRD =0b11111111;
     PORTD =0b00000000;

     //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);
      }
}
Moderator edit: added code tags.
 

MrChips

Joined Oct 2, 2009
30,713
Remove the PINB test.
Can you make the LEDs on PORTD flash (with suitable delays inserted into the code)?
Code:
while(1)
{
  PORTD = 0;
  _delay_ms(500);
  PORTD = 0xFF;
  _delay_ms(500);
}
 
Top