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

sahar7478
Fri Dec 08 2017, 12:47 PM
can i get gsm a6 library? can anybody help? tia
Faiqa
Sat Nov 25 2017, 08:32 AM
Can i get the code of Monitering and controlling of green house effect? ... Can anyone help me ?
Wed Nov 08 2017, 01:35 PM
Anirudh jain
I want to burn code in atmel 89s51 24pc 0329 but cannot get the connection with the burner. Can somebody provide me with the circuit diagram for connecting controller to burner. I'm using usb burner
Thu Nov 02 2017, 11:44 AM
karpal
write a program to exchange the content of b register with external RAM address 02CFH help me
Wed Nov 01 2017, 05:47 PM
sanae
i am using the DHT11 but i'm getting some errors in the simulate the programe in proteus 8 professional,: 1)cannot find model file 'DHTXX.MDF'.2)simulation failed due to netlist linker error(s).
Fri Sep 29 2017, 02:45 AM
Maaz
i am doing the same project on verilog please contact me [email protected]
Wed Sep 20 2017, 12:36 AM
Ted
In a missing pulse detector using a 555 timer connected as a monostable vibrator with a PNP transistor,how does the positve input pulse turn on the PNP transistor?
Sun Sep 03 2017, 02:29 PM
martymarty
How do I connect an <NOKIA 1100 LCD > to an atmega328P ?
poojab51296
Sun Aug 20 2017, 10:23 AM
i am intending to interface sd card with STM32F4 and want to write a file in sd card using SPI protocol but using HAL only. Can anyone help me PLEASE!!!!
poojab51296
Sun Aug 20 2017, 10:22 AM
hello

Downloads

Comments

roopay
Wed Dec 13 2017, 07:11 AM
janvimandeep
Mon Dec 11 2017, 01:05 PM
fernanmonroy2
Mon Dec 11 2017, 08:33 AM
sahar7478
Fri Dec 08 2017, 12:34 PM
zhudao
Tue Dec 05 2017, 06:14 AM
chetan6379
Sat Dec 02 2017, 03:42 PM
HaloSpartanOdst
Fri Dec 01 2017, 12:05 PM
workzone
Sat Nov 25 2017, 01:13 PM

Online

Guests: 62, Members: 0 ...

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

Members: 37928
Newest member: roopay
Forum Activity
fernanmonroy2 Avatar
Posted By fernanmonroy2
Mon Dec 11 2017, 08:40 AM
Que significa que el contador de programa abordar...
zhudao Avatar
Posted By zhudao
Wed Dec 06 2017, 03:08 AM
I think either your 120 - 5 volt adapter, or the L...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Dec 05 2017, 08:12 PM
I think either your 120 - 5 volt adapter, or the L...
zhudao Avatar
Posted By zhudao
Tue Dec 05 2017, 06:41 AM
Hi to all, in the period time I'm doing an project...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Nov 20 2017, 10:18 PM
Here is a version of the tutorial that reads a key...
Ansh12 Avatar
Posted By Ansh12
Mon Nov 20 2017, 02:15 AM
As a starting point, can you tell us how much of t...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Nov 19 2017, 08:35 PM
As a starting point, can you tell us how much of t...
Ansh12 Avatar
Posted By Ansh12
Fri Nov 17 2017, 04:07 AM
I am looking keypad interfacing program. I was rea...
JardCrocker Avatar
Posted By JardCrocker
Wed Nov 08 2017, 05:29 PM
Hi...i am a new user here. I was also facing the s...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Nov 07 2017, 07:13 PM
> It employs a fairly advanced technique to detect...