CGRAM and Character Building

As already explained, all character based LCD of type HD44780 has CGRAM area to create user defined patterns. For making custom patterns we need to write values to the CGRAM area defining which pixel to glow. These values are to be written in the CGRAM adress starting from 0x40. If you are wondering why it starts from 0x40? Then the answer is given below.





Bit 7 is 0 and Bit 6 is 1, due to which the CGRAM adress command starts from 0x40, where the address of CGRAM (Acg) starts from 0x00. CGRAM has a total of 64 Bytes. When you are using LCD as 5x8 dots in function set then you can define a total of 8 user defined patterns (1 Byte for each row and 8 rows for each pattern), where as when LCD is working in 5x10 dots, you can define 4 user defined patterns.

Lets take an of bulding a custom pattern. All we have to do is make a pixel-map of 7x5 and get the hex or decimal value or hex value for each row, bit value is 1 if pixel is glowing and bit value is 0 if pixel is off. The final 7 values are loaded to the CGRAM one by one. As i said there are 8 rows for each pattern, so last row is usually left blank (0x00) for the cursor. If you are not using cursor then you can make use of that 8th row also. so you get a bigger pattern.


To explain the above explaination in a better way. I am going to take an example. Lets make a "Bell" pattern as shown below.



Now we get the values for each row as shown.

 Bit: 4 3 2 1 0 - Hex
Row1: 0 0 1 0 0 - 0x04
Row2: 0 1 1 1 0 - 0x0E
Row3: 0 1 1 1 0 - 0x0E
Row4: 0 1 1 1 0 - 0x0E
Row5: 1 1 1 1 1 - 0x1F
Row6: 0 0 0 0 0 - 0x00
Row7: 0 0 1 0 0 - 0x04
Row8: 0 0 0 0 0 - 0x00

We are not using row 8 as in our pattern it is not required. if you are using cursor then it is recommended not to use the 8th row. Now as we have got the values. We just need to put these values in the CGRAM. You can decided which place you want to store in. Following is the memory map for custom patterns in CGRAM.

Memory Map
Pattern No.CGRAM Address (Acg)
10x00 - 0x07
20x08 - 0x0F
30x10 - 0x17
40x18 - 0x1F
50x20 - 0x27
60x28 - 0x2F
70x30 - 0x37
80x38 - 0x3F

We can point the cursor to CGRAM address by sending command, which is 0x40 + CGRAM address (For more information please see Table 4 in commands section). Lets say we want to write the Bell pattern at second pattern location. So we send the command as 0x48 (0x40 + 0x08), and then we send the pattern data. Below is a small programming example to do this.

;LCD Ports are same as discussed in previous sections

LCD_build:
         mov   A,#48H         ;Load the location where we want to store
         acall LCD_command    ;Send the command
         mov   A,#04H         ;Load row 1 data
         acall LCD_senddata   ;Send the data
         mov   A,#0EH         ;Load row 2 data
         acall LCD_senddata   ;Send the data
         mov   A,#0EH         ;Load row 3 data
         acall LCD_senddata   ;Send the data
         mov   A,#0EH         ;Load row 4 data
         acall LCD_senddata   ;Send the data
         mov   A,#1FH         ;Load row 5 data
         acall LCD_senddata   ;Send the data
         mov   A,#00H         ;Load row 6 data
         acall LCD_senddata   ;Send the data
         mov   A,#04H         ;Load row 7 data
         acall LCD_senddata   ;Send the data
         mov   A,#00H         ;Load row 8 data
         acall LCD_senddata   ;Send the data
         ret                  ;Return from routine

The above routine will create bell character at pattern location 2. To display the above generated pattern on LCD, simply load the pattern location (0,1,2,...7) and call the LCD_senddata subroutine. Now we can also write the above routine in C as...

//LCD Ports are same as discussed in previous sections
void LCD_build(){
	LCD_command(0x48);       //Load the location where we want to store
	LCD_senddata(0x04);      //Load row 1 data
	LCD_senddata(0x0E);      //Load row 2 data
	LCD_senddata(0x0E);      //Load row 3 data
	LCD_senddata(0x0E);      //Load row 4 data
	LCD_senddata(0x1F);      //Load row 5 data
	LCD_senddata(0x00);      //Load row 6 data
	LCD_senddata(0x04);      //Load row 7 data
	LCD_senddata(0x00);      //Load row 8 data
}


I think now most of you find programing in C more simple than assembly. We can also summarize the above in a simple small routine so that you can simply call the build routine providing a pointer to array containing the build data. Below example shows how to do it.
//Input:
//     location: location where you want to store
//               0,1,2,....7
//     ptr: Pointer to pattern data
//
//Usage:
//     pattern[8]={0x04,0x0E,0x0E,0x0E,0x1F,0x00,0x04,0x00};
//     LCD_build(1,pattern);
//
//LCD Ports are same as discussed in previous sections

void LCD_build(unsigned char location, unsigned char *ptr){
      unsigned char i;
      if(location<8){
          LCD_command(0x40+(location*8));
          for(i=0;i<8;i++)
             LCD_senddata(ptr[ i ]);
     }

}

Displaying Custom Character

Custom characters are assigned fixed display codes from 0 to 7 for pattern stored in location pointed by CGRAM address 0x40, 0x48, 0x50... and so on. So if user wants to display first pattern (@0x40), simply call.
LCD_senddata(0); // For pattern @0x40
LCD_senddata(1); // For pattern @0x48
LCD_senddata(2); // For pattern @0x50
// so on...


Similarly in assembly:
mov   A,#0H ; For pattern @40H
acall LCD_senddata
mov   A,#1H ; For pattern @48H
acall LCD_senddata
mov   A,#2H ; For pattern @50H
acall LCD_senddata
; and so on..


So the above example shows how to simpify most of your work. To make easy for you to find the values for custom patterns. You can make use of Custom Character Calculator given below.

BitmapDecimalHex

List of bitmap values:
Dec:
Hex:

  

Share it! Like it!

Conversation

Mon Jun 12 2017, 02:36 PM
madhusoodanan
how to receive string using 8051
ajay_bhargav
Thu Jun 08 2017, 12:48 PM
@Vinoth post in forum.
Wed Jun 07 2017, 01:40 AM
vinothkannan
how interface eprom in this code
Thu May 25 2017, 12:25 AM
Monir Hossain
Can i change the 7-Segment are common pin ? And how to?
Mon May 08 2017, 07:45 AM
ivan muvunyi
please can you send me wiring diagram for automatic control of 3 phase induction motor for production of maize flour
Wed May 03 2017, 03:39 PM
imran qureshi
hello friend
Sun Apr 30 2017, 12:49 AM
Lijin G Varghese
Can u send me the circuit diagram for Program to interface GSM Module with 8051 microcontroller (AT89C51) using PC and LCD
Fri Apr 28 2017, 10:47 AM
SANDEEP T S
I need pcb wizard 3.5 version kindly send valid link of yours
ExperimenterUK
Mon Apr 17 2017, 06:56 PM
Sorry..We had a problem with registration.Confirmation emails were not sent.Please re-register
Thu Apr 13 2017, 02:29 PM
Piyush Aughad
I want circuit & program for radar detection using Microcontroller 8051

Downloads

Comments

richard1976
Sat Jun 24 2017, 05:48 PM
DruvaN
Wed Jun 21 2017, 11:44 AM
lalala
Tue Jun 20 2017, 11:05 PM
Siddh@
Tue Jun 20 2017, 07:47 AM
firdaus0hsem
Sat Jun 17 2017, 04:53 PM
amir9297
Fri Jun 16 2017, 05:44 AM
madhusoodhanan
Mon Jun 12 2017, 07:31 PM
Aminookano
Sun Jun 11 2017, 05:50 AM

Online

Guests: 52, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37847
Newest member: richard1976
Forum Activity
richard1976 Avatar
Posted By richard1976
Sat Jun 24 2017, 08:47 PM
Hi can you tell me how to send and recieve usart d...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Jun 24 2017, 08:24 PM
HiUnfortunately there is too much code here to wor...
richard1976 Avatar
Posted By richard1976
Sat Jun 24 2017, 06:01 PM
hi i have written this programme and can send data...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Jun 23 2017, 08:19 PM
>Should I need to check PWR_VLD bits every time on...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Jun 23 2017, 07:42 PM
I would say yes, but data transfer will be slower ...
Utsavi Avatar
Posted By Utsavi
Thu Jun 22 2017, 12:14 PM
Hello ExperimenterUK, Thanks for your support. Fro...
DruvaN Avatar
Posted By DruvaN
Wed Jun 21 2017, 11:51 AM
Is it possible to Use 8051 bit-banging method to p...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Jun 20 2017, 06:27 PM
You could start by looking at this tutorial on ke...
firdaus0hsem Avatar
Posted By firdaus0hsem
Sat Jun 17 2017, 04:58 PM
I have an issue regarding how to use 4x3 keypad to...
amir9297 Avatar
Posted By amir9297
Fri Jun 16 2017, 05:47 AM