LPC2148 LCD Interfacing

Updated: Jan 28, 2019

LCD (Liquid Crystal Display):-

LCD is very useful in providing user interface as well as for debugging purpose. The LCD provide an easy way to get display for an embedded system. The most common used ALPHANUMERIC displays are 1*16(Single line and 16 characters), 2*16(double line and 16 characters per line) and 4*20(4 line and 20 characters per line).


LCD Pin Out:-



LCD Pin Diagram

LCD Pin Description


LCD Command:-


Set Cursor Move Direction:-

ID – Increment the Cursor after each Byte written to Display if Set

S – Shift Display when Byte Written to Display


Enable Display/Cursor:-

D – Turn the Display ON (1)/OFF (0)

C – Turn Cursor ON (1)/OFF (0)

B – Cursor Blink ON (1)/OFF (0)


Move Cursor/ Shift Display:-

SC – Display Shift ON (1)/OFF (0)

RL – Direction of Shift Right (1)/Left (0)


Shift Interface Length:-

DL – Set data Interface Length 8 (1)/4 (0)

N – Number of Display Lines 1(0)/2(1)

F – Character Font 5*10 (1) / 5*7 (0)


Poll the “Busy Flag”

BF – This bit is set while the LCD is processing


Move Cursor to CGRAM/Display:-

A – Address


Read/Write ASCII to the Display:-

D - Data



Schematic Diagram :-



Code for LCD Header File:-

#ifndef __lcd_H__

#define __lcd_H__


#define d4high() IO0SET |= (1 << 19)

#define d5high() IO0SET |= (1 << 20)

#define d6high() IO0SET |= (1 << 21)

#define d7high() IO0SET |= (1 << 22)

#define d4low() IO0CLR |= (1 << 19)

#define d5low() IO0CLR |= (1 << 20)

#define d6low() IO0CLR |= (1 << 21)

#define d7low() IO0CLR |= (1 << 22)


#define DATA_PORT() IO0SET |=(1<<16) //Function to select data port on lcd

#define COMMAND_PORT() IO0CLR |=(1<<16) //Function to select command port on LCD

#define READ_DATA() IO0SET |=(1<<17) //Function to select read operation on lcd

#define WRITE_DATA() IO0CLR |=(1<<17) //Function to select write operation on LCD

#define EN_HI() IO0SET |=(1<<18) //Function to Enable lcd

#define EN_LOW() IO0CLR |=(1<<18) //Function to disable LCD


void Delay(unsigned char );

void Delay_Small(unsigned char );

unsigned char Busy_Wait(void); //This function checks the busy status of lcd

void lcd_cmd(unsigned int data); //This function is used to send lcd commands

void lcd_data(unsigned int data); //This function is used to send data to lcd

void wrt_dec_char(unsigned char );

void wrt_dec_long(unsigned long );

void wrt_dec_int(unsigned int );

void cursorxy(unsigned char , unsigned char );

void lcd_init(void) ;

void lcd_out(unsigned char , unsigned char ,char *data);

void lcd_print(char row, char coloumn, unsigned int value, int digits);


#endif

Code for LCD Command Initialization:-

#include"lcd.h"

#include <lpc214x.h> //Includes LPC2148 register definitions


unsigned int temp;

unsigned int unit;

unsigned int tens;

unsigned int hundred;

unsigned int thousand;

unsigned int million;


void Delay(unsigned char j)

{

unsigned int i;

for(;j>0;j--)

{

for(i=0; i<60000; i++);

}

}


void Delay_Small(unsigned char j)

{

unsigned int i;

for(;j>0;j--)

{

for(i=0; i<1000; i++);

}

}


void wrt_2dec_place(unsigned char a)

/* function to write the fractional part after decimal upto

two decimal places

INPUT : the decimal part as unsigned char */

{

unsigned char i;

unsigned char b,c[2]={48,48};

for(i = 1;a > 0;i--)

{

b = a % 10;

b = b + 48;

a = a / 10;

c[i] = b;

}

for(i =0; i<=1;i++)

lcd_data(c[i]);

}


void lcd_cmd(unsigned int data) //This function is used to send lcd cmds

{

unsigned int temp=0;

EN_LOW();

COMMAND_PORT();

WRITE_DATA();

// temp=data;

// IO0PIN&=0xFF87FFFF;

// IO0PIN|=(temp & 0xF0) << 15;

// EN_HI();

// EN_LOW();

// temp=data & 0x0F;

// IO0PIN&=0xFF87FFFF;

// IO0PIN|=(temp) << 19;

if(data & 0x80) d7high(); else d7low();

if(data & 0x40) d6high(); else d6low();

if(data & 0x20) d5high(); else d5low();

if(data & 0x10) d4high(); else d4low();

// temp=data;

// IO0PIN&=0xFF87FFFF;

// IO0PIN|=(temp & 0xF0) << 15;

EN_HI();

EN_LOW();

if(data & 0x08) d7high(); else d7low();

if(data & 0x04) d6high(); else d6low();

if(data & 0x02) d5high(); else d5low();

if(data & 0x01) d4high(); else d4low();

EN_HI();

EN_LOW();

//Delay(100);

//while(Busy_Wait());

Delay(10);

}


void lcd_data(unsigned int data) //This function is used to send data to lcd

{

unsigned int temp=0;

EN_LOW();

DATA_PORT();

WRITE_DATA();

if(data & 0x80) d7high(); else d7low();

if(data & 0x40) d6high(); else d6low();

if(data & 0x20) d5high(); else d5low();

if(data & 0x10) d4high(); else d4low();

// temp=data;

// IO0PIN&=0xFF87FFFF;

// IO0PIN|=(temp & 0xF0) << 15;

EN_HI();

EN_LOW();

if(data & 0x08) d7high(); else d7low();

if(data & 0x04) d6high(); else d6low();

if(data & 0x02) d5high(); else d5low();

if(data & 0x01) d4high(); else d4low();

// temp=data & 0x0F;

// IO0PIN&=0xFF87FFFF;

// IO0PIN|=(temp) << 19;

EN_HI();

EN_LOW();

Delay_Small(1);

}


void wrt_dec_char(unsigned char a)

{

unsigned char i;

unsigned char b,c[3]={48,48,48};

for(i = 2;a > 0;i--)

{

b = a % 10;

b = b + 48;

a = a / 10;

c[i] = b;

}

for(i =0; i<=2;i++)

lcd_data(c[i]);

}


void wrt_dec_long(unsigned long a)

{

unsigned char i;

unsigned char b,c[10]={48,48,48,48,48,48,48,48,48,48};

for(i = 9;a > 0;i--)

{

b = a % 10;

b = b + 48;

a = a / 10;

c[i] = b;

}

for(i =0; i<=9;i++)

lcd_data(c[i]);

}

void wrt_dec_int(unsigned int a)

{

unsigned char i;

unsigned char b,c[5]={48,48,48,48,48};

for(i = 4;a > 0;i--)

{

b = a % 10;

b = b + 48;

a = a / 10;

c[i] = b;

}

for(i =0; i<=4;i++)

lcd_data(c[i]);

}

void cursorxy(unsigned char x, unsigned char y)

{

if((x<1||x>2)&&(y<1||y>16))

{

x=1;

y=1;

}

if(x == 1)

lcd_cmd(0x7F+y);

else

lcd_cmd(0xBF+y);

/*********************use below function if you are using 20 * 4 lcd display*********

if((x<1||x>4)&&(y<1||y>20))

{

x=1;

y=1;

}

if(x == 1)

lcd_cmd(0x7F+y);

if(x == 2)

lcd_cmd(0xBF+y);

if(x == 3)

lcd_cmd(0x93+y);

if(x == 4)

lcd_cmd(0xD3+y);

*************************************************************************************/

}

void lcd_init()

{

lcd_cmd(0x20);

lcd_cmd(0x28);

lcd_cmd(0x0C);

lcd_cmd(0x06);

}

void lcd_out(unsigned char xpos, unsigned char ypos,char *data)

{

unsigned char count = 0;

cursorxy(xpos,ypos);

while(*data)

{

lcd_data(*data);

data++;

}

}

//Function To Print Any input value upto the desired digit on lcd

void lcd_print(char row, char coloumn, unsigned int value, int digits)

{

unsigned char flag=0;

if(row==0||coloumn==0)

{

cursorxy(1,1);

}

else

{

cursorxy(row,coloumn);

}

if(digits==5 || flag==1)

{

million=value/10000+48;

lcd_data(million);

flag=1;

}

if(digits==4 || flag==1)

{

temp = value/1000;

thousand = temp%10 + 48;

lcd_data(thousand);

flag=1;

}

if(digits==3 || flag==1)

{

temp = value/100;

hundred = temp%10 + 48;

lcd_data(hundred);

flag=1;

}

if(digits==2 || flag==1)

{

temp = value/10;

tens = temp%10 + 48;

lcd_data(tens);

flag=1;

}

if(digits==1 || flag==1)

{

unit = value%10 + 48;

lcd_data(unit);

}

if(digits>5)

{

lcd_data('E');

}

}

/********************* USE BELOW FUNCTION IF USING 20 * 4 lcd DISPLAY *************

unsigned char count = 0;

cursorxy(xpos,ypos);

while(*dat)

{

lcd_chr_cp(*dat);

dat++;

count++;

if(count == 21-(ypos))

{

xpos++;

cursorxy(xpos,1);

count = 0;

}

}

***********************************************************************************/

Code for Writing ‘ARM7’ and Incrementing Values of ‘i’ :-


#include <LPC214x.H> /* LPC214x definitions */

#include"lcd.h"

void delay_ms(unsigned int count)

{

volatile unsigned int j,k;

for (j=0;j<count;j++)

{

for (k=0;k<3000;k++)

{

__asm

{

nop;

nop;

}

}

}

}

int main (void)

{

int i = 0;

PINSEL0 = 0x00000000; // Enable UART0 Rx and Tx pins

PINSEL1 = 0x00000000; // Enable AD0.1 and AD0.2 // Enable AD0.1 and AD0.2

IO0DIR |= ( 1<< 15) | ( 1<< 16) | ( 1<< 17) | ( 1<< 18) | ( 1<< 19) | ( 1<< 20) | ( 1<< 21) | ( 1<< 22) ;

lcd_init();

lcd_cmd(1);

lcd_out(1,1,"ARM7 LCD");

while(1)

{

i++;

lcd_print(2,1,i,2);

delay_ms(1000);

}

}


19 views

Adress

Vidya Robotics,

Plot no 22 Ashok Nagar Garkheda Road,Near Manik Hospital 

Aurangabad

431001

Courses 

Contact Info

info(at)vidyarobotics.com

vidyarobotics(at)gmail.com

9689068780

9922968553


 

Stay In touch 

Become Our Bestie!

  • LinkedIn Social Icon
  • White Facebook Icon
  • White YouTube Icon