How to Display Custom Character on LCD in Embedded C
To display a custom character on an LCD in
embedded C, define the character pattern as a byte array and load it into the LCD's CGRAM using lcd_create_char() or equivalent function. Then, display it by writing the custom character's code to the LCD with lcd_write() or similar.Syntax
To display a custom character on an LCD, you typically follow these steps:
- Define the character pattern as an array of bytes, each byte representing a row of pixels.
- Load this pattern into the LCD's CGRAM (Character Generator RAM) using a function like
lcd_create_char(location, pattern). - Display the custom character by writing its location code (0-7) to the LCD using
lcd_write(location).
Each byte in the pattern usually has 5 bits for pixels (for a 5x8 LCD character).
embedded_c
uint8_t customChar[8] = { 0b00000, 0b01010, 0b10101, 0b10001, 0b10101, 0b01010, 0b00000, 0b00000 }; lcd_create_char(0, customChar); // Load pattern into CGRAM location 0 lcd_set_cursor(0, 0); // Set cursor to first row, first column lcd_write(0); // Display custom char stored at location 0
Example
This example shows how to define a smiley face custom character, load it into the LCD, and display it at the first position.
embedded_c
#include <stdint.h> #include "lcd.h" // Assume lcd.h provides lcd_create_char, lcd_set_cursor, lcd_write int main() { uint8_t smiley[8] = { 0b00000, 0b01010, 0b01010, 0b00000, 0b10001, 0b01110, 0b00000, 0b00000 }; lcd_init(); // Initialize LCD lcd_create_char(0, smiley); // Load smiley into CGRAM slot 0 lcd_set_cursor(0, 0); // Move cursor to first row, first column lcd_write(0); // Display custom char at location 0 while(1) {} return 0; }
Output
Smiley face character displayed at first row, first column of the LCD
Common Pitfalls
- Not loading the custom character into CGRAM before displaying it causes blank or wrong characters.
- Using a location outside 0-7 for CGRAM will not work because LCDs support only 8 custom chars.
- Forgetting to set the cursor position before writing the character can display it in the wrong place.
- Incorrect byte patterns can produce unexpected shapes; ensure each byte uses only the lower 5 bits for pixels.
embedded_c
/* Wrong: Using location 8 (invalid) */ lcd_create_char(8, customChar); // Invalid location, will not display /* Right: Use location 0-7 */ lcd_create_char(0, customChar); // Correct usage
Quick Reference
| Step | Description |
|---|---|
| Define pattern | Create 8-byte array with 5-bit pixel rows |
| Load pattern | Use lcd_create_char(location, pattern) with location 0-7 |
| Set cursor | Position cursor with lcd_set_cursor(row, col) |
| Display char | Write location code with lcd_write(location) |
Key Takeaways
Define your custom character as an 8-byte array with 5-bit pixel rows.
Load the character into LCD CGRAM at a location between 0 and 7 before displaying.
Set the LCD cursor to the desired position before writing the custom character code.
Only 8 custom characters can be stored at once on typical 16x2 or 20x4 LCDs.
Verify your byte patterns carefully to get the correct shape on the LCD.