You are on page 1of 3

Lab No.

1 - I/O Ports Programming & LED Interfacing


Home Task:
If Bit 0 of PORTB is high, LEDs connected at PORTA will glow from LSB to MSB.
If Bit 0 of PORTB is low, LEDs connected at PORTA will glow from MSB to LSB. In
both
cases, one LED should be ON at a time.
CODE:#define F_CPU 1000000UL
#include <util/delay.h> // For delay function
#include <avr/io.h>
int main(void)
{DDRA = 0xFF; // Set Port A as output, Data Direction Register
DDRB = 0x00; // Set Port B as input, Data Direction Register
while(1) //Forever Loop
{
unsigned char x;
unsigned char msb[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char lsb[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
if(PINB&0b00000001)
{for(x=0;x<8;x++)
{PORTA=msb[x];
_delay_ms(250);}}
else
for(x=0;x<8;x++)
{ PORTA=lsb[x];
_delay_ms(250);}}}

You might also like