Monday, 23 February 2015

Interfacing of led with avr


Led (  Light emitting diode) : Light Emitting Diodes are the mostly commonly used components in many applications. They are made of semiconducting material. This article describes about basic interfacing of LEDs to the 8051 family microcontrollers.


Interfacing of led with AVR Microcontroller:


Circuit diagram for blinking a led:




Program for blinking a led:

1) C code:

#include<avr/io.h>
#include<util/delay.h>
void main()
{

DDRA=0b11111111;
PORTA=0B00000001;
_delay_ms(1000);
PORTA=0B00000010;
_delay_ms(1000);
PORTA=0B00000100;
_delay_ms(1000);
PORTA=0B00001000;
_delay_ms(1000);
PORTA=0B00010000;
_delay_ms(1000);
PORTA=0B00100000;
_delay_ms(1000);
PORTA=0B01000000;
_delay_ms(1000);
PORTA=0B10000000;
_delay_ms(1000);

}



Alternate way:

#include<avr/io.h>
#include<util/delay.h>
int arr [8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void main()
{
while(1)
{
int i;
DDRA=0b11111111;
DDRB=0b11111111;
for(i=0;i<8;i++)
{
PORTA=arr[i];
PORTB=arr[7-i];
_delay_ms(500);
}
}
}

2) Proteus Simulation:






Program to switch on and off an 32 leds connected at PORTA, PORTB, PORTC,PORTD.


1) C code:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
while(1)
{
DDRA=0b11111111;
DDRB=0b11111111;
DDRC=0b11111111;
DDRD=0b11111111;
PORTA=0b11110000;
PORTB=0b00100000;
PORTC=0b01000000;
PORTD=0b11110000;
_delay_ms(5000);
PORTA=0b00001111;
PORTB=0b00000010;
PORTC=0b00000100;
PORTD=0b00001111;
_delay_ms(5000);
}
}

2) Proteus Simulation:







Program to moving 32 leds connected at PORTA, PORTB, PORTC,PORTD.


1) C code:

#include<avr/io.h>
#include<util/delay.h>
void main()
{
while(1)
{
int i;
DDRA=0b11111111;
DDRB=0b11111111;
DDRC=0b11111111;
DDRD=0b11111111;
PORTA=0b11110000;
PORTB=0b00100000;
PORTC=0b01000000;
PORTD=0b11110000;
_delay_ms(2000);
for(i=0;i<4;i++)
{
PORTA=PORTA>>1;
PORTB=PORTB>>1;
PORTC=PORTC>>1;
PORTD=PORTD>>1;
_delay_ms(1000);
}
}
}

2) Proteus simulation:








0 comments:

Post a Comment

if you have any doubt please let me know