Friday 15 March 2013

How to interface a 16*2 LCD display with atmega8



Hi friends,Welcome to my  website! 


              I'm a student of electronics and communication systems. My real interest focus around Electronics  and Computer programming. I enjoy micro-controller and computer programming.I'm familiar with  micro-controllers, like 8051, AVR..I'm also interested in websites design especially blog template design..

Today we are going to discuss about the LCD interface with the atmega8 micro-controller.

Pin Configuration of ATMEGA8

16*2 LCD display

Schematic diagram of LCD interface with ATMEGA8(click on image-full size view )

Configurations

Here,PORTB(PB0-PB7) set as data port in 8 bit mode LCD display.Connect D0-D7 with the PORTB.

  • Ground the first lead of the lcd(vss) display.
  • Connect vcc to a 5v power supply.
  • Conncet VEE with a pot.It's used to adjust the contrast of the LCD display
 15th and 16th leads are used to power the back light of the LCD.Connect 15th with 5v and ground 16th lead.

C CODE

#define F_CPU 1000000
#include <avr/io.h>
void delay(unsigned char);

//Main Code
int main()
{
DDRB=0xff;    //set PORTB as out put
 
  DDRD=0b01110000;   //Set PD.4,5 and 6 as Output
 
/*
RS=PD5
           R/W=PD6
         
           E=PD4          */

  //Give Inital Delay for LCD to startup as LCD is a slower Device
  delay(2);

  init_lcd();

while(1)
    {

       
           
   lcd_cmd(0x80);       //Goto Line-1,first position
  lcd_send_string("WELCOME TO! ");

   lcd_cmd(0xC0);      //Goto Line-2, first position    

   lcd_send_string("EGLOB :) ");
   lcd_cmd(0x01);     //Clear the lcd
   delay(1);
      
}

//LCD function
/*------------------------------------------------------------------------------------------------------------*/

//Function for sending commands to LCD
void lcd_cmd(unsigned char command)

{
 

//Put command on the Data Bus
  PORTB = command;

  //Enable LCD for command writing
  PORTD = 0b00010000;

  //Allow delay for LCD to read the databus
  delay(1);

  //Disable LCD again
  PORTD = 0b00000000;

  //Allow some more delay
  delay(1);
}

//Function for sending Data to LCD

void lcd_data(unsigned char data)
{
  //Put data on Data Bus
  PORTB= data;


  //Set R/S (Regiter Select) to High, and Enable to High
  PORTD = 0b00110000;

  //Allow for delay
  delay(1);

  //Disable LCD again
  PORTD = 0b00100000;

  //Allow for some more delay
  delay(1);
}

//Function to send String to LCD
void lcd_send_string(char* string)
{
  while(*string)
{
  //Send value of pointer as data to LCD

  lcd_data(*string);
  //Increment string pointer
  string++;
  }
}




//Function to Initilise LCD
void init_lcd()
{
  //Setup both lines of LCD
  lcd_cmd(0x38);
  //Set Cursor off - Enable LCD
  lcd_cmd(0x0E);
  //Clear Screen
  lcd_cmd(0x01);
  //Goto first position
  lcd_cmd(0x80);
}
 /*----------------------------------------------------------------------------------------------------------*/

//Delay function

void delay(unsigned char dtime)
{
int i,j;
for(i=0;i<=dtime;i++)
{
for(j=0;j<5000;j++);
}}
//You can use your own delay functions and replace this delay function with your code
/*-----------------------------------------------------------------------------------------------------------*/


Feel free to ask your doubts..

ENJOY! 

7 comments:

  1. Thanks for sharing this post . i never face any problem with this LCD display from the date of purchase it

    ReplyDelete
  2. Hi,
    Thank for this simplified code. How to display long string?
    Thanks

    ReplyDelete
  3. nice explanation

    i will appreciate your work..thank you for your work

    ReplyDelete
  4. which compiler is used for lcd ??

    ReplyDelete
  5. R/S and RW needs to be swapped in the physical configuration of hardware in order to run this program.... great effort ... thanx.. it saved us a lot of time....keep up the good work....

    ReplyDelete
  6. i have problem when i upload to my device, atmega8. This is problem.

    avrdude.exe: set SCK frequency to 187500 Hz
    avrdude.exe: error: programm enable: target doesn't answer. 1
    avrdude.exe: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.


    avrdude.exe done. Thank you.

    To solve this problem, that should i do?

    ReplyDelete