Search
Close this search box.

Home

Interfacing 16X2 LCD to AVR Microcontroller

This session completely deals with the interfacing AVR microcontroller (ATMEGA 16) with 16X2 LCD. The Atmega16 belongs to the AVR microcontroller family.

Circuit Diagram of Interfacing 16X2 LCD to AVR Microcontroller:

Interfacing 16X2 LCD to AVR Microcontroller Circuit Diagram

Circuit Explanation:

  • Well this is not different from the way interfacing the LCD to 8051 or PIC microcontroller. The crystal oscillator will provide the clock to the microcontroller. The capacitors connected to the crystal will act as filters and help the crystal to resonate and oscillates to its parallel resonate mode.
  •  The potentiometer which is connected to the pin 3 and pin2 of LCD will help to adjust the contrast of the LCD. The 4, 5 and 6 pins of LCD that is Register select, Read/write and Enable pins are connected to the PD0, PD1 and PD2 pins of Atmega16. The data pins of LCD are connected to the pins of 33 to 40 pins of Atmega16.

Programming ATMEGA16 for Interfacing with 16X2 LCD:

You can get the brief information of LCD from the post Interfacing 16×2 from 8051 microcontroller. As I said earlier programming basic is all same expect using the pins and the registers of the microcontroller.

It very important how the data is send to the LCD and how the command is send to the LCD, suppose if you are sending data to the LCD, then you have to make the ENABLE pin of 16×2 LCD pin to low before sending the data, when you think the data you want send is ready make the ENABLE pin again high that is 1 in coding language. If you make ENABLE pin high then only LCD will work.

 Just by making the ENABLE pin high will not work, you have make REGISTER SELECT pin (RS pin) also high so that LCD will accept that it a normal data which has to be displayed on the screen of LCD, if you forgot to make RS pin high it eventually think that user is sending it a command and make it self ready to act according to the command like making cursor to move, clearing the data on the LCD, changing the cursor position etc.

 Last but not least another pin you need to worry of read/write pin, we all know that for any device the basic functionality start with read and write, reading the data and writing the data is main and important function for any peripheral or system. here in the LCD while sending the data for displaying you have to make the R/W pin low, so that LCD will under stand that data should be written on the LCD screen and act accordingly.

Just sending the data and displaying it will not complete the task; arrangement of data in understandable way is the important and crucial task for the programmer. You can arrange the data in the LCD or making the LCD to work according to your wish, can be done by sending the commands or special functions to the LCD, you may think that what type of commands are needed to work for LCD, commands for cursor position, increasing or decreasing the contrast, making the cursor to change line like from first line to second line etc.  To send a command to the LCD you need to make pins high and low just like sending the data. For sending the command you need to make the ENABLE PIN high, REGISTER SELECT pin (RS pin) low that is 0 in programmer terms, and read/write pin (R/W pin) high, you need to remember this configuration for sending the command.

Different commands and there hexadecimal code generally used by the programmer while displaying the data.

SNO
Instruction for LCD
Hex code
1
If you want to display content in one line in 5x7 matrix
0x30
2
If you want to display content in two line in 5x7 matrix
0x38
3
If you display 4 bit data in one line in 5x7 matrix
0x20
4
If you display 4 bit data in two line in 5x7 matrix
0x28
5
entry mode
0x06
6
To clear the display without clearing the ram content
0x08
7
Making the cursor on and also display on
0x0E
8
Making the cursor off and also display off
0x0C
9
Displaying the data on cursor blinking
0x0F
10
Shifting complete display data to left side
0x18
11
Shifting complete display data to right side
0x1C
12
Moving cursor to one place or one character left
0x10
13
Moving cursor to one place or one character RIGHT
0x14
14
Clearing the complete display including RAM DATA
0x01
15
Set DDRAM address on cursor position
0x80+add

If we want to talk in brief for displaying data in LCD

  • E=1; enable pin should be high
  • RS=1; Register select should be high
  • R/W=0; Read/Write pin should be low.

For sending command to LCD

  • E=1; enable pin should be high
  • RS=0; Register select should be low
  • R/W=1; Read/Write pin should be high.

When you are passing a string, its better use a string pointer and increment the pointer, if you are incrementing a pointer it will automatically go the next address of the variable in which you can store your character which you wanted to display. See the below example.

void write_string(unsigned char *str)   //store address value of the string in pointer *str

{

int i=0;

while(strng[i]!=’\0′)  // loop will go on till the NULL character in the string 

               {
                              lcd_write(strng[i]);// sending data on LCD byte by byte
                              i++;
               }
               return;

}

Code for Interfacing the LCD to ATMEGA16:

LCD DATA port----PORT B
signal port------PORT D
               rs-------PD0
               rw-------PD1
               en-------PD2
*/

#define LCD_DATA PORTB                //LCD data port
#define ctrl PORTD
#define en PD2                         // enable signal
#define rw PD1                       // read/write signal
#define rs PD0                     // register select signal

void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);

int main()
{
DDRB=0xff;                                  // setting the port B
DDRD=0x07;                                // setting for port D
init_LCD();                                 // initialization of LCD
_delay_ms(50);                        // delay of 50 mili seconds
LCD_write_string(“hello world”);                      // function to print string on LCD
return 0;
}

void init_LCD(void)
{
LCD_cmd(0x38);                            // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
LCD_cmd(0x01);                                 // clear LCD
_delay_ms(1);
LCD_cmd(0x0E);                        // cursor ON
_delay_ms(1);
LCD_cmd(0x80);                     // —8 go to first line and –0 is for 0th position
_delay_ms(1);
return;
}

void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return;
}

void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return ;
}

void LCD_write_string(unsigned char *str)             //store address value of the string in pointer *str
{
int i=0;
while(str[i]!=’\0′)                               // loop will go on till the NULL character in the string
{
LCD_write(str[i]);                            // sending data on LCD byte by byte
i++;
}
return;
}

3 Responses

  1. Well said! – I read about this concept in wiki and I felt that the information provided in this post has good stuff as compared to wiki. thanks!

  2. and code…

    #include
    #include

    #define F_CPU 1000000UL

    #define LCD_DATA PORTD // data port
    #define ctrl PORTC // controle port
    #define en PC3 // enable port and pin
    #define rw PC2 // read/write port and pin
    #define rs PC1 // register select port and pin

    void LCD_cmd(unsigned char cmd);
    void init_LCD(void);
    void LCD_write(unsigned char data);
    void LCD_write_string(char *str);

    int main()
    {
    DDRD=0xff; // setting output the port D
    DDRC=0b00001110; // setting output for port C

    init_LCD(); // initialization of LCD
    _delay_ms(100); // delay of 50 mili seconds
    LCD_write_string(“Andrej!!!111”); // function to print string on LCD

    while(1)
    {
    }
    return 0;
    }

    void init_LCD(void)
    {
    LCD_cmd(0x01); // clear LCD
    _delay_ms(1);
    LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
    _delay_ms(1);
    LCD_cmd(0x0E); // cursor ON
    _delay_ms(1);
    LCD_cmd(0x80); // —8 go to first line and –0 is for 0th position
    _delay_ms(1);
    return;
    }

    void LCD_cmd(unsigned char cmd)
    {
    LCD_DATA=cmd;
    ctrl =(0<<rs)|(0<<rw)|(1<<en);
    _delay_ms(1);
    ctrl =(0<<rs)|(0<<rw)|(0<<en);
    _delay_ms(50);
    return;
    }

    void LCD_write(unsigned char data)
    {
    LCD_DATA= data;
    ctrl = (1<<rs)|(0<<rw)|(1<<en);
    _delay_ms(1);
    ctrl = (1<<rs)|(0<<rw)|(0<<en);
    _delay_ms(50);
    return ;
    }

    void LCD_write_string(char *str)
    {
    int i=0;
    while(str[i]!='') // loop will go on till the NULL character in the string
    {
    LCD_write(str[i]); // sending data on LCD byte by byte
    i++;
    }
    return;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *