Loads eight custom characters to LCD

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /* Lcd_Custom_Char
00003 
00004 Author: Lluis Nadal.
00005 Date: 15th October 2010.
00006 
00007 Up to 8 characters can be programmed in CGRAM, but because CGRAM is a RAM, characters are lost
00008 on power off and must be reloaded on power on.
00009 Assuming 2x16 LCD with HD44780 Hitachi controller compatible.
00010 
00011 */
00012 
00013 #include "mbed.h"
00014 #include "TextLCD.h"
00015 
00016 
00017 // Defines LCD connections.
00018 TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4, d5, d6, d7
00019 
00020 // Defines 8 custom characters.
00021 char custom_char[8][8] = {
00022     {0x07,0x08,0x1F,0x08,0x1F,0x08,0x07,0x00}, // Euro sign.
00023     {0x00,0x0E,0x11,0x11,0x11,0x0A,0x1B,0x00}, // Ohm sign.
00024     {0x00,0x00,0x00,0x12,0x12,0x12,0x1C,0x10}, // Micro sign.
00025     {0x00,0x00,0x1F,0x0A,0x0A,0x0A,0x0A,0x00}, // Pi sign.
00026     {0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00}, // Degree sign.
00027     {0x0E,0x04,0x0E,0x15,0x015,0x0E,0x04,0x0E},// Phi sign.
00028     {0x04,0x0E,0x15,0x04,0x04,0x04,0x04,0x04}, // Arrow up.
00029     {0x04,0x04,0x04,0x04,0x04,0x15,0x0E,0x04}  // Arrow down.
00030 };
00031 
00032 
00033 // Defines LCD bus to write data.
00034 BusOut Lcd_pins(p27, p28, p29, p30); // d4, d5, d6, d7
00035 
00036 DigitalOut rs_pin(p24); // LCD pin rs (register select.)
00037 DigitalOut e_pin(p26);  // LCD pin e (enable).
00038 
00039 
00040 // Because we use 4 bit LCD, data must be sent in two steps.
00041 void writePort(int value) {
00042 
00043     Lcd_pins = value >> 4;  // Shifts 4 bit right.
00044     wait(0.000040f); // Wait 40us.
00045     e_pin = 0;
00046     wait(0.000040f);
00047     e_pin = 1;
00048     Lcd_pins = value;
00049     wait(0.000040f);
00050     e_pin = 0;
00051     wait(0.000040f);
00052     e_pin = 1;
00053 }
00054 
00055 
00056 
00057 int main() {
00058 
00059 
00060     lcd.cls();
00061     lcd.printf("Loading...");
00062     wait(2);
00063 
00064 
00065     for (int j=0; j<8; j++) {
00066 
00067         rs_pin = 0; // We send a command.
00068 
00069         /* 0X40 is the initial CGRAM address. Because each character needs a total amount of 8 memory 
00070         locations, we increment addres in 8 units after each character.
00071         */
00072         writePort(0x40+8*j);
00073 
00074 
00075 // Writes data.
00076         rs_pin = 1; // We send data.
00077 
00078 
00079         for (int i=0; i<8; i++) {
00080             writePort(custom_char[j][i]);
00081         }
00082     }
00083 
00084 
00085 
00086     lcd.cls();
00087     wait(0.010);
00088 
00089     lcd.printf("Custom character");
00090     lcd.locate(0,1);
00091 
00092 // Prints loaded custom characters. ASCII codes 0 to 7.
00093     for (int j=0; j<8; j++) {
00094         lcd.putc(j);
00095     }
00096 
00097 }