Interfacing 16x2 LCD display with PIC18F4520 controller
The LCD display is also one of the most important and most used peripheral devices in embedded systems. Whenever we required to display some information or some status we usually prefer an LCD display.
In this article, we will talk about how to interface a 16x2 LCD display to the PIC18F4520 controller.
We will cover this article in the following steps:-
- About the project
- Required components
- Software tools
- Circuit diagram
- Writing code for the controller
- Upload the Hex file to the controller
- Simulating the project
About the project
Required components
- PIC18F4520 controller
- LM016L (16x2 LCD display)
Software tools
The software tools that require are
- Proteus 8 professional
- MPlab IDE
Circuit diagram
The circuit diagram for the LCD interfacing is given below.
Here, the rs, rw, en pin of the LCD display is connected to Port C0, Port C1, Port C2 and Port D is connected from D0 to D7 pin.
Writing code for the controller
Now, after connecting all the required components, it's time to write the code for the controller.
The code for the controller is given below
#include <xc.h>
#define _XTAL_FREQ 20000000
#define rs RC0
#define rw RC1
#define en RC2
void lcd_data(unsigned char data);
void lcd_cmd(unsigned char command);
void lcd_string(const unsigned char *string, unsigned char length);
void lcd_initialize();
void main()
{
TRISD=0;
TRISC0=0;
TRISC1=0;
TRISC2=0;
lcd_initialize();
while(1)
{
lcd_cmd(0x01);
lcd_cmd(0xc7);
lcd_data('3');
__delay_ms(1000);
lcd_cmd(0xc7);
lcd_data('2');
__delay_ms(1000);
lcd_cmd(0xc7);
lcd_data('1');
__delay_ms(1000);
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_string("Code Somenplus",14);
__delay_ms(1000);
}
}
void lcd_data(unsigned char data)
{
PORTD=data;
rs=1;
rw=0;
en=1;
__delay_ms(5);
en=0;
}
void lcd_cmd(unsigned char command)
{
PORTD=command;
rs=0;
rw=0;
en=1;
__delay_ms(5);
en=0;
}
void lcd_string(const unsigned char *string, unsigned char length)
{
unsigned char i;
for(i=0;i<length;i++)
{
lcd_data(string[i]);
}
}
void lcd_initialize()
{
lcd_cmd(0x38);
lcd_cmd(0x06);
lcd_cmd(0x0c);
lcd_cmd(0x01);
}
After writing the above code all you need is to compile, run and generate the Hex file.
CONNECT WITH US