Monday 23 July 2018

Scrolling String on 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.
We are already know about how LCD works and atmega8 micro-controller. Here PIC18F4520 has 5 PORTS. The bellow steps are LCD programming using PIC18F4520 microcontroller.
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 . 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: Scrolling string in LCD using PIC18F4520
// Author: Hack Projects India
// Module description: Operate a LCD
// **************************************************

#include <p18f4520.h>
#include <delays.h>
#pragma config OSC=HS, FCMEN=ON, WDT=OFF, IESO=OFF, XINST=OFF, LVP=OFF
#define lcd PORTD
unsigned char rs,en;
unsigned int d;

void cmd_lcd(unsigned char z)
{
  rs=0x00;//for command
  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
     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 delay(unsigned short long x)
{
    while(x>0)
    {
        x--;
    }
    return;
}
void main()
{
 unsigned char s[]="Hack Projects India ",i,j;
      ADCON1=0b00001111;
      TRISA=0Xff;
 TRISD=0;  // Configure Port d as output port
 TRISB=0;  // Configure Port B as output port
      init_lcd();
 while(1)
        {

   cmd_lcd(0x80); // Initial starting possion
   i=0;
   for(;s[i]!='\0';)
   data_lcd(s[i++]); // Print the data up to null character
   i=0;
   j=s[0]; // Take the 1st character in j
   for(;s[i]!='\0';i++)
   s[i] = s[i+1]; // Shift the characters in array
   s[i-1]=j; // Take the 1st character in last
   s[i]='\0'; // Finally take null character
   delay(5000);
        }
}


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