lcd

____________________________________________________________________________________________________

Introduction

I think there is no more need to introduce how to interface LCD with microcontroller because we earlier learnt it. So in this module we learn how to interface LCD with micrcontroller in 4-bit mode rather than 8-bit conventional mode.

Benefit of LCD interfacing in  4-bit

There are limited numbers of GPIO (general purpose input outputs) pins in micrcontrollers. When we design the large embedded system with single micrcontroller, there is need of efficient use of GPIOs. And as a good programmer, it is nice practice to use GPIO pins efficiently. So interfacing LCD with microcontroller by using 4-bit mode, we save 4 GPIO pins.

 

Concept of 4-bit mode

Till now whatever we discussed in the previous part of ths LCD module, we were dealing with 8-bit mode. Now we are going to learn how to use LCD in 4-bit mode.

                        In 4-bit mode, the data is sent in nibbles(1 nibble= 4 bit)  form, first we send the higher nibble and then the lower nibble with same RS, RW and EN pin fuctioning as we were doing in 8-bit mode. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation.

 

Connection description:

Connections of LCD with microcontroller are shown in circuit diagram of next tab. In 4-bit mode, the Data lines must be connected with D4, D5, D6 and D7 pins of LCD module and RS, RW & EN pin functioning same as it was in 8-bit mode.

 Configuring LCD to 4-bit mode:

The LCD can be configured in 4-bit mode by sending appropriate command which is called “Function set” to it. The Function set is hexadecimal code for LCD’s control unit, which selects working modes of LCD. The “Function Set” is mentioned below:

Untitled

DL – Data Length, DL = 1 for 8-bit mode, and  DL = 0 for 4-bit mode

N  –   No. of Lines N = 1 for 2 Lines selection , and N = 0 for  1Lines selection

F  –   Fonts F = 1 for 5×10 dots, and F = 0 for 5×7 dots

                         According to Function Set, the value of 4–bit mode will be 0010 0000(0x20) because DL=0. The value of “Function Set” for the LCD configuration 2 line (N=1), 5X7 dots (F=0) and 4-bit (DL=0) mode will be 0010 1000(0x28).

                        When the power supply is given to LCD, it remains in 8-bit mode(default). Now, if 0x20 is sent, lower nibble will not be received by LCD because four data lines (D4-D7) are connected, So 0x02 is sent instead of 0x20.

Steps of Programming

 

Step1: Initializing LCD in 4-bit mode.

void lcd_ini( )
{
cmd(0X02);         // to initialize LCD in 4-bit mode.

cmd(0X28);         //to initialize LCD in 2 lines, 5X7 dots and 4bit mode

cmd(0x0E);         //display ON, cursor blinking

cmd(0X06);         //shift cursor to right on writing

cmd(0X01);         //clear display screen

cmd(0X80);         //force cursor to starting position of first line
}

Step2: Nibble sending.

 

For example, If lower 4 pins of any 8-bit port are connected to LCD’s data line. So, the lower nibble of a byte can be sent to LCD data lines by masking higher nibble. To send higher nibble, data byte is shifted right for four places. higher nibble replaces lower nibble by this shifting. Data is sent after masking the byte.

 

Example:    if you want to send 0x28, then

void cmd(unsigned char Command)               //now command = 0x28, according to our example
{
unsigned int Temp;

//SENDING HIGHER NIBBLE

Temp = (Command>>4) & 0x0F;          //now, Temp = 0x02

PORTx | = Temp;          //sending Temp to port x with OR operation so other bits are not affected

RS=RW=0;

EN=1;

delay();

EN=0;

//SENDING LOWER NIBBLE

delay();

Temp = Command & 0x0F;        //now, Temp = 0x08

PORTx | = Temp;          //sending Temp to port x with OR operation so other bits are not affected

RS=RW=0;

EN=1;

delay();

EN=0;
}

Step 3: Exactly the same way we send the data to LCD and function of RS, RW and EN is same as we were doing.

1. circuit diagram of ARM7( LPC2148 ) interfacing with lcd in 4 bit mode

arm7 ( lpc2148 ) interfacing lcd in 4bit

1. program of ARM7 ( LPC2148 )  interfacing with lcd in 4 bit mode

/******************************************************
www.firmcodes.com
DEVELOPED BY:- FIRMWARE DEVELOPER
WHAT PROGRAM DO:- ARM7 ( LPC2148 ) INTERFACING WITH LCD IN 4 BIT MODE
******************************************************/

#include<lpc21xx.h>

#include"delay.h"

#define LCD (0xf<<19)
#define RS (1<<16)
#define RW (1<<17)
#define EN (1<<18)

void lcd_ini();     // LCD initilization function
void lcd_str(unsigned char *temp);  // function to write string on lcd
void cmd(unsigned char x);     // lcd command function
void lcd_display(unsigned char x);  // lcd data display function

void main()    // main program
  {
		 PINSEL1=0X00000000;   // selcect as gpio
	   IO0DIR=0XFFFFFFFF;    // make direction as output
	   lcd_ini();        //initilization lcd
	   delay_fv(100,100);  // calling delay function
	   lcd_str("WELCOME TO");  // writing string on lcd
	   cmd(0xc0);      // move the cursor on next line
	   lcd_str("FIRMCODES.COM");  // writing string on lcd
	   while(1);   // infinite loop
  }
	
void lcd_display(unsigned char x)  // lcd display funtion
  {
			unsigned int temp;    //initilize variable
			delay_ms(700);        // calling delay
			IO0CLR|=(RS|RW|EN|LCD); // clearing rs, rw,en and lcd pins
			temp=(x>>4)&0x0f;   // rotating value of x by 4 and anding with 0x0f
			IO0SET|=(temp<<19);  //put value of temp at on lcd pins
		 IO0SET|=RS;    // set re pin
		 IO0CLR|=RW;    // clear rw pin 
		 IO0SET|=EN;    // set enable pin
			delay_ms(10); // hault for some time
			IO0CLR|=EN;   // clear enable pin
			
			delay_fv(1000,10); // calling delay function
			IO0CLR|=(RS|RW|EN|LCD); // clearing rs, rw,en and lcd pins
			temp=x&0x0f;    // anding value of x with 0x0f
			IO0SET|=(temp<<19); // putting value of temp on lcd data pin
		 IO0SET|=RS;  // set re pin 
		 IO0CLR|=RW;  // clear rw pin
		 IO0SET|=EN;  // set enable pin
			delay_ms(10); // hault for some time
		 IO0CLR|=EN;  // clear enable pin
			delay_ms(100); // calling delay function
  }

void cmd(unsigned char x)   // lcd command funtion
  {
			unsigned int temp;      //initilize variable
			IO0CLR|=(RS|RW|EN|LCD); // clearing rs, rw,en and lcd pins
			temp=(x>>4)&0x0f;   // rotating value of x by 4 and anding with 0x0f
		 IO0SET|=(temp<<19);  //put value of temp at on lcd pins
		 IO0CLR|=RS;  // clear re pin
		 IO0CLR|=RW;  // clear rw pin
		 IO0SET|=EN;  // set enable pin
		 delay_ms(1000); // hault for some time
		 IO0CLR|=EN;  // clear enable pin
			
			delay_fv(100,10);  // calling delay function
			IO0CLR|=(RS|RW|EN|LCD);  // clearing rs, rw,en and lcd pins
			temp=x&0x0f;     // anding value of x with 0x0f
		 IO0SET|=(temp<<19);  // putting value of temp on lcd data pin
		 IO0CLR|=RS;  // clear re pin 
		 IO0CLR|=RW;  // clear rw pin
		 IO0SET|=EN;  // set enable pin
		 delay_ms(1000); // hault for some time
		 IO0CLR|=EN; // clear enable pin
		delay_fv(100,10);	 // calling delay function
 }

void lcd_ini()   // lcd initilization function
 {
			cmd(0X02);  // to initialize LCD in 4-bit mode.
		  cmd(0X28);  //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
		  cmd(0x0e);  // cursor ON
			cmd(0X06);
			cmd(0X01);  // clear lcd
			cmd(0X80);  // cursor indicating first line first position
  }

void lcd_str(unsigned char *str)  // funtion to write sting on lcd
  {
     while(*str!='\0')  // check str for NULL
       {
          lcd_display(*str);  // write one characture from string
          str++;     // increament string
       }
  }

DELAY.H header file

void delay_ff()
{
    unsigned int b,v;
    for(b=0;b<600;b++)
    for(v=0;v<100;v++);
}

void delay_pf(unsigned int x)
{
 unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<153;j++);
}

void delay_fv(unsigned int x,int y)
{
 unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<y;j++);
}

void delay_ms(int count)
{
  int j=0,i=0;

  for(j=0;j<count;j++)
  {
    /* At 60Mhz, the below loop introduces
    delay of 10 us */
    for(i=0;i<35;i++);
  }
}

PROTEUS File for SIMULATION(Password Of RAR file is :-firmcodes.com)

download (1)

Content for the tab VIDEO

Suggested Reading

  1. Interfacing of Graphical LCD with ARM7 ( LPC2148 )
  2. Custom character generation on LCD with ARM(LPC21XX)
  3. Dot Matrix LED Display Multiplexing Matrix with ARM (LPC21XX)