DISPLAYING CUSTOM CHARACTERS ON LCD USING PIC16F877A
Reference: https://electrosome.com/custom-characters-lcd-pic-mikroc/
NOTE: Please go through the tutorial on Interfacing Character LCD with PIC Microcontroller – MicroC Pro.
Let us first discuss some of the basic concepts and terminologies that we are going to use in displaying custom characters on our 16x2 LCD. Basically, the LCD is consist of 3 memory blocks called CGROM, DDRAM, and CGRAM.
Character Generation ROM (CGROM)
This is the memory which holds the 5×8 or the 5×10 dot patterns of predefined characters in the LCD. It can create 208 5x8 character dot patterns and 32 5x10 character dot patterns.
Display Data RAM (DDRAM)
Just like our personal computers, the LCD also has a RAM and it is called DDRAM. This is the memory which holds the character data which is currently displayed on the LCD. Its capacity is 80x8 bits.
Character Generation RAM (CGRAM)
This memory works similar to CGROM as this is a RAM we can modify its data anytime. We can store our custom character patterns in this memory through program. We can store up to eight 5x8 character dot patterns and four 5x10 character dot patterns in this memory.
The starting address of the CGRAM is from 00-07.
Creating your own Custom Characters
- On your Microsoft Excel software create a 5x8 box just like the picture above. That will serve as our 5x8 dot pattern as seen on our LCD.
- To create your custom character, just fill the box that will satisfy your design.
- The shaded boxes are labeled 1 and the unshaded boxes are labeled as 0. Since we are processing 8 bits of data, the first column of boxes serves as our higher bit data and the remaining column of boxes is our lower bit data.
- On our example above, just convert the binary form of data into decimal.
MODIFIED CODE:
// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
const char character0[] = {0,0,10,31,31,14,4,0};
const char character1[] = {14,27,17,17,17,17,31,0};
const char character2[] = {14,27,17,17,17,31,31,0};
const char character3[] = {14,27,17,17,31,31,31,0};
const char character4[] = {14,27,17,31,31,31,31,0};
const char character5[] = {14,31,31,31,31,31,31,0};
const char character6[] = {0,4,2,31,2,4,0,0};
const char character7[] = {0,0,14,17,17,10,27,0};
void CustomChar0() {
char i;
Lcd_Cmd(64);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character0[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar1() {
char i;
Lcd_Cmd(72);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character1[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar2() {
char i;
Lcd_Cmd(80);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character2[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar3() {
char i;
Lcd_Cmd(88);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character3[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar4() {
char i;
Lcd_Cmd(96);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character4[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar5() {
char i;
Lcd_Cmd(104);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character5[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar6() {
char i;
Lcd_Cmd(112);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character6[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar7() {
char i;
Lcd_Cmd(120);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character7[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void main() {
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
//Send Custom Charactors to CGRAM
CustomChar0();
CustomChar1();
CustomChar2();
CustomChar3();
CustomChar4();
CustomChar5();
CustomChar6();
CustomChar7();
//End
//Display Custom Characters
Lcd_Chr(1,1,0);
Delay_ms(1000);
Lcd_Chr(1,2,1);
Delay_ms(1000);
Lcd_Chr(1,3,2);
Delay_ms(1000);
Lcd_Chr(1,4,3);
Delay_ms(1000);
Lcd_Chr(1,5,4);
Delay_ms(1000);
Lcd_Chr(1,6,5);
Delay_ms(1000);
Lcd_Chr(1,7,6);
Delay_ms(1000);
Lcd_Chr(1,8,7);
Delay_ms(1000);
}
NOTE: Some of the syntax of the code might not build on your project because of the version of MikroC programmer you are using. Just go to the Help and go to the LCD Library of your MikroC programmer to check the right syntax of your program. It is also nice to drop some of your questions on the comment section below.