Tuesday, September 29, 2015

PROJECT 2b: Interfacing LCD 16x2 with AVR


Hey Guys,

Today lets discuss a basic output device. A LCD(Liquid Crystal Display) for your AVR's.
Its a nice and easy tutorial where you can learn to interface an LCD and display your bot status or any serially , Wireless received messages or anything from your ucontroller.

Soon i'll also share with you the files on how to do it on our 3 other favorite platforms:
1) Arduino
2) 805x series.
3) PIC controller


Lets look at the basic working of an LCD module. It receives data in ASCII format.


PIN DIAGRAM  and their uses:

VDD- +5v
VEE- power supply to contrast using a potentiometer
VSS- ground

RS-Register select (0=command code     1 = data word)

R/W-Read/Write    (0 = writing         1 = reading)

E- Enable (High to low pulse to latch data)

D0(lsb) to D7(msb) are data bits



Fig. A Basic LCD and its pins.




Some handy commands we will use:

0x01= clear screen
0x02= return home
0x04= cursor 1 place left
0x06= cursor 1 place right
0x05 shift display left
0x07 shift display right
0x0E= display on , cursor blinking
0x0f = display on , cursor blinking
0x80= force cursor to beginning of first line
0xC0= force cursor to beginning of second line
0x28= 2 lines 5x7 matrix(D4-D7, 4 bit data)
0x38= 2 lines 5x7 matrix(D0- D7, 8 bit data)

so suppose the LCD data bits D0 to D7 are connected to port C.1 to C.7

RS= PORTB.0

R/W= PORTB.1

Enable pin=PORTB.2

VDD- +5v

VEE- power supply to contrast using a potentiometer

VSS- ground


Below is the  basic circuit and different states of LCD on running the below program.




burn the demo code below to your AVR USING PROGRAMMER's Notepad and enjoy making minor twitches and interfacing your lcd with AVR.

*****************************************************************************************************


void delay_ms(unsigned int c)
{
_delay_ms(c);
}



void comm_write (unsigned char a)
{
   PORTD=0x01;
PORTC=a;
PORTB &= ~(0x03);
PORTB|= (1<<2);
delay_ms(2);
PORTB &= ~(1<<2);
delay_ms(100);
PORTD&=~(0X01);
}

void data_write(char* str )
{
unsigned char i=0;
while (str[i] !=0)
{
PORTC=str[i];
PORTB = PORTB & ~(0X02);
PORTB = PORTB |(0x01);
PORTB|=(1<<2);
delay_ms(200);
PORTB= PORTB & ~(1<<2);
delay_ms(200);
PORTB = PORTB & ~(0X03);
i++;
if (i>15)
{
comm_write(0x07);
}
}
}

void init_lcd()
{
DDRB=0Xff;
DDRC=0Xff;
DDRD=0x03;
PORTB &= ~(1<<2);
PORTD|=(1<<2);
delay_ms(2000);
PORTD&=~(1<<2);
comm_write(0x38);
comm_write(0x0E);
comm_write(0x01);
delay_ms(2000);
comm_write(0x06);
data_write("Abhirup.");
comm_write(0xC0);
data_write("welcome to my tutorials");

}

int main(void)
{
init_lcd();
return 0;
}



*************************************************************************************************

Have fun playing around. And feel free to ask questions around the circuit or code or its scope of use.

keep reading :)
Abheerup

No comments:

Post a Comment