You are on page 1of 4

How to display text on 16x2 LCD using PIC18F4550 Microcontroller

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 16x2 character LCD. In the previous article, a single character was displayed on LCD by properly configuring its data and command registers. A string is nothing but a sequential arrangement of several characters that can be displayed on LCD by using the programming steps mentioned here. The circuit connections and user-defined functions are same as earlier. The LCD data pins are connected to PortB of PIC18F4550 while the control pins are connected to first three pins of PortA.

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 \0 of the string. Use lcddata() function to send individual character values of the string to be displayed on LCD.

Code: // Program to display text on 16x2 LCD using PIC18F4550 Microcontroller // Configuration bits /* _CPUDIV_OSC1_PLL2_1L, _FOSC_HS_1H, oscillator _WDT_OFF_2H, MCLRE_ON_3H */ //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; LATA=0; TRISB=0; // Divide clock by 2 // Select High Speed (HS) // Watchdog Timer off // Master Clear on

// Configure Port A as output port // Configure Port B as output port

LATB=0; lcd_ini(); // LCD initialization while(data[i]!='\0') { 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

lcdcmd(0x0C); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x80); to 1st line, 1st column }

5x7 font // Display On and Cursor Off // Clear display screen // Increment cursor // Set cursor position

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; }

You might also like