LCD-LED Display

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.

Outline

  • Circuit Diagram of Interfacing 16X2 LCD to AVR Microcontroller:
    • Programming ATMEGA16 for Interfacing with 16X2 LCD:
    • Code for Interfacing the LCD to ATMEGA16:

Circuit Diagram of Interfacing 16X2 LCD to AVR Microcontroller:

Circuit Explanation:

  • This is similar to how the LCD is connected to an 8051 or PIC microcontroller. The microcontroller will receive its clock from the crystal oscillator. The capacitors attached to the crystal will operate as filters, assisting the crystal in resonating and oscillating in parallel mode.
  • The LCD’s contrast may be adjusted with the help of the potentiometer, which is attached to pins 3 and 2. The Register select, Read/write, and Enable pins of the LCD are connected to the PD0, PD1, and PD2 pins of the Atmega16. The LCD’s data pins are wired to the Atmega16’s pins 33 to 40.

Programming ATMEGA16 for Interfacing with 16X2 LCD:

You can learn more about LCDs in the essay Interfacing 162 with an 8051 microprocessor. As I previously stated, the fundamentals of programming are the same, with the exception of accessing the microcontroller’s pins and registers.

It is critical how data and commands are sent to the LCD. For example, if you are sending data to the LCD, you must set the ENABLE pin of the 162 LCD pin to low before sending the data, then set the ENABLE pin to high when you believe the data you want to send is ready, which is 1 in coding language. Only the LCD will work if the ENABLE pin is set to high.

Simply setting the ENABLE pin to high will not work; you must also set the REGISTER SELECT pin (RS pin) to high so that the LCD recognises that the data being displayed on the screen is normal. If you forget to set the RS pin to high, the LCD will assume that the user is issuing a command and will prepare itself to act in accordance with the command, such as moving the cursor, clearing the data on the LCD, and so on.

Last but not least, there is the read/write pin to consider. As we all know, any device’s basic capability begins with read and write; reading and writing data is the most basic and important function for any peripheral or system. When transferring data to the LCD for display, set the R/W pin low so that the LCD recognises that data should be written on the screen and responds appropriately.

Sending data and displaying it will not finish the work; the programmer’s most significant and crucial task is to arrange data in an understandable manner. You can arrange data in the LCD or make the LCD work according to your wishes by sending commands or special functions to the LCD. You might wonder what kind of commands are needed to make the LCD work, such as commands for cursor position, increasing or decreasing contrast, changing the cursor line like from first to second line, and so on. To transmit a command to the LCD, set the pins high and low in the same way that data is sent.

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.

SNOINSTRUCTION FOR LCDHEX CODE
1If you want to display content in one line in 5×7 matrix0x30
2If you want to display content in two line in 5×7 matrix0x38
3If you display 4 bit data in one line in 5×7 matrix0x20
4If you display 4 bit data in two line in 5×7 matrix0x28
5entry mode0x06
6To clear the display without clearing the ram content0x08
7Making the cursor on and also display on0x0E
8Making the cursor off and also display off0x0C
9Displaying the data on cursor blinking0x0F
10Shifting complete display data to left side0x18
11Shifting complete display data to right side0x1C
12Moving cursor to one place or one character left0x10
13Moving cursor to one place or one character RIGHT0x14
14Clearing the complete display including RAM DATA0x01
15Set DDRAM address on cursor position0x80+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;
}

Tags

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button
Close
Close