Friday, July 24, 2015

HD44780 lcd16x2 - custom character

The HD44780 has character generator RAM (CGRAM) to store user-defined character pattern. There are two font types - 5x8, 5x10. For 5x8 dots, 8 character patterns can be written, and for 5x10 dots, 4 character patterns can be written.

Since 2 lines mode is always preferred, the below setting is for font 5x8.

To understand the process of storing custom character, the correspondence between EPROM Address Data and Character Pattern (5x8 Dots) is needed to be studied.
For each custom character for 5x8 font, 8 lines of address are needed to store one character pattern.
So address, A2, A1, and A0 are the 8 selectable line position of the character pattern.
while data O4, O3, O2, O1, O0 correspond to character pattern data.
Last, the A5, A4, and A3 represent the 8 selectable address to a character.

The relationship between CGRAM Addresses, Character Codes (DDRAM) and Character
Patterns (CGRAM Data) can be better shown at figure below

The CGRAM address bits 0,1,2 are the line pattern position,
The CGRAM address bits 3,4,5 are the custom character position, it is linked with DDRAM location.

The DDRAM address bits 3 (*) indicates no effect which means address 0000 *000 can represent two locations 0000 0000, and 0000 1000. Both locations store the same custom character.

To go to CGRAM address the below instruction is used.

The address bits DB5, DB4, DB3 are custom character position,
while address bits DB2, DB1, DB0 are line pattern position.

When adding a custom character, it is only required to point to custom character position (DB5, DB4, DB3), and leave the line pattern position (DB2, DB1, DB0) equal to zero.
As the first line pattern is written, the HD44780 will automatically point to next location as (DB2, DB1, DB0) will be increased accordingly. So, pointing to next CGRAM location is not needed in next line pattern writing.

(DB5, DB4, DB3) - Custom character position
(DB2, DB1, DB0) - Line pattern position

To create custom character, use the instruction as follow:
rom char battery1[8] = {
        0b01110,
    0b01010,
    0b10001,
    0b10001,
    0b10001,
    0b10001,
    0b10001,
    0b11111
    };
rom specific that this data is saved at flash memory.

To save the custom character, use the instruction
HD44780_AddCharacter(0, battery1);


All the custom characters are stored at the specific location as shown at figure below:


HD44780_AddCharacter(0, battery1);
location 0 is upper bits 0000, lower bits 0000.

To write the custom character to lcd display, just simply use the write data to DDRAM such as:
HD44780_WriteData(0x00);

BTW, to display the  character "0", HD44780_WriteData(0x30) and HD44780_WriteData("0") will get the same result.

Finally, the display of custom character - battery. I let the led1 and led2 blinking in forever loop (just for fun).


I keep all the source code at github.





No comments:

Post a Comment