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:
- 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 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:
It very important how the data is sent to the LCD and how the command is sent 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 the only LCD will work.
Just by making the ENABLE pin high will not work, you have to 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 itself 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 reading/write pin, we all know that for any device the basic functionality start with reading and write, reading the data and writing the data is a 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 understand that data should be written on the LCD screen and act accordingly.
Just sending the data and displaying it will not complete the task; the arrangement of data in an understandable way is an 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 the 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.
|If you want to display content in one line in the 5×7 matrix|
|If you want to display content in two lines in the 5×7 matrix|
|If you display 4-bit data in one line in the 5×7 matrix|
|If you display 4-bit data in two lines in the 5×7 matrix|
|To clear the display without clearing the ram content|
|Making the cursor on and also display on|
|Making the cursor off and also display off|
|Displaying the data on cursor blinking|
|Shifting complete display data to the left side|
|Shifting complete display data to the right side|
|Moving cursor to one place or one character left|
|Moving cursor to one place or one character RIGHT|
|Clearing the complete display including RAM DATA|
|Set DDRAM address on the cursor position|
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 a 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 to 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]!=’′) // 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]!=’′) // loop will go on till the NULL character in the string
LCD_write(str[i]); // sending data on LCD byte by byte