You Are Here: Home » AVR ATmega Projects » LCD Projects » Interfacing 16X2 LCD to AVR Microcontroller

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 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.

LED-Interfacing-with-8051-150x150

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.

Interfacing 16X2 LCD to AVR Microcontroller

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 ;

 

For more detail: Interfacing 16X2 LCD to AVR Microcontroller

Leave a Comment

You must be logged in to post a comment.

Read previous post:
Water Level Indicator
Water Level Indicator

The Water Level Indicator employs a simple mechanism to detect and indicate the water level in an overhead tank or...

Close
Scroll to top