Skip to main content

PIC 4 Bit LCD Interfacing Tutorial

The mikroC PRO for PIC provides a library for communication with Lcds (with HD44780 compliant controllers) through the 4-bit interface. An example of Lcd connections is given on the schematic at the bottom of this page.

For LCD Basics: Character LCD Basics  

External dependencies of Lcd Library
The following variables must be defined in all projects using Lcd Library :
Description :
Example :
extern sfr sbit LCD_RS:
Register Select line.
sbit LCD_RS at RB4_bit;
extern sfr sbit LCD_EN:
Enable line.
sbit LCD_EN at RB5_bit;
extern sfr sbit LCD_D7;
Data 7 line.
sbit LCD_D7 at RB3_bit;
extern sfr sbit LCD_D6;
Data 6 line.
sbit LCD_D6 at RB2_bit;
extern sfr sbit LCD_D5;
Data 5 line.
sbit LCD_D5 at RB1_bit;
extern sfr sbit LCD_D4;
Data 4 line.
sbit LCD_D4 at RB0_bit;
extern sfr sbit LCD_RS_Direction;
Register Select direction pin.
sbit LCD_RS_Direction at TRISB4_bit;
extern sfr sbit LCD_EN_Direction;
Enable direction pin.
sbit LCD_EN_Direction at TRISB5_bit;
extern sfr sbit LCD_D7_Direction;
Data 7 direction pin.
sbit LCD_D7_Direction at TRISB3_bit;
extern sfr sbit LCD_D6_Direction;
Data 6 direction pin.
sbit LCD_D6_Direction at TRISB2_bit;
extern sfr sbit LCD_D5_Direction;
Data 5 direction pin.
sbit LCD_D5_Direction at TRISB1_bit;
extern sfr sbit LCD_D4_Direction;
Data 4 direction pin.
sbit LCD_D4_Direction at TRISB0_bit;
Library Routines
  • Lcd_Init
  • Lcd_Out
  • Lcd_Out_Cp
  • Lcd_Chr
  • Lcd_Chr_Cp
  • Lcd_Cmd
Lcd_Init
Prototype
void Lcd_Init();
Returns
Nothing.
Description
Initializes Lcd module.
Requires
Global variables:
  • LCD_D7: Data bit 7
  • LCD_D6: Data bit 6
  • LCD_D5: Data bit 5
  • LCD_D4: Data bit 4
  • LCD_RS: Register Select (data/instruction) signal pin
  • LCD_EN: Enable signal pin
  • LCD_D7_Direction: Direction of the Data 7 pin
  • LCD_D6_Direction: Direction of the Data 6 pin
  • LCD_D5_Direction: Direction of the Data 5 pin
  • LCD_D4_Direction: Direction of the Data 4 pin
  • LCD_RS_Direction: Direction of the Register Select pin
  • LCD_EN_Direction: Direction of the Enable signal pin
must be defined before using this function.
Example
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
...

Lcd_Init();
Lcd_Out
Prototype
void Lcd_Out(char row, char column, char *text);
Returns
Nothing.
Description
Prints text on Lcd starting from specified position. Both string variables and literals can be passed as a text.
Parameters :
  • row: starting position row number
  • column: starting position column number
  • text: text to be written
Requires
The Lcd module needs to be initialized. See Lcd_Init routine.
Example
// Write text "Hello!" on Lcd starting from row 1, column 3:
Lcd_Out(1, 3, "Hello!");
Lcd_Out_Cp
Prototype
void Lcd_Out_Cp(char *text);
Returns
Nothing.
Description
Prints text on Lcd at current cursor position. Both string variables and literals can be passed as a text.
Parameters :
  • text: text to be written
Requires
The Lcd module needs to be initialized. See Lcd_Init routine.
Example
// Write text "Here!" at current cursor position:
Lcd_Out_Cp("Here!");
Lcd_Chr
Prototype
void Lcd_Chr(char row, char column, char out_char);
Returns
Nothing.
Description
Prints character on Lcd at specified position. Both variables and literals can be passed as a character.
Parameters :
  • row: writing position row number
  • column: writing position column number
  • out_char: character to be written
Requires
The Lcd module needs to be initialized. See Lcd_Init routine.
Example
// Write character "i" at row 2, column 3:
Lcd_Chr(2, 3, 'i');
Lcd_Chr_Cp
Prototype
void Lcd_Chr_Cp(char out_char);
Returns
Nothing.
Description
Prints character on Lcd at current cursor position. Both variables and literals can be passed as a character.
Parameters :
  • out_char: character to be written
Requires
The Lcd module needs to be initialized. See Lcd_Init routine.
Example
// Write character "e" at current cursor position:
Lcd_Chr_Cp('e');
Lcd_Cmd
Prototype
void Lcd_Cmd(char out_char);
Returns
Nothing.
Description
Sends command to Lcd.
Parameters :
  • out_char: command to be sent
  Note : Predefined constants can be passed to the function, see Available Lcd Commands.
Requires
The Lcd module needs to be initialized. See Lcd_Init table.
Example
// Clear Lcd display:
Lcd_Cmd(_LCD_CLEAR);
Available Lcd Commands
Lcd Command
Purpose
_LCD_FIRST_ROW
Move cursor to the 1st row
_LCD_SECOND_ROW
Move cursor to the 2nd row
_LCD_THIRD_ROW
Move cursor to the 3rd row
_LCD_FOURTH_ROW
Move cursor to the 4th row
_LCD_CLEAR
Clear display
_LCD_RETURN_HOME
Return cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected.
_LCD_CURSOR_OFF
Turn off cursor
_LCD_UNDERLINE_ON
Underline cursor on
_LCD_BLINK_CURSOR_ON
Blink cursor on
_LCD_MOVE_CURSOR_LEFT
Move cursor left without changing display data RAM
_LCD_MOVE_CURSOR_RIGHT
Move cursor right without changing display data RAM
_LCD_TURN_ON
Turn Lcd display on
_LCD_TURN_OFF
Turn Lcd display off
_LCD_SHIFT_LEFT
Shift display left without changing display data RAM
_LCD_SHIFT_RIGHT
Shift display right without changing display data RAM
Code
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char txt1[] = "Embedded";   
char txt2[] = "Projects";
char txt3[] = "Lcd 4 bit";
char txt4[] = "Example";

char i;                              // Loop variable

void Move_Delay() {                  // Function used for text moving
  Delay_ms(500);                     // You can change the moving speed here
}

void main(){
 

  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,6,txt3);                 // Write text in first row

  Lcd_Out(2,6,txt4);                 // Write text in second row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display

  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,5,txt2);                 // Write text in second row

  Delay_ms(2000);

  // Moving text
  for(i=0; i<4; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }

  while(1) {                         // Endless loop
    for(i=0; i<8; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }

    for(i=0; i<8; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();
    }
  }
}

Comments

Popular posts from this blog

PIC 16F877A Microcontroller Based Electronic Lock 16x2LCD 4x3 Keypad

 Circuit Diagram Security is a prime concern in our day-today life. Everyone wants to be as much secure as possible. An access control for doors forms a vital link in a security chain. The microcontroller based digital lock for Doors is an access control system that allows only authorized persons to access a restricted area. An electronic lock or digital lock is a device which has an electronic control assembly attached to it. They are provided with an access control system. This system allows the user to unlock the device with a password. The password is entered by making use of a keypad. The user can also set his password to ensure better protection. The major components include a Keypad, LCD and the controller PIC16F877A. This article describes the making of an electronic code lock using the 16F877A microcontroller. The system is fully controlled by the 8 bit microcontroller 16F877A which has a 8Kbytes of ROM for the program memory. The password is stored in the

Electronic Voting Machine Using 8051 Microcontroller (AT89C51)

  Circuit Electronic voting machine has now replaced the traditional mechanism of voting due to several advantages like security, automatic counting etc. This project presents a way to develop an electronic voting machine which displays the count of votes on a 16x2 LCD interface. A user can get his/her vote register through a set of switches (one for each candidate). After every cast of vote, the subsequent count can be seen on LCD. The circuit uses AT89C51 microcontroller and the code for the project has been written in C. This LCD based electronic voting machine is designed for four candidates. The input part consists of a set of six tactile switches. The switches and 16x2 LCD are interfaced to microcontroller AT89C51 for various operations and displays. The provision of casting votes for the candidates has been provided through four of these switches. These switches are made active high and connected to pins 2-5 (P1^1 – P1^4) of the controller. The remaining two

PC Based Digital Voltmeter Using PIC 16F877A

Voltmeter C# .Net PIC, Arduino, AVR This is a simple voltmeter which measures 0-5V at a precision of 4.8 mV. This is a simple design using inbuilt ADC of PIC 16F877A. PIC 16F877A have 8 channel 10bit ADC.   This is a computer interfaced project. Measured voltage is output in serial interface software in computer. There is a serial interface circuit (MAX232) is necessary for interfacing with computer, which is not included in the circuit. Please check       PIC Serial Communication Tutorial (UART)   for the circuit and more details. Using one of the most popular 8 bit PIC 16f877A, for instance, reading the datasheet , we'll find that the ADC modules (10 bit) are controlled by four different registers. The first two, ADCON0 and ADCON1 , are used to set and start the ADC module. When high level language is used, the programmer doesn't need to care a lot of the register connected to the results because they are normally stored in a variable by a rout