Saturday, 16 August 2014

Graphical LCD interfacing with PIC16F877 Code + Schematic + Proteus Simulation

     This tutorial is all about "How to interface Graphical LCD with PIC16F877". You may download code, schematic and Proteus simulation from the download section at the bottom of this page. 

     From this point I assumed that you know following (if not then read them before proceeding any further in this post).

  1. How to use Mplab X IDE (Click Here).
  2. How to set Configuration bits in code (Click Here).
  3. How to blink an LED (Click Here).
Proteus Simulation:

     In the following figure 1. We have proteus simulation in which I am using graphical LCD 128x64 with PIC16F877. This LCD is similar to JHD12864E (datasheet) with only addition of two pins those for LCD backlight. I have tested this code on real graphical LCD it is working.

Figure 1. Proteus Simulation.

    In the following figure 2. PortD is connected to DATA and ADDRESS bus of graphical LCD. whereas PortC higher bits are connected to LCD control signals. As you have seen the Reset (RST) pin is active low.A variable resister is for LCD contrast. MCLR pin is pulled high for micro-controller normal operation. I am using 10MHz crystal. You may see PIC16F877 datasheet for more details

Figure 2. Schematic Graphical LCD
General Details:

     As I mentioned before this graphical LCD is similar to JHD12864 (Controller K0108 type) so I would like to go briefly into details rest of it you may read from its datasheet. This LCD is split logically into two half left and right with two control pins ChipSelect1(CS1) and ChipSelect2 (CS2) respectively. CS1 is responsible for displaying in left half and CS2 is responsible for displaying in right half. further more each chip has 8 vertical sections named as pages (page 0~7). Every single page is further divided into 64 horizontal sections named columns and each column has 8 bits. When you stack them horizontally they are 64 bytes as shown in figure 3. So if you want to display one picture having resolution of 128x64 on this LCD you have total 1024 bytes. For example (pages) times (columns) times (no. of chips) => 8*64*2 = 1024.
Figure 3. Graphical LCD explained.
     Now we should move ahead for "How to generate display Screens for 128x64 Graphical LCD". To generate code for one picture we need to have a picture of size 128x64. So Paint it in Microsoft paint. The first step is to set the size of picture go into file>Properties a dialog box will open select colour "black and white", width "128" and height "64". as shown in figure 4.

Figure 4. Pixel settings
      Now you can zoom this page and draw any picture like I have drawn following figure 5. The important step is to save this picture as "monochrome bitmap (.bmp)" format. 

Figure 5. monochrome (.bmp) picture.
     Now we have to translate this picture into its equivalent binary data or constant array of bytes so here is a software for translating this into its binaries called LCD Assistant by Radoslaw (All rights reserved for Radoslaw). You may download it (Click Here).  as shown in figure 6. To export code you need to go file>export this would generate a file open it in Notepad and copy the code and paste it in Mplab X and compile.

Figure 6. LCD Assistant.


     In the following figure 7 we have a main function in which we have InitializeGraphicalLCD() which is responsible for initializing graphical LCD according to LCD datasheet. Then we have a fill screen function this will fill full screen with all pixels ON and then a one second delay after that all screen is cleared after this I have displayed a complete picture on LCD that I have generated as shown above in this post.

Figure 7. main function.
     Following figure 8 shows the header file in which I have #defined all the page addresses, indexes, necessary commands and Pins used to interface graphical LCD. The purpose of this defines are just to write a code that looks good, managed and readable. which is good code practice I recommend you to follow these good programming practices too.

Figure 8. Graphical LCD header file.
     In the following figure 9 we have InitializeGraphicalLCD() In which I configure all LCD pins as output. Then send some necessary commands to LCD which is described in its datasheet. As you have seen that I have to write these commands to LCDs both Chips so I send BothChip as parameter to function that is responsible for writing command to LCD.

Figure 9. Initialize Graphical LCD.
     In the following figure 10 we have ChipSelection() function which is responsible for selecting the chip either 1 , 2 or both. To select chip we need to pass chip no. as parameter.

Figure 10. ChipSelection.
     In the following figure 11 we have ToggleEnablePin() function which is responsible for toggling enable pin. After every data or address send to LCD we have to toggle its Enable pin state as described in LCD datasheet.

Figure 11. Toggle Enable Pin.
     In the following figure 12 we have WriteCommandToLCD() function which is responsible for writing commands to LCD by passing command and chip no.

Figure 12. Write command to LCD.
     In the following figure 13 we have WriteDataToLCD() which is responsible for writing data to LCD by passing data and chip no. as parameter to this function.

Figure 13. write data to LCD. 
     In the following figure 14 we have WriteAddressToLCD() which is responsible for sending address value on which data is written on LCD by passing addressX and addressY valve as well as chip no. to this function as parameter.

Figure 14. write address to LCD.
     In the following figure 15 we have DisplayPicture() which is responsible for displaying complete picture on LCD by passing a address of first byte of data as you can see there is a pointer for memory location. This function writes 1024 bytes of data to LCD. 

Figure 15. Display Picture on LCD.


 You may download Code which is written in C language. Using MPLAB X IDE and compiler XC8 for 8 bit Microcontrollers. Click here

If you learned something interesting please consider donation. Which would help me to produce such content for young engineers Thank you.

Reading Suggestions:
 For all topics Click here

All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2014 IbrahimLabs. All rights reserved.

Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...