16x2 LCD Interfacing With AVR

Embedded Systems With AVR Tutorial Series

16X2 LCD Interfacing With AVR ATmega16

Displaying TEXT on LCD..!!

 

Contributed By:- Mitanshu Kumar

AVR_CLASSES_IN_BHOPAL1.jpg

--------------------------------------------------------

LCD (Liquid-Crystal Display)

A liquid-crystal display (LCD) is an electronically modulated optical device or simply say an electronic display module that uses the light-modulating properties of liquid crystals to display data on screen. In LCD, characters displays on screen by changing the orientation of Poly-silicon molecules (Liquid- Crystal) by means of electric field across them.

 

Since LCD panels produce no light of their own, they require external light to produce a visible image of characters to be displayed. Due to this reason, we use LEDs as light source (Backlight) in LCDs to produce visible image of characters. Backlights illuminate the LCD from the side or back of the display panel.

 

Generally, we prefer LCDs over other seven segments and multi-segment LEDs because there are no limitations of displaying data on LCD. We can display any numeric value, any alphabetic character, special & even custom characters and animations, etc. on LCDs.

 

LCDs are used in a wide range of applications including computer monitors, televisions, instrument panels, aircraft cockpit displays, and indoor and outdoor signage.

A 16x2 means (16 columns and 2 rows), it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5x7 pixel matrix. This LCD has two registers, namely, Command and Data.

 

The command register stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing it, clearing its screen, setting the cursor position, controlling display etc. The data register stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.

 

LCD displays also have two RAMs, naming DDRAM and CGRAM. DDRAM registers in which position which character in the ASCII chart would be displayed. Each byte of DDRAM represents each unique position on the LCD display. The LCD controller reads the information from the DDRAM and displays it on the LCD screen. CGRAM allows user to define their custom characters. The user defined custom characters stores in CGRAM.

 

*Here is the general PIN description of an 16x2 LCD:-

 

 AVR_CLASSES_IN_BHOPAL3.jpg

 

PIN1 (VSS):- This PIN is used to supply GND to LCD screen.

PIN2 (VDD):- This PIN supplies +3V or +5V to LCD screen.

PIN3 (VEE):- Contrast Adjustment, through variable resistor. With the help of this PIN, we can control the Brightness of display Characters.

PIN4 (RS):-   This Pin gives the information to LCD that the input value is Command or Data. To input the Command, RS should be at LOW (0) and for Data Input; it should be at HIGH (1).

PIN5 (RW):- This pin instructs the LCD to Write or Read. If we apply Low (logic0) to RW pin, LCD gets the instruction to write and if we apply HIGH (logic1) to RW pin, LCD will Read the stored value in it.

PIN6 (E):- E means Enable. LCD sends Instructions to memory only when a High to Low pulse is given to this pin.

PIN7 to PIN14 (D0- D7):- Data Pins.

PIN15 and PIN16 (LED Power Supply):- These Pins are used to supply 5V power to LEDs.

 

  • Display Character Address Code: - (All the codes are in Hex value)

As explained earlier, there are 16x2 positions/addresses of DDRAM in LCD to display and store characters. Also, there is 64 Bytes CGRAM space to store custom (user-defined) characters. These are the addresses of DDRAM and CGRAM of LCD which has great importance in displaying data and characters on screen.

 

DDRAM Address:-

0X80

0X81

0X82

0X83

0X84

0X85

0X86

0X87

0X88

0X89

0X8A

0X8B

0X8C

0X8D

0X8E

0X8F

0XC0

0XC1

0XC2

0XC3

0XC4

0XC5

0XC6

0XC7

0XC8

0XC9

0XCA

0XCB

0XCC

0XCD

0XCE

0XCF

CGRAM Address:-

CGRAM Address

Corresponding memory location in DDRAM

0x40 ------ 0x47

0x00

0x48 ------ 0x4f

0x01

0x50 ------ 0x57

0x02

0x58 ------ 0x5f

0x03

0x60 ------ 0x67

0x04

0x68 ------ 0x6f

0x05

0x70 ------ 0x77

0x06

0x78 ------ 0x7f

0x07

 

Note: - There can be different RAM addresses, PIN descriptions and no. of PINs in different LCDs, depending upon the module and manufacturer of the LCD. 

 

  • Few Commands used in LCD; one should know these to work with LCD.

0x38_ 8 Bit mode Active.

0x02 and 0x28_ 4 Bit mode Active.

0x01_ Clear Screen.

0x1c_ Shift text Right.**

0x18_ Shift text left.

0x0f_ display ON and cursor blink.

0x0c_ display ON and cursor OFF.

0x0e_ both display and Cursor ON.

** This command 'shifts the text left' in older versions of Proteus ISIS.

 

  • Few Examples:-
  • DATA Pins (D0-D7) = PORTC
  • Control pins;
  • RS = PD0;
  • RW =PD1;
  • E = PD2;
  • Proteus Keyword_(16x2 LCD)

 

How to display Character on LCD:-

Task- Write a programme to display ' AVR ' on LCD.

#define F_CPU 1000000UL

#include

#include

 

void cmd(char a)        //Command Routine.

{

      PORTC= a;         //PORTC, Data Pins.

      PORTD&=~(1<<PD0); //RS=0; Command Input.

      PORTD&=~(1<<PD1); //RW=0; Write command.

      PORTD|=(1<<PD2);  // E=1; Enable (HIGH).

      PORTD&=~(1<<PD2); // E=0; Enable (LOW).

      _delay_ms(20);

      }

     

void data(char a)       //data Routine.

{

      PORTC= a;         //PORTC, Data Pins.

      PORTD|=(1<<PD0);  //RS=1; Data Input.

      PORTD&=~(1<<PD1); //RW=0; Write command.

      PORTD|=(1<<PD2);  // E=1; Enable (HIGH).

      PORTD&=~(1<<PD2); // E=0; Enable (LOW).

      _delay_ms(20);

      }

void main()

{

      DDRD=DDRC=0xff;   //PORTC and PORTD output.

      cmd(0x38);        //8 Bit Mode active.

      cmd(0x0c);        //display ON and cursor OFF.

      cmd(0x01);        //clear screen.

      cmd(0x80);        //placing cursor at position; 1stROW & 1stColumn.

      data('A');        //data to be displayed.

data('V');

data('R');

}

 

How to display string on LCD:-

Task- Write a programme to display "Embedded Informatics " on LCD.

 

#define F_CPU 1000000UL

#include

#include

 

void cmd(char a)

{

      PORTC= a;

      PORTD&=~(1<<PD0);

      PORTD&=~(1<<PD1);

      PORTD|=(1<<PD2);

      PORTD&=~(1<<PD2);

      _delay_ms(20);

      }

     

void data(char a)

{

      PORTC= a;

      PORTD|=(1<<PD0);

      PORTD&=~(1<<PD1);

      PORTD|=(1<<PD2);

      PORTD&=~(1<<PD2);

      _delay_ms(20);

      }

     

void print(char a[])          //creating ARRAY function.

{

      int l;

      while(a[i]!=0)          //loop will out at null value.

      {

            data(a[i]);

            i++;

      }

}

 

void main()

{

      DDRD=DDRC=0xff;

      cmd(0x38);

      cmd(0x0e);

      cmd(0x01);

      cmd(0x80);

      print("Embedded");

     cmd(0xc0);

print("Informatics..");

}

 

How to display custom character on LCD:-

Task- Write a programme to display an emoji on LCD.

 

#define F_CPU 1000000UL

#include

#include

 

void cmd(char a)

{

      PORTC= a;

      PORTD&=~(1<<PD0);

      PORTD&=~(1<<PD1);

      PORTD|=(1<<PD2);

      PORTD&=~(1<<PD2);

      _delay_ms(20);

}

 

void data(char a)

{

      PORTC= a;

      PORTD|=(1<<PD0);

      PORTD&=~(1<<PD1);

      PORTD|=(1<<PD2);

      PORTD&=~(1<<PD2);

      _delay_ms(20);

}

     

void print(char a[])

{

      int l;

      while(a[i]!=0)

      {

            data(a[i]);

            i++;

      }

}

     

 

void main()

{

      DDRD=DDRC=0xff;

      cmd(0x38);

      cmd(0x0c);

      cmd(0x01);

      cmd(0x80);

      print("CHILLAX...");

      cmd(0x60);              //placing cursor inside CGRAM.

      data(0x11);             //storing data at the mentioned address.

      data(0x11);

      data(0x04);

      data(0x04);

      data(0x04);

      data(0x11);

      data(0x0e);

      data(0x00);

      cmd(0xc0);        //placing cursor on display at 2nd row, 1st column.

      data(0x04);       //displaying data as stored in CGRAM.

}

 

 

Similarly, we can display different custom characters on LCD using this method.

 

Note:-

We can also display data on LCD in 4 Bit mode. In this mode, we use only 4 data pins (as defined by manufacturer) of LCD to display our data, namely; DB4, DB5, DB6, DB7

 

 

CLICK HERE TO VIEW / DOWNLOAD AVR TRAINING SYLLABUS

 

CLICK HERE TO REGISTER NOW FOR AVR TRAINING..

 

 

Top