TextLCD library for controlling various LCD panels based on the HD44780 4-bit interface. Includes support for custom character bitmaps.

Dependents:   HD44780-LCD_with_Bluetooth

Fork of TextLCD by Simon Ford

Examples

Hello world in custom font

#include "mbed.h"
#include "TextLCD.h"

int main() {
    lcd=new TextLCD(D8, D9, D4, D5, D6, D7);


    char c_H[]={0x1b,0x1b,0x1b,0x1f,0x1b,0x1b,0x1b,0x1b};
    char c_e[]={0x0,0x0,0xe,0x1b,0x1f,0x18,0x18,0xf};
    char c_l[]={0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6};
    char c_o[]={0x0,0x0,0xe,0x1b,0x1b,0x1b,0x1b,0xe};
    char c_W[]={0x1b,0x1b,0x1b,0x1b,0x1f,0x1f,0x1f,0x1b};
    char c_r[]={0x0,0x0,0xe,0x1b,0x18,0x18,0x18,0x18};
    char c_d[]={0x3,0x3,0xf,0x1b,0x1b,0x1b,0x1b,0xf};

    lcd->defineChar(1, c_H);
    lcd->putc(1);
    lcd->defineChar(2, c_e);
    lcd->putc(2);
    lcd->defineChar(3, c_l);
    lcd->putc(3);
    lcd->putc(3);
    lcd->defineChar(4, c_o);
    lcd->putc(4);
    lcd->putc(32);
    lcd->defineChar(5, c_W);
    lcd->putc(5);
    lcd->putc(4);
    lcd->defineChar(6, c_r);
    lcd->putc(6);
    lcd->putc(3);
    lcd->defineChar(7, c_d);
    lcd->putc(7);

    while (1) {
    }
}

Animated busy indicator

This animation only uses 1 of the 8 custom character slots, and can be used like a regular character in the output and will automatically animate in-place. Any number of the animated characters can be displayed on the display at once using this animation technique.

#include "mbed.h"
#include "TextLCD.h"

int main() {
    lcd=new TextLCD(D8, D9, D4, D5, D6, D7);

    //The 7 animation characters
    char spin[]={   0x4,0x4,0x4,0x4,0x4,0x4,0x4,0x0,
                    0x2,0x2,0x2,0x4,0x8,0x8,0x8,0x0,
                    0x1,0x2,0x2,0x4,0x8,0x8,0x10,0x0,
                    0x0,0x0,0x3,0x4,0x18,0x0,0x0,0x0,
                    0x0,0x0,0x0,0x1f,0x0,0x0,0x0,0x0,
                    0x0,0x0,0x18,0x4,0x3,0x0,0x0,0x0,
                    0x10,0x8,0x8,0x4,0x2,0x2,0x01,0x0
                };

    //Output text.
    lcd->printf("\1 Please  wait \1");
    
    //Animation loop
    int frame=0;
    while (1) {
        lcd->defineChar(1, spin+frame*8);
        frame=(frame+1)%(sizeof(spin)/8);
        wait(.14);
    }
Committer:
daneast
Date:
Tue Nov 04 03:06:01 2014 +0000
Revision:
9:914e500624b5
Parent:
7:44f34c09bd37
Add routine to define custom character bitmaps.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:ac48b187213c 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
simon 6:e4cb7ddee0d3 2 * Copyright (c) 2007-2010, sford, http://mbed.org
simon 1:ac48b187213c 3 *
simon 1:ac48b187213c 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:ac48b187213c 5 * of this software and associated documentation files (the "Software"), to deal
simon 1:ac48b187213c 6 * in the Software without restriction, including without limitation the rights
simon 1:ac48b187213c 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:ac48b187213c 8 * copies of the Software, and to permit persons to whom the Software is
simon 1:ac48b187213c 9 * furnished to do so, subject to the following conditions:
simon 1:ac48b187213c 10 *
simon 1:ac48b187213c 11 * The above copyright notice and this permission notice shall be included in
simon 1:ac48b187213c 12 * all copies or substantial portions of the Software.
simon 1:ac48b187213c 13 *
simon 1:ac48b187213c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:ac48b187213c 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:ac48b187213c 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:ac48b187213c 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:ac48b187213c 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:ac48b187213c 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:ac48b187213c 20 * THE SOFTWARE.
simon 1:ac48b187213c 21 */
simon 1:ac48b187213c 22
simon 1:ac48b187213c 23 #include "TextLCD.h"
simon 1:ac48b187213c 24 #include "mbed.h"
simon 1:ac48b187213c 25
simon 7:44f34c09bd37 26 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5,
simon 7:44f34c09bd37 27 PinName d6, PinName d7, LCDType type) : _rs(rs),
simon 7:44f34c09bd37 28 _e(e), _d(d4, d5, d6, d7),
simon 1:ac48b187213c 29 _type(type) {
simon 1:ac48b187213c 30
simon 1:ac48b187213c 31 _e = 1;
simon 1:ac48b187213c 32 _rs = 0; // command mode
simon 1:ac48b187213c 33
simon 1:ac48b187213c 34 wait(0.015); // Wait 15ms to ensure powered up
simon 1:ac48b187213c 35
simon 1:ac48b187213c 36 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
simon 1:ac48b187213c 37 for (int i=0; i<3; i++) {
simon 1:ac48b187213c 38 writeByte(0x3);
simon 1:ac48b187213c 39 wait(0.00164); // this command takes 1.64ms, so wait for it
simon 1:ac48b187213c 40 }
simon 1:ac48b187213c 41 writeByte(0x2); // 4-bit mode
simon 1:ac48b187213c 42 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 43
simon 1:ac48b187213c 44 writeCommand(0x28); // Function set 001 BW N F - -
simon 1:ac48b187213c 45 writeCommand(0x0C);
simon 1:ac48b187213c 46 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
simon 1:ac48b187213c 47 cls();
simon 1:ac48b187213c 48 }
simon 1:ac48b187213c 49
simon 1:ac48b187213c 50 void TextLCD::character(int column, int row, int c) {
simon 1:ac48b187213c 51 int a = address(column, row);
simon 1:ac48b187213c 52 writeCommand(a);
simon 1:ac48b187213c 53 writeData(c);
simon 1:ac48b187213c 54 }
simon 1:ac48b187213c 55
simon 1:ac48b187213c 56 void TextLCD::cls() {
simon 1:ac48b187213c 57 writeCommand(0x01); // cls, and set cursor to 0
simon 1:ac48b187213c 58 wait(0.00164f); // This command takes 1.64 ms
simon 1:ac48b187213c 59 locate(0, 0);
simon 1:ac48b187213c 60 }
simon 1:ac48b187213c 61
simon 1:ac48b187213c 62 void TextLCD::locate(int column, int row) {
simon 1:ac48b187213c 63 _column = column;
simon 1:ac48b187213c 64 _row = row;
simon 1:ac48b187213c 65 }
simon 1:ac48b187213c 66
daneast 9:914e500624b5 67 void TextLCD::defineChar(int index, char *data) {
daneast 9:914e500624b5 68 if (index<0||index>7)
daneast 9:914e500624b5 69 return;
daneast 9:914e500624b5 70 writeCommand(0x40+index*8);
daneast 9:914e500624b5 71 for (int i=0; i<8; i++)
daneast 9:914e500624b5 72 writeData(*data++);
daneast 9:914e500624b5 73 }
daneast 9:914e500624b5 74
simon 1:ac48b187213c 75 int TextLCD::_putc(int value) {
simon 1:ac48b187213c 76 if (value == '\n') {
simon 1:ac48b187213c 77 _column = 0;
simon 1:ac48b187213c 78 _row++;
simon 1:ac48b187213c 79 if (_row >= rows()) {
simon 1:ac48b187213c 80 _row = 0;
simon 1:ac48b187213c 81 }
simon 1:ac48b187213c 82 } else {
simon 1:ac48b187213c 83 character(_column, _row, value);
simon 1:ac48b187213c 84 _column++;
simon 1:ac48b187213c 85 if (_column >= columns()) {
simon 1:ac48b187213c 86 _column = 0;
simon 1:ac48b187213c 87 _row++;
simon 1:ac48b187213c 88 if (_row >= rows()) {
simon 1:ac48b187213c 89 _row = 0;
simon 1:ac48b187213c 90 }
simon 1:ac48b187213c 91 }
simon 1:ac48b187213c 92 }
simon 1:ac48b187213c 93 return value;
simon 1:ac48b187213c 94 }
simon 1:ac48b187213c 95
simon 1:ac48b187213c 96 int TextLCD::_getc() {
simon 1:ac48b187213c 97 return -1;
simon 1:ac48b187213c 98 }
simon 1:ac48b187213c 99
simon 1:ac48b187213c 100 void TextLCD::writeByte(int value) {
simon 1:ac48b187213c 101 _d = value >> 4;
simon 1:ac48b187213c 102 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 103 _e = 0;
simon 1:ac48b187213c 104 wait(0.000040f);
simon 1:ac48b187213c 105 _e = 1;
simon 1:ac48b187213c 106 _d = value >> 0;
simon 1:ac48b187213c 107 wait(0.000040f);
simon 1:ac48b187213c 108 _e = 0;
simon 1:ac48b187213c 109 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 110 _e = 1;
simon 1:ac48b187213c 111 }
simon 1:ac48b187213c 112
simon 1:ac48b187213c 113 void TextLCD::writeCommand(int command) {
simon 1:ac48b187213c 114 _rs = 0;
simon 1:ac48b187213c 115 writeByte(command);
simon 1:ac48b187213c 116 }
simon 1:ac48b187213c 117
simon 1:ac48b187213c 118 void TextLCD::writeData(int data) {
simon 1:ac48b187213c 119 _rs = 1;
simon 1:ac48b187213c 120 writeByte(data);
simon 1:ac48b187213c 121 }
simon 1:ac48b187213c 122
simon 1:ac48b187213c 123 int TextLCD::address(int column, int row) {
simon 1:ac48b187213c 124 switch (_type) {
simon 1:ac48b187213c 125 case LCD20x4:
simon 1:ac48b187213c 126 switch (row) {
simon 1:ac48b187213c 127 case 0:
simon 1:ac48b187213c 128 return 0x80 + column;
simon 1:ac48b187213c 129 case 1:
simon 1:ac48b187213c 130 return 0xc0 + column;
simon 1:ac48b187213c 131 case 2:
simon 1:ac48b187213c 132 return 0x94 + column;
simon 1:ac48b187213c 133 case 3:
simon 1:ac48b187213c 134 return 0xd4 + column;
simon 1:ac48b187213c 135 }
simon 1:ac48b187213c 136 case LCD16x2B:
simon 4:bf5b706f8d32 137 return 0x80 + (row * 40) + column;
simon 1:ac48b187213c 138 case LCD16x2:
simon 1:ac48b187213c 139 case LCD20x2:
simon 1:ac48b187213c 140 default:
simon 4:bf5b706f8d32 141 return 0x80 + (row * 0x40) + column;
simon 1:ac48b187213c 142 }
simon 1:ac48b187213c 143 }
simon 1:ac48b187213c 144
simon 1:ac48b187213c 145 int TextLCD::columns() {
simon 1:ac48b187213c 146 switch (_type) {
simon 1:ac48b187213c 147 case LCD20x4:
simon 1:ac48b187213c 148 case LCD20x2:
simon 1:ac48b187213c 149 return 20;
simon 1:ac48b187213c 150 case LCD16x2:
simon 1:ac48b187213c 151 case LCD16x2B:
simon 1:ac48b187213c 152 default:
simon 1:ac48b187213c 153 return 16;
simon 1:ac48b187213c 154 }
simon 1:ac48b187213c 155 }
simon 1:ac48b187213c 156
simon 1:ac48b187213c 157 int TextLCD::rows() {
simon 1:ac48b187213c 158 switch (_type) {
simon 1:ac48b187213c 159 case LCD20x4:
simon 1:ac48b187213c 160 return 4;
simon 1:ac48b187213c 161 case LCD16x2:
simon 1:ac48b187213c 162 case LCD16x2B:
simon 1:ac48b187213c 163 case LCD20x2:
simon 1:ac48b187213c 164 default:
simon 1:ac48b187213c 165 return 2;
simon 1:ac48b187213c 166 }
simon 1:ac48b187213c 167 }