Home > Microcontroller >Comprehensive Circuits > How to interface a 16×2 LCD display to a PIC18F4550 microcontroller?

How to interface a 16×2 LCD display to a PIC18F4550 microcontroller?

Source: InternetPublisher:走马观花 Keywords: MCU LCD display PIC18F4550 Updated: 2025/02/07

In this case study, this article will show you how to interface a 16×2 LCD display to a PIC18F4550 microcontroller from the PIC18F series.

Features of PIC18F4550:

(1) PIC18F4550 belongs to the PIC18F series; PIC18F4550 is an 8-bit microcontroller that uses a RISC architecture. PIC18F4550 has 40 PDIP (dual in-line package) pins and 44 TQFP (quad flat package) pins.

(2) The PIC18F4550 has 32KB of flash memory, 2048 bytes of SRAM (synchronous random access memory), and 256 bytes of EEPROM (electrically erasable program read-only memory).

(3) It has 35 I/O pins for interfacing and communicating with other peripheral devices and 13-channel 10-bit analog-to-digital converter for interfacing and communicating with analog peripheral devices (DC motor, LDR, etc.).

(4) It has 2 CCP and 1 ECCP modules, which are enhanced capture and compare modules, mainly used for modulation and waveform generation functions. The CCP module is a 16-bit register, which serves as a 16-bit capture bit register, a 16-bit compare bit register, a PWM and a duty cycle register.

(5) PIC18F4550 has SPI (Serial Peripheral Interface) and i2c (Inter-Integrated Circuit) for master and slave modes. It has SPP (Streaming Parallel Port) for USB streaming.

(6) The PIC18F4550 has four built-in timer modules (timer0 to timer3), two comparator modules, and three external interrupts. It has a dual oscillator option that allows the microcontroller and USB module to run at different clock speeds. It can operate at 2.0V to 5.5V.

16X2 LCD interface PIC microcontroller circuit diagram:

Circuit-Diagram-of-16X2-LCD-Interfacing-PIC-Microcontroller.png

16X2 LCD Interface PIC Microcontroller - Circuit Description:

Resistor R1 is used to provide contrast for LCD. A 12MHz crystal is connected to OSC1 and OSC2 pins of Pic microcontroller PIC18F4550 as system clock. Capacitors C2 and C3 will act as filters for the crystal oscillator. You can use different ports or pins to connect LCD and then go to different ports, please check the datasheet whether the pin is a general purpose pin or a special function pin.

Connecting 16X2 LCD to PIC:

Interfacing an LCD to a PIC is no different than interfacing to an 8051. Only the pins, registers, and architecture used for the interface will be different. When we look at the program, the functions such as initialization, sending data to the LCD, etc. are almost the same.

In the pic programming for initializing LCD, R/W pin should be low to write data, enable pin should be high, register select pin (RS) should be high to write data. To send command, RS should be low, R/W pin should be low, enable pin should be high.

Initialize LCD function:

lcdcmd(0x38); //Configure LCD to 8-bit mode, 2 lines of 5×7 font

lcdcmd(0x0C); //Display on and off cursor

lcdcmd(0x01); //Clear the display screen

lcdcmd(0x06); //Add cursor

lcdcmd(0x80); //Set the cursor position to the first row and first column

Send command to LC:

rs=0; register select pin is low level.

rw=0; The read/write pin is also used to write commands to the LCD.

en=1; enable pin is high level.

Sending data to the LCD:

rs=1; register select pin is high level.

rw=0; The read/write pin is also used to write commands to the LCD.

en=1; enable pin is high level.

Programming steps:

Install MPLAB in your system and create a new project, when selecting the device and family choose the PIC18F family and add the PIC18F4550 controller to your project.

Select the compiler you have installed and add the file to your project. After adding the file paste the code given below and run it. As it is a precompiled and tested program, you will not find any error.

After the program compiles without errors, use the PICKIT2 or PICKIT3 programmer/debugger to dump the program to your board.

If you are not using PICKIT then just compile the code and make the HEX file to program your PIC microcontroller using this HEX file.

LCD and PIC18F4550 interface program:

#define rs LATA.F0

#define rw LATA.F1

#define in LATA.F2

//LCD data pin

#define lcdport LATB

void lcd_init();

void lcdcmd(unsigned char);

void lcddata(unsigned char);

unsigned char data[20]=”hello world”;

unsigned int i=0;

void main(void)

{

TRISA=0; //Configure port A as an output port

YEARS=0;

TRISB=0; //Configure port B as output port

LATB=0;

lcd_init(); // LCD initialization

while(data[i]!=’′)

{

lcddata(data[i]); // Call lcddata function

i++;

Delay_ms(300);

}

}

void lcd_init()

{

lcdcmd(0x38);

lcdcmd (0x0C);

lcdcmd(0x01);

lcdcmd(0x06);

lcdcmd(0x80);

}

void lcdcmd(unsigned char cmdout)

{

lcdport=cmdout;

rs=0;

rw=0;

en=1;

Delay_ms(10);

en=0;

}

void lcddata(unsigned char dataout)

{

lcdport=dataout;

rs=1;

rw=0;

en=1;

Delay_ms(10);

en=0;

}

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号