In this post I will walk you through on how to write a program onto your AVR microcontroller to display your name on a 16x2 or 16x1 LCD. You do not require a lot of components or soldering. for simplicity, I used a female to female connector to connect the lcd to the microcontroller. I have soldered a header pin for the LCD module.
First let us understand the LCD pin configuration.It has 16 pins. and works on parallel communication.one must also remember that LCD's are slow compared to the microcontrollers we use. hence we have to use a lot of delay(in order of ms not minutes :-P). among the 16 pins, there are three important pins, namely R/S,E,R/W which are addressed as Register Select, Enable,Read or Write respectively.To make it simple, in this post let us connect R/W to ground. which brings the lcd in write mode. so now we have 2 control pins namely RS and E and 8 data pins from D0-D7 to send parallel data. we also have two pins for power supply and two for LED backlight. before I tell the pin number of the above, I will mention the last pin which is also very important and it is called contrast.a voltage divider is constructed for this pin to adjust the contrast.
LCD Pin configuration:
- Gnd
- VCC
- contrast
- RS
- RW
- E
- D0
- D1
- D2
- D3
- D4
- D5
- D6
- D7
- LED+
- LED-
Circuit construction:
The connections are simple. This can be implemented for 8051 or any other Avr microcontrollers with little or no modifications. Here I am using an atmega 8. It is a 28 pin IC. To write data I will be using port B, because it is the only 8 bit port available in atmega 8. for RS,RW,E I used port D 4,5,6 pins respectively.connect a potentiometer to control the contrast to pin 3 of the LCD. if you want backlight, connect the pin 15 and pin 16 of lcd to powersupply. while connecting port B with LCD make sure that PB.0 is connected to D0 and PB.7 is connected to D7. please refer the datasheet for pin details.I havent used any external crystal as the external crystal has to be connected to port B and I require the port B. the AVR microcontrollers have a built in 1Mhz clock so it works fine without an external crystal.
list of commands
CODE:
lcd.c
/*
* lcd.c
*
* Created: 02-05-2014 10.35.07 PM
* Author: Jayanth
*/
/*
D5 RS
D6 RW
D7 EN
PORTB Data
*/
#include <avr/io.h>
#include <util/delay.h>
int i;
char a[16]="hello world!";
char b[16]="By Jayanth";
void lcd_init (void)
{
DDRB=0xff;//data pins
DDRD=0xff;//control pins
_delay_ms(5);
PORTB=0x38;// function set: 2 line, 8 bit mode
cmd();
PORTB=0x0F;//Display : on,Cursor: on,Cursor Blink: on
cmd();
PORTB=0x0C;//cursor invisible
cmd();
PORTB=0x06;//Increment mode
cmd();
PORTB=0x01;//clr screen
cmd();
PORTB=0x02;// return home
cmd();
}
void cmd(void)
{
PORTD=0x80;
_delay_ms(5);
PORTD=0x00;
_delay_ms(5);
}
void data(void)
{
PORTD=0xA0;
_delay_ms(5);
PORTD=0x20;
_delay_ms(5);
PORTD=0x00;
}
int main(void)
{
lcd_init();
for(int i=0; i<=11; i++)
{
PORTB=a[i];// loop to display first line
data();
}
PORTB=0xc3;//second line + address
cmd();
for(int i=0; i<=9; i++)
{
PORTB=b[i];// loop to display second line
data();
}
}
Note: if you have any problems with the util/delay.h header file, create a new delay function using a for loop to create some delay.
Comments
Post a Comment