How to display text on 16×2 LCD using PIC18F4550 Microcontroller

Summary of How to display text on 16×2 LCD using PIC18F4550 Microcontroller


This article details the process of displaying text messages on a 16x2 LCD using a PIC18F4550 microcontroller. It outlines programming steps involving LCD configuration, string storage in a character array, and iterative character transmission via the `lcddata()` function within a loop. The provided C code initializes ports, sets up the LCD in 8-bit mode, and displays the string "EngineersGarage" with a delay between characters.

Parts used in the Display Text on 16x2 LCD Project:

  • LCD
  • PIC18F4550 Microcontroller
  • Preset

Several automated and semi-automated devices require a message to be displayed in order to indicate their working status. In continuation to LCD interfacing with PIC18F4550, this article explains how to display a message or string on a 16×2 character LCD.

Programming steps:
·         Configure the LCD.
·         Store a string in a character array.
unsigned char data[20]=“EngineersGarage”;
·         Run a loop till the loop counter encounters the null character ‘’ of the string.
·         Use lcddata() function to send individual character values of the string to be displayed on LCD. 

Code:

while(data[i]!=’’)
    {
        lcddata(data[i]);   
        i++;
        Delay_ms(300);
    }

Project Source Code

###


// Program to display text on 16×2 LCD using PIC18F4550 Microcontroller

// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L, // Divide clock by 2
_FOSC_HS_1H, // Select High Speed (HS) oscillator
_WDT_OFF_2H, // Watchdog Timer off
MCLRE_ON_3H // Master Clear on
*/

//LCD Control pins
#define rs LATA.F0
#define rw LATA.F1
#define en LATA.F2

//LCD Data pins
#define lcdport LATB

void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
unsigned char data[20]=”EngineersGarage”;
unsigned int i=0;

void main(void)
{
TRISA=0; // Configure Port A as output port
LATA=0;
TRISB=0; // Configure Port B as output port
LATB=0;
lcd_ini(); // LCD initialization
while(data[i]!=”)
{
lcddata(data[i]); // Call lcddata function to send characters
// one by one from “data” array
i++;
Delay_ms(300);
}

}
void lcd_ini()
{
lcdcmd(0x38); // Configure the LCD in 8-bit mode, 2 line and 5×7 font
lcdcmd(0x0C); // Display On and Cursor Off
lcdcmd(0x01); // Clear display screen
lcdcmd(0x06); // Increment cursor
lcdcmd(0x80); // Set cursor position to 1st line, 1st column
}

void lcdcmd(unsigned char cmdout)
{
lcdport=cmdout; //Send command to lcdport=PORTB
rs=0;
rw=0;
en=1;
Delay_ms(10);
en=0;
}

void lcddata(unsigned char dataout)
{
lcdport=dataout; //Send data to lcdport=PORTB
rs=1;
rw=0;
en=1;
Delay_ms(10);
en=0;
}

###

Circuit Diagrams

Circuit Diagrams 3

Project Components

Project Video

Source: How to display text on 16×2 LCD using PIC18F4550 Microcontroller

Quick Solutions to Questions related to Display Text on 16x2 LCD Project:

  • How is the string stored for display?
    A string is stored in a character array named data.
  • What function sends individual characters to the LCD?
    The lcddata() function is used to send individual character values.
  • How does the program know when to stop displaying characters?
    The loop runs until the loop counter encounters the null character of the string.
  • Which oscillator is selected in the configuration bits?
    The High Speed (HS) oscillator is selected.
  • What is the delay time set between displaying characters?
    A delay of 300 milliseconds is used between each character.
  • How are Port A and Port B configured?
    Both Port A and Port B are configured as output ports.
  • What command configures the LCD in 8-bit mode?
    The command 0x38 configures the LCD in 8-bit mode, 2 line, and 5x7 font.
  • Where does the cursor start after initialization?
    The cursor position is set to the 1st line and 1st column.

About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.