Updated for more display types. Fixed memoryaddress confusion in address() method. Added new getAddress() method. Added support for UDCs, Backlight control and other features such as control through I2C and SPI port expanders and controllers with native I2C and SPI interfaces. Refactored to fix issue with pins that are default declared as NC.

Dependents:   GPSDevice TestTextLCD SD to Flash Data Transfer DrumMachine ... more

Fork of TextLCD by Simon Ford

Example

Hello World! for the TextLCD

#include "mbed.h"
#include "TextLCD.h"
 
// Host PC Communication channels
Serial pc(USBTX, USBRX); // tx, rx
 
// I2C Communication
I2C i2c_lcd(p28,p27); // SDA, SCL
 
// SPI Communication
SPI spi_lcd(p5, NC, p7); // MOSI, MISO, SCLK

//TextLCD lcd(p15, p16, p17, p18, p19, p20);                // RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=HD44780
//TextLCD_SPI lcd(&spi_lcd, p8, TextLCD::LCD40x4);   // SPI bus, 74595 expander, CS pin, LCD Type  
TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD20x4);  // I2C bus, PCF8574 Slaveaddress, LCD Type
//TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2, TextLCD::WS0010); // I2C bus, PCF8574 Slaveaddress, LCD Type, Device Type
//TextLCD_SPI_N lcd(&spi_lcd, p8, p9);               // SPI bus, CS pin, RS pin, LCDType=LCD16x2, BL=NC, LCDTCtrl=ST7032_3V3   
//TextLCD_I2C_N lcd(&i2c_lcd, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3); // I2C bus, Slaveaddress, LCD Type, BL=NC, LCDTCtrl=ST7032_3V3  

int main() {
    pc.printf("LCD Test. Columns=%d, Rows=%d\n\r", lcd.columns(), lcd.rows());
    
    for (int row=0; row<lcd.rows(); row++) {
      int col=0;
      
      pc.printf("MemAddr(Col=%d, Row=%d)=0x%02X\n\r", col, row, lcd.getAddress(col, row));      
//      lcd.putc('-');
      lcd.putc('0' + row);      
      
      for (col=1; col<lcd.columns()-1; col++) {    
        lcd.putc('*');
      }
 
      pc.printf("MemAddr(Col=%d, Row=%d)=0x%02X\n\r", col, row, lcd.getAddress(col, row));      
      lcd.putc('+');
        
    }    
    
// Show cursor as blinking character
    lcd.setCursor(TextLCD::CurOff_BlkOn);
 
// Set and show user defined characters. A maximum of 8 UDCs are supported by the HD44780.
// They are defined by a 5x7 bitpattern. 
    lcd.setUDC(0, (char *) udc_0);  // Show |>
    lcd.putc(0);    
    lcd.setUDC(1, (char *) udc_1);  // Show <|
    lcd.putc(1);    

}

Handbook page

More info is here

Committer:
wim
Date:
Fri Nov 06 18:59:27 2015 +0000
Revision:
41:111ca62e8a59
Added setFont() support (for SSD1803, US2066, ST7070) and support for UTF-8 code (Cyrillic language), added SPLC792A controller.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 41:111ca62e8a59 1 /* mbed TextLCD Library, for LCDs based on HD44780 controllers
wim 41:111ca62e8a59 2 * Copyright (c) 2015, WH
wim 41:111ca62e8a59 3 * 2015, v01: WH, AR. Added UTF8 decode tables for Cyrilic font (by Andriy Ribalko).
wim 41:111ca62e8a59 4 *
wim 41:111ca62e8a59 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 41:111ca62e8a59 6 * of this software and associated documentation files (the "Software"), to deal
wim 41:111ca62e8a59 7 * in the Software without restriction, including without limitation the rights
wim 41:111ca62e8a59 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 41:111ca62e8a59 9 * copies of the Software, and to permit persons to whom the Software is
wim 41:111ca62e8a59 10 * furnished to do so, subject to the following conditions:
wim 41:111ca62e8a59 11 *
wim 41:111ca62e8a59 12 * The above copyright notice and this permission notice shall be included in
wim 41:111ca62e8a59 13 * all copies or substantial portions of the Software.
wim 41:111ca62e8a59 14 *
wim 41:111ca62e8a59 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 41:111ca62e8a59 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 41:111ca62e8a59 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 41:111ca62e8a59 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 41:111ca62e8a59 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 41:111ca62e8a59 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 41:111ca62e8a59 21 * THE SOFTWARE.
wim 41:111ca62e8a59 22 */
wim 41:111ca62e8a59 23 #ifndef MBED_TEXTLCDUTF8_INC
wim 41:111ca62e8a59 24 #define MBED_TEXTLCDUTF8_INC
wim 41:111ca62e8a59 25
wim 41:111ca62e8a59 26 #include "TextLCD_Config.h"
wim 41:111ca62e8a59 27
wim 41:111ca62e8a59 28 #if(LCD_UTF8_FONT == 1)
wim 41:111ca62e8a59 29 // Code by Andriy Ribalko
wim 41:111ca62e8a59 30 // UTF8 conversion, please add other tables for your language
wim 41:111ca62e8a59 31 // See wikipedia.org/wiki/UTF-8 and www.utf8-chartable.de
wim 41:111ca62e8a59 32
wim 41:111ca62e8a59 33 //The two tables below are used to map UTF8 codes onto character codes for an LCD controller that
wim 41:111ca62e8a59 34 //supports a specific fonttable. The UTF codes for a specific language like Cyrilic are between 0x0400 and 0x04FF.
wim 41:111ca62e8a59 35
wim 41:111ca62e8a59 36 //Select one table for a specific controller and language
wim 41:111ca62e8a59 37
wim 41:111ca62e8a59 38 #if(0)
wim 41:111ca62e8a59 39 //Table for controller xxxx
wim 41:111ca62e8a59 40 //The two tables below are used to map Cyrilic/Russian UTF8 codes onto character codes for an LCD controller that
wim 41:111ca62e8a59 41 //supports a Cyrilic fonttable. The UTF codes for Cyrilic are between 0x0400 and 0x04FF
wim 41:111ca62e8a59 42
wim 41:111ca62e8a59 43 #define UTF_FIRST 0x0400
wim 41:111ca62e8a59 44 #define UTF_LAST 0x04FF
wim 41:111ca62e8a59 45 #define UTF_SEQ_REC_FIRST utf_seq_rec_first_cyr
wim 41:111ca62e8a59 46 #define UTF_SEQ_REC_LAST utf_seq_rec_last_cyr
wim 41:111ca62e8a59 47 #define UTF_SEQ_RECODE utf_seq_recode_cyr
wim 41:111ca62e8a59 48 #define UTF_RND_RECODE utf_rnd_recode_cyr
wim 41:111ca62e8a59 49
wim 41:111ca62e8a59 50 #define utf_seq_rec_first_cyr 0x0410 //UTF code of first symbol in sequential table UTF_recode
wim 41:111ca62e8a59 51 #define utf_seq_rec_last_cyr 0x044F //UTF code of last symbol in sequential table UTF_recode
wim 41:111ca62e8a59 52
wim 41:111ca62e8a59 53 const char utf_seq_recode_cyr[] = {
wim 41:111ca62e8a59 54 0x41,0xa0,0x42,0xa1, 0xe0,0x45,0xa3,0xa4, 0xa5,0xa6,0x4b,0xa7, 0x4d,0x48,0x4f,0xa8, //Upper case Cyrillic
wim 41:111ca62e8a59 55 0x50,0x43,0x54,0xa9, 0xaa,0x58,0xe1,0xab, 0xac,0xe2,0xad,0xae, 0x62,0xaf,0xb0,0xb1,
wim 41:111ca62e8a59 56 0x61,0xb2,0xb3,0xb4, 0xe3,0x65,0xb6,0xb7, 0xb8,0xb9,0xba,0xbb, 0xbc,0xbd,0x6f,0xbe, //Lower case Cyrillic
wim 41:111ca62e8a59 57 0x70,0x63,0xbf,0x79, 0xe4,0x78,0xe5,0xc0, 0xc1,0xe6,0xc2,0xc3, 0xc4,0xc5,0xc6,0xc7
wim 41:111ca62e8a59 58 };
wim 41:111ca62e8a59 59
wim 41:111ca62e8a59 60 //Two dimensional table for some non-sequential symbol decoding (RUS/UKR)
wim 41:111ca62e8a59 61 //U+0401 --> 0xa2 (Ё), U+0451 --> 0xb5 (ё), U+0406 --> 0x49 (І), U+0456 --> 0x69 (і)
wim 41:111ca62e8a59 62 const short int utf_rnd_recode_cyr [5][2]= {
wim 41:111ca62e8a59 63 {0x0401, 0xa2},
wim 41:111ca62e8a59 64 {0x0451, 0xb5},
wim 41:111ca62e8a59 65 {0x0406, 0x49},
wim 41:111ca62e8a59 66 {0x0456, 0x69},
wim 41:111ca62e8a59 67 {0} //Last element table zero
wim 41:111ca62e8a59 68 };
wim 41:111ca62e8a59 69 #endif
wim 41:111ca62e8a59 70
wim 41:111ca62e8a59 71 #if(LCD_UTF8_CYR_B == 1)
wim 41:111ca62e8a59 72 //ROM_B Table for controller SSD1803 and US2066
wim 41:111ca62e8a59 73 //The two tables below are used to map Cyrilic/Russian UTF8 codes onto character codes for an LCD controller that
wim 41:111ca62e8a59 74 //supports a Cyrilic fonttable. The UTF codes for Cyrilic are between 0x0400 and 0x04FF
wim 41:111ca62e8a59 75
wim 41:111ca62e8a59 76 #define UTF_FIRST 0x0400
wim 41:111ca62e8a59 77 #define UTF_LAST 0x04FF
wim 41:111ca62e8a59 78 #define UTF_SEQ_REC_FIRST utf_seq_rec_first_cyr
wim 41:111ca62e8a59 79 #define UTF_SEQ_REC_LAST utf_seq_rec_last_cyr
wim 41:111ca62e8a59 80 #define UTF_SEQ_RECODE utf_seq_recode_cyr
wim 41:111ca62e8a59 81 #define UTF_RND_RECODE utf_rnd_recode_cyr
wim 41:111ca62e8a59 82
wim 41:111ca62e8a59 83 #define utf_seq_rec_first_cyr 0x0410 //UTF code of first symbol in sequential table UTF_recode
wim 41:111ca62e8a59 84 #define utf_seq_rec_last_cyr 0x044F //UTF code of last symbol in sequential table UTF_recode
wim 41:111ca62e8a59 85 const char utf_seq_recode_cyr[] = {
wim 41:111ca62e8a59 86 0x80,0x81,0x82,0x83, 0x84,0x85,0x86,0x87, 0x88,0x89,0x8A,0x8B, 0x8C,0x8D,0x8E,0x8F, //Upper case Cyrillic
wim 41:111ca62e8a59 87 0x90,0x91,0x92,0x93, 0x94,0x95,0x96,0x97, 0x98,0x99,0x9A,0x9B, 0x9C,0x9D,0x9E,0x9F,
wim 41:111ca62e8a59 88 0x61,0x81,0x62,0x83, 0x84,0x65,0x86,0x87, 0x88,0x89,0x6B,0x8B, 0x6D,0x69,0x6F,0x8F, //Lower case Cyrillic (~Upper)
wim 41:111ca62e8a59 89 0x70,0x63,0x92,0x79, 0x94,0x95,0x96,0x97, 0x98,0x99,0x9A,0x9B, 0x9C,0x9D,0x9E,0x9F
wim 41:111ca62e8a59 90 };
wim 41:111ca62e8a59 91
wim 41:111ca62e8a59 92 //Two dimensional table for some non-sequential symbol decoding (RUS/UKR)
wim 41:111ca62e8a59 93 //U+0400 --> 0xC8 (E)
wim 41:111ca62e8a59 94 //U+0401 --> 0xCB (Ё)
wim 41:111ca62e8a59 95 //U+0405 --> 0x53 (S)
wim 41:111ca62e8a59 96 //U+0406 --> 0x49 (І)
wim 41:111ca62e8a59 97 //U+0407 --> 0xCF (І)
wim 41:111ca62e8a59 98 //U+0408 --> 0x4A (J)
wim 41:111ca62e8a59 99 //U+0450 --> 0xE8 ( )
wim 41:111ca62e8a59 100 //U+0451 --> 0xEB (ё)
wim 41:111ca62e8a59 101 //U+0456 --> 0x69 (і)
wim 41:111ca62e8a59 102 //U+0457 --> 0xCF (і)
wim 41:111ca62e8a59 103 //U+0458 --> 0x6A (j)
wim 41:111ca62e8a59 104 const short int utf_rnd_recode_cyr [][2]= {
wim 41:111ca62e8a59 105 {0x0400, 0xC8},
wim 41:111ca62e8a59 106 {0x0401, 0xCB},
wim 41:111ca62e8a59 107 {0x0405, 0x53},
wim 41:111ca62e8a59 108 {0x0406, 0x49},
wim 41:111ca62e8a59 109 {0x0407, 0xCF},
wim 41:111ca62e8a59 110 {0x0408, 0x4A},
wim 41:111ca62e8a59 111 {0x0450, 0xE8},
wim 41:111ca62e8a59 112 {0x0451, 0xEB},
wim 41:111ca62e8a59 113 {0x0456, 0x69},
wim 41:111ca62e8a59 114 {0x0457, 0xCF},
wim 41:111ca62e8a59 115 {0x0458, 0x6A},
wim 41:111ca62e8a59 116 {0 , 0} //Last element table zero
wim 41:111ca62e8a59 117 };
wim 41:111ca62e8a59 118 #endif
wim 41:111ca62e8a59 119
wim 41:111ca62e8a59 120
wim 41:111ca62e8a59 121 //end UTF conversion
wim 41:111ca62e8a59 122 #endif
wim 41:111ca62e8a59 123
wim 41:111ca62e8a59 124 #endif