Monday, 23 July 2018

Interfacing 16X2 LCD to PIC Microcontroller (PIC18F4520)

Aim:

The main aim of this project is how to interface a 16X2 LCD to PIC18F4520 micro-controller.

Description:


The most commonly used Character based LCDs are based on Hitachi’s HD44780 controller or other which are compatible with HD44580. In this tutorial, we will discuss about character based LCDs, their interfacing with various microcontrollers, various interfaces (8-bit/4-bit), programming, special stuff and tricks you can do with these simple looking LCDs which can give a new look to your application.

Instruction Register (IR) and Data Register (DR)
There are two 8-bit registers in HD44780 controller Instruction and Data register. Instruction register corresponds to the register where you send commands to LCD e.g LCD shift command, LCD clear, LCD address etc. and Data register is used for storing data which is to be displayed on LCD. when send the enable signal of the LCD is
asserted, the data on the pins is latched in to the data register and data is then moved automatically to the DDRAM and hence is displayed on the LCD. Data Register is not only used for sending data to DDRAM but also for CGRAM, the address where you want to send the data, is decided by the instruction you send to LCD.We will discuss more on LCD instuction set further in this tutorial.
Commands and Instruction set
Only the instruction register (IR) and the data register (DR) of the LCD can be controlled by the MCU. Before starting the internal operation of the LCD, control information is temporarily stored into these registers to allow interfacing with various MCUs, which operate at different speeds, or various peripheral control devices. The internal operation
of the LCD is determined by signals sent from the MCU. These signals, which include register selection signal (RS), read/write signal (R/W), and the data bus (DB0 to DB7), make up the LCD instructions (Table 3). There are four categories of instructions that:
  • Designate LCD functions, such as display format, data length, etc.
  • Set internal RAM addresses
  • Perform data transfer with internal RAM
  • Perform miscellaneous functions
LCD Initialization
Before using the LCD for display purpose, LCD has to be initialized either by the internal reset circuit or sending set of commands to initialize the LCD. It is the user who has to decide whether an LCD has to be initialized by instructions or by internal reset circuit. we will dicuss both ways of initialization one by one.
Sending Commands to LCD
To send commands we simply need to select the command register. Everything is same as we have done in the initialization routine. But we will summarize the common steps and put them in a single subroutine. Following are the steps:
  •  Move data to LCD port
  • select command register
  • select write operation
  • send enable signal
  • wait for LCD to process the command
Sending Data to LCD
To send data we simply need to select the data register. Everything is same asthe command routine. Following are the steps:
  • Move data to LCD port
  • select data register
  • select write operation
  • send enable signal
  • wait for LCD to process the data

Block Diagram

4 lcd

Schematic

lcd

Code

// ***********************************************************
// Project: Display string in LCD using PIC18F4520
// Author: Hack Projects India
// Module description: Operate single LCD
// ***********************************************************
#include <p18f4520.h>
#include <delays.h>
#define lcd PORTD
unsigned char rs,en,rw;
unsigned int d;

void cmd_lcd(unsigned char z)
{
  rs=0x00;//for command
  rw=0x00;
  en=0x08;//en high
  lcd=((z)&0xf0)|en|rs;
  Delay10TCYx(10);
  en=0x00;//en low
  lcd=((z)&0xf0)|en|rs;
  en=0x08;//en high
  lcd=((z<<4)&0xf0)|en|rs;
  Delay10TCYx(10);
  en=0x00;//en low
  lcd=((z<<4)&0xf0)|en|rs;
}
void data_lcd(unsigned char z)
{
     rs=0x04 ;//for data
     rw=0x00;
     en=0x08;//en high
     lcd=((z)&0xf0)|en|rs;
     Delay10TCYx(10);
     en=0x00;//en low
     lcd=((z)&0xf0)|en|rs;
     en=0x08;//en high
     lcd=((z<<4)&0xf0)|en|rs;
     Delay10TCYx(10);
     en=0x00;//en low
     lcd=((z<<4)&0xf0)|en|rs;
}
void init_lcd()
{
     cmd_lcd(0x02);//4bit mode
     cmd_lcd(0x06);//entry mode
     cmd_lcd(0x0c);//diply on cr off
     cmd_lcd(0x28);//4 bit
}
void main()
{
      ADCON1=0b00001111;
      TRISA=0Xff;
      TRISD=0;                              // Configure Port d as output port
      TRISB=0;                              // Configure Port B as output port
      init_lcd();
      data_lcd('H');
      data_lcd('A');
      data_lcd('C');
      data_lcd('K');
      data_lcd(' ');
      data_lcd('P');
      data_lcd('R');
      data_lcd('O');
      data_lcd('J');
      data_lcd('E');
      data_lcd('C');
      data_lcd('T');
      data_lcd('S');
}

Downloads:

The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment