In this tutorial I will print/display ASCII characters on 16×2 LCD using pic16f877 microcontroller. The LCD is interfaced with the pic microcontroller in 8-bit mode. The code is written in c language. High-tech C compiler is used to compile code and the code is written in Mp-lab ide. Interfacing 16×2 LCD with pic microcontroller and displaying characters on LCD is very easy. Only the slightly complex part is how to generate/display ASCII characters on LCD. Well, you don't need to generate ASCII characters, they are already present in the LCD 16x2 (HD44780) Ram controller, like other characters and numbers . You just need to know how to invoke ASCII characters to be displayed on the LCD screen.
Project requirements
- Microchip Pic16f877 Microcontroller.
- 16×2 character LCD.
- Crystal 20MHz
- Power supply
- Breadboard or PCB (Printed Circuit Board) Design
- Variable potentiometer/resistor (to set LCD contrast)
If you are new to the field of microcontrollers and LCDs and don't know about LCD pinout, working and internal structure of LCD, then follow the following tutorials. They will enlighten you about the working of the 16×2 LCD. Will you learn how to display characters on LCD? Difference between commands and data sent to LCD? It will also explain how to use the 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 16×2 LCD data pins. This means that 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 D-Port bits. All other connections are normal connections, applying +5 volts to the microcontroller and LCD. You can see the circuit diagram given below. Rs (Register Select) is connected to D-Port Pin No. 6. En (Enable) is connected to D-Port Pin No. 7. The read/write pin is grounded. The LCD will always remain in the recording state as long as we ground the R/W pin.

16×2 LCD with 8-bit mode interface with microcontroller pic
Project code
Coming to the code part, I first include the htc.h header file. If you are using a high-end C compiler, always include this library, this library is required to be included in every project that will be compiled with the high-end C compiler. It contains compiler directives, etc. Next, the oscillator frequency is set, which is 20 MHz. Next, the individual pins of the D-Port are set. These pins are used to provide control signals to the LCD. The delay function is used to generate some arbitrary delay when needed. LCDCMD function is sending commands to LCD with control signals. The display function is sending data to the LCD with control signals. The lcdint function is initializing our LCD (8-bit mode, display on, cursor off, etc.).
Important instruction
In the main function, two instructions invoke 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 the result in the 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 equals 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 running i=i-0x30 i contains 0x00. So we are at the starting address of the ASCII characters. Now increase the address one by one and display the ASCII character associated with that address on the LCD screen.
|
HD44780 ASCII Character Addresses
|
Observation that the ascii characters of the HD44780 controller differ from the standard ascii characters. The HD44780 controller contains ASCII characters in the format given on the right side. Some addresses are also null. So don't be confused when you see the characters below displayed on your LCD screen.
The total digits in the RAM memory of the 16×2 LCD is 256. So I decided to display all of them. It contains ASCII, numeric, alphabetic and special (Chinese) characters. Some addresses are null, so nothing will be displayed on the LCD screen at those addresses.
Download the project, code and simulation files. The code is written in c language using Mp-lab software and high-end c compiler. The simulation is done in Proteaus 8.0. Please give us your feedback on the project.
Project Code/Files
