In this tutorial i am going to print/display ASCII characters on 16×2 lcd using pic16f877 microcontroller. Lcd is interfaced with pic microcontroller in 8-bit mode. Code is written in c language. High tech c compiler is used to compile code and code is written in Mp-lab ide. Interfacing 16×2 lcd with pic microcontroller and displaying characters on lcd is very easy. Only the portion which is little bit complex is how to generate/display ASCII characters on lcd. Well you don’t need to generate ASCII characters they are already present in the 16×2 lcd controller (HD44780) Ram, like other characters and numbers. You just need to know how to invoke the ASCII characters to be displayed on the lcd screen.
If you are new in the field of microcontrollers and lcd’s and don’t know about lcd pin out, working and internal structure of lcd then please go through the following tutorials. They will clear you about the working of 16×2 lcd. You will learn how to display characters on lcd? Difference between commands and data send to lcd? It will also explain you about how to use lcd in 4-bit and 8-bit mode.
The circuit diagram of the project is given below. Port-B of Pic16f877 microcontroller is connected with data pins of 16×2 lcd. It means Port-B is used to send commands and data to 16×2 lcd. Lcd control signals(read/write, enable, register select) are provided using individual bits of Port-D. All the other connections are normal connections applying +5 volts to microcontroller and lcd. You can see the circuit diagram given below. Rs(Register select) is connected to Port-D Pin#6. En(Enable) is connected to Port-D Pin#7. Read/Write pin is grounded. Lcd will always remain in write state since we made the R/W pin ground.
Project Code
Coming to the code portion, I first include the header file htc.h. If you are using high tech c compiler always include this library, this library is necessary to be included in every project that is going to be compiled with high tech c compiler. It contains the compilers directives etc. Then the frequency of the oscillator is defined which is 20 MHz. Then individual pins of Port-D are defined. These pins are used to provide control signals to lcd. delay() function is used to generate some arbitrary delay where necessary. lcdcmd() function is sending commands to lcd with control signals. display() function is sending data to lcd with control signals. lcdint() function is initializing our lcd(8-bit mode, display on ,cursor off etc).
Important Instruction
In the main function two instructions are invoking ascii characters. The instruction i=j/10; where j is integer(int) and i is character(char). Now when we divide two integers and save result in character(char) variable. The result is stored in ascii format. Since j is 0 and dividing 0 by 10 gives 0. So i contains 0, ASCII value of zero.
The ascii character 0 is present at address 0x30. To go to address 0x00 negate 0x30 from 0x30. The instruction i=i-0x30; is doing the same job. First i contains 0x30 after executing i=i-0x30 i contains 0x00. Hence we are at starting address of ascii characters. Now increment the address one by one and display the ascii character associated with that address on the lcd screen.
Notethat the ascii characters of the HD44780 controller differs from the standard ascii characters. The HD44780 controller contains ascii characters in the format given on the right side. Some addresses are also void. So don’t get confused when you see the characters like below displayed on your lcd screen.
Total digits in ram of 16×2 lcd are 256. So i decided to display them all. It contains ASCII, numeric, alphabet and special characters(Chinese). Some address are void so nothing will display on the lcd screen across these addresses.
#include<htc.h>
#define_XTAL_FREQ20000000//Oscillator frequency
#defineen RD7 //Lcd Controlling Pins
#definers RD6
#definerw RD5
voiddelay(unsignedint time) //Time delay function
{
unsignedint i,j;
for(i=0;i< time;i++)
for(j=0;j< 5;j++);
}
//Function for sending values to the command register of LCD
voidlcdcmd(unsignedchar value)
{
PORTB=value;
rs= 0; //register select-rs
rw = 0; //read-write-rd
en = 1; //enable-e
delay(50);
en=0; //enable-e
delay(50);
}
//Function for sending values to the data register of LCD
voiddisplay(unsignedchar value)
{
PORTB=value;
rs= 1; //register select-rs
rw= 0; //read-write-rd
en= 1; //enable-e
delay(500);
en=0; //enable-e
delay(50);
}
//function to initialize the registers and pins of LCD
//always use with every lcd of hitachi
voidlcdint(void)
{
TRISB=0x00; //Port 1 is used as output port
TRISD5=0; //Lcd Controlling pins as output
TRISD6=0;
TRISD7=0;
delay(15000);
display(0x30);
delay(4500);
display(0x30);
delay(300);
display(0x30);
delay(650);
lcdcmd(0x38);
delay(50);
lcdcmd(0x0C);
delay(50);
lcdcmd(0x01);
delay(50);
lcdcmd(0x06);
delay(50);
lcdcmd(0x80);
delay(50);
}
voidmain()
{
char i;
unsignedint j,k;
j=0;k=0;
i=j/10;
i=i-0x30;
lcdint();
while(j!=255){
display(i);
j++;i++;k++;
if(k==15){ //Displaying characters on first line of lcd
lcdcmd(0xC0);
}
if(k==31){ //Displaying characters on second line of lcd
lcdcmd(0x80);
k=0;
lcdcmd(0x01); //Clear Lcd
}
delay(12000);
}//End While
}
Download the project files , code and simulation. The code is written in c language using Mp-lab software and high tech c compiler. The simulation is made in proteaus 8.0. Please give us your feed back on the project.
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.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. ACCEPTCheck Privacy Policy
Manage consent
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.