This PIC16F877 microcontroller tutorial provides very detailed and comprehensive LCD[1]Β code.
Using PIC16 simulator (Proteus) you can verify this LCD code and change it according to your needs.Β This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the βDownloadsβΒ section at the bottom of this page.
It is assumed that you know how to blink an LED with PIC16F877Β microcontroller. If you donβt then please readΒ this pageΒ first, before proceeding with this article.
The following diagram (made inΒ Proteus) shows the PIC microcontroller circuit diagram.
In the above figure, RB0 pin is being used asΒ RSΒ pin for LCD. RB1 pin is used asΒ RWΒ pin,Β RB2 pin is used asΒ EnableΒ pinΒ and RB4 to RB7 pins are used asΒ Data busΒ for the LCD. When code starts running then βsaeedsolutions.blogspot.comβΒ is displayed on the first line of LCD first. Also, on the second line in the end, two custom characters are also displayed (Downward arrow and Upward arrow). Then whole screen starts to scroll towards left. Following animation shows the result of simulating the code in Proteus.
Code
The code for the main function is shown below.
In the main function, firstly LCD is initialized usingΒ InitLCD()Β function. After that, βsaeedsolutions.blogspot.comβΒ is written on the LCD screen[2].Β In this way usingΒ WriteStringToLCD()Β function, you can write any string on the LCD screen.Β GotoYXPositionOnLCD(LCD_LINE2, 38)Β statement makes the DDRAM(Data Display RAM) pointer to go to LCD line 2 second last character position, this means that next character written to LCD will be displayed at this position.Β Each line of the LCD can have 40 characters saved in its DDRAM at most and only 16 characters are displayed at a given time[3].
There are 8 custom characters[4]Β defined in the code as well. At position 0x06Β Downward ArrowΒ custom character is defined and at position 0x07Β Upward ArrowΒ custom character is defined. These two characters are displayed at last position of LCD line 2 usingΒ WriteDataToLCD(0x06)Β forΒ Downward ArrowΒ andΒ WriteDataToLCD(0x07)Β forΒ Upward Arrow. Then there is delay of 1 second usingΒ __delay_ms(1000);Β statement.
In theΒ while(1)Β loop, LCD screen is scrolled to the left by one character after every 0.2 seconds. This scrolling is achieved using the functionΒ ScrollLCD(ScrollScreen, ToLeft, 1), in which you can changeΒ ScrollScreenΒ toΒ ScrollCursorΒ if you want to scroll cursor only. You can changeΒ ToLeftΒ parameter toΒ ToRightΒ if you want to scroll towards right. You can change value 1 to anyΒ xΒ number, if you want to scroll LCD byΒ xΒ characters instead of 1 character at a time.
In the code, custom characters[4]Β defined are shown below (FromΒ LCD.hΒ file).
There are 8 custom characters defined. You can learn how to create these arrays fromΒ this link.Β Then you can replace these arrays with your own custom character arrays and those custom characters can be called from the main function if required.
In the code you can easily select the configuration for the LCD to be used. Following figure shows the pin selection code.
Here for example, you can change RB0 to RB3 if you want to attachΒ RSΒ pin of the LCD on pin36 of PIC16F877. You will also need to changeΒ LCD_RS_DirΒ to TRISB3 for RB3. Similarly, you can change other pin definitions as well.
In theΒ Selectables, there are many macros defined.Β You should uncomment or comment these macros as per requirement. For example, the above code shows that LCD is being configured inΒ DataBus4bitΒ mode, Entry mode is set to increment with each character, 16Γ2 LCD is selected, character font is 5Γ8 and custom characters are being defined.
There are many functions available inΒ LCD.cΒ file which you can use in your code. This tutorial only covers few of these functions.Β You can leave your comments in the comment section below.
Notes and References
[1] Β Here LCD means most widely used common 16Γ2 LCD, which has HD44780U controller in it.
[2] Β As shown in Figure 1,Β βsaeedsolutions.blogspot.comβΒ is displayed in theΒ ProteusΒ simulation.
[3] Β This statement is valid only for 16Γ2 LCD. For 20Γ2 LCD 20 characters are displayed on LCD screen at a given time, but only 40 characters can be saved for each LCD line in DDRAM.
[4] Β Custom character are special characters, which you can store in the CGRAM(Character generator RAM) of LCD. These characters can be designed to display custom made character. For details on how to make these custom characters, you can checkΒ this link.
Downloads
LCD interfacing code usingΒ PIC16F877 was compiled inΒ MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.Β To download code andΒ ProteusΒ simulationΒ click here.
For more detail: PIC16F877 LCD code and Proteus simulation