/* port_test4.c
 * This is a program which checks the 0th pin of port D for input and if it is ON, keeps writing 1 to 0th pin of port B 
 * else if pin 0 of port D is set, it toggles pin 0 of port B
 */

#include <avr/io.h> //standard include for ATMega16
#define sbi(x,y) x |= _BV(y) //set bit
#define cbi(x,y) x &= ~(_BV(y)) //clear bit
#define tbi(x,y) x ^= _BV(y) //toggle bit
#define is_high(x,y) ((x & _BV(y)) == _BV(y)) //check if the input pin is high
#define is_low(x,y) ((x & _BV(y)) == 0) //check if the input pin is low

int main(void)
{
    unsigned int i;
    unsigned int j;
    DDRB=0xff; //PORTB as OUTPUT
    PORTB=0x00;
    DDRD=0x00; //PORTD as INPUT
    PORTD=0xff; //Enable Pull-up on the input port

    while(1==1) //Infinite loop
    {

        for(i=0;i<2;i++)
        {
            if(i==0)
            {
                sbi(PORTB,PB0);
            }
            else if(i==1)
            {
                if(is_low(PIND,PD0))
                {
                    cbi(PORTB,PB0);
                }
                else
                {
                    sbi(PORTB,PB0);
                }
            }
            for(j=0;j<65535;j++); //The 'for' loops are only for delay
            for(j=0;j<65535;j++);
        }
    }
    return 0;
}