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:
- Programming ATMEGA16 for Interfacing with 16X2 LCD:
- Code for Interfacing the LCD to ATMEGA16:
Circuit Diagram of Interfacing 16X2 LCD to AVR Microcontroller:
- 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.
|SNO||INSTRUCTION FOR LCD||HEX CODE|
|1||If you want to display content in one line in 5×7 matrix||0x30|
|2||If you want to display content in two line in 5×7 matrix||0x38|
|3||If you display 4 bit data in one line in 5×7 matrix||0x20|
|4||If you display 4 bit data in two line in 5×7 matrix||0x28|
|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
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
Code for Interfacing the LCD to ATMEGA16:
LCD DATA port----PORT B
signal port------PORT D
#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 LCD_write(unsigned char data);
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
LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
LCD_cmd(0x01); // clear LCD
LCD_cmd(0x0E); // cursor ON
LCD_cmd(0x80); // —8 go to first line and –0 is for 0th position
void LCD_cmd(unsigned char cmd)
void LCD_write(unsigned char data)
ctrl = (1<<rs)|(0<<rw)|(1<<en);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
void LCD_write_string(unsigned char *str) //store address value of the string in pointer *str
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