Interfacing Liquid Crystal Display (LCD) witth LPC2148 MCU

Interfacing Liquid Crystal Display (LCD) witth LPC2148 MCU

Theory

A 16×2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5×7 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 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. For that purpose, address space for first 16 ASCII characters are reserved for users. After CGRAM has been setup to display characters, user can easily display their custom characters on the LCD screen.

For other Details regarding the operation of the LCD see the AVR  LCD  Tutorial. LCD_block_diagram

 

Initializing the LCD in 4 Bit mode

  1. Send wake up command 3 time to LCD which is 0x30
  2. Initialize LCD in 4 bit mode which is 0x20
  1.  Clear the display by sending 8 bits clear display command which is 0X01.
  2. Auto address increment by sending 8 bits command 0X06.
  3. Cursor off by sending 8 bits command 0X0C.
  4. Set Cursor to the first location of First line by sending the 8 bit command 0X80.
  5. Set Cursor to the first location of second line by sending the 8 bits command 0XC0.
    lcd_pin_description

Schematic

lcd_LPC2148

LCD  Source code

  #include<lpc21xx.h>
   void delay(unsigned long val); //  function to generate delay
   void write_command(int cmd);//function  to write command to LCD
   void write_data(int data);// function to  write data to LCD
   void lcd_command(char cmd);// function  to separate 8 bit command to nibbles and send to //write_command
   void lcd_data(char data); );// function  to separate 8 bit data to nibbles and send to write_data
   void init_lcd(void);// function to  initialize LCD
   void printlcd(char *CPtr);// Function to  take String as argument and send data to write_data //to print 
   int main()
   {
   IO1DIR  = 0x00fe0000;//initializing P1.17b to 1.23 as  output ports
   init_lcd();//initializing  LCD
   printlcd("KEC ROBOTICS");//sending string to print in LCD
   lcd_command(0xc0);//  moving crusor to first position of second line 
   }
   void delay(unsigned long val)
   {          while  (val>0)
   {val--;}
   }
   void printlcd(char *CPtr)
   {
   while(*CPtr!='\0')
   {
   lcd_data(*CPtr);
   CPtr++;
   delay(20000);}}
   void init_lcd(void)
   {
   lcd_command(0x03);
   delay(100000);
   lcd_command(0x03);
   delay(100000);
   lcd_command(0x02);//initialize  LCd in 4 bit mode
   delay(100000);
   lcd_command(0x01);//command  to clear LCD
   delay(10000);
   lcd_command(0x06);//command  for auto address increase 
   delay(10000);
   lcd_command(0x80);//send  crusor to first location of first line
   delay(10000);}
   void lcd_data(char data)
   {
   char  shift_data2;
   shift_data2= data & 0XF0;
   write_data(shift_data2);
   shift_data2= (data<<4) &  0XF0;
   write_data(shift_data2);}
   void lcd_command(char cmd)
   {
   char  shift_data1;
   shift_data1= cmd  & 0XF0;
   write_command(shift_data1);
   shift_data1= (cmd<<4)  & 0XF0;
   write_command(shift_data1);}
   void write_command(int cmd)
   {
   IO1CLR=0x00fe0000;
   IO1CLR=0x00060000;  //RS=RW=0
   cmd=cmd<<16;
   IO1SET=cmd; 
   IO1SET=0x00080000; // Set high to  enable pin
   delay(100000);
   IO1CLR=0x00080000; // Set low to  enable pin
   delay(10000);}
   void write_data(int data)
   {
   IO1CLR=0x00fe0000;
   IO1SET=0x00020000;  //Select register select to 1
   data=data<<16;
   IO1SET=data;
   IO1SET=0x00080000; // Set high to  enable pin
   delay(100000);
   IO1CLR=0x00080000;  // Set low to enable pin
   delay(10000);}

Share