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

Revision:
38:cbe275b0b647
Parent:
37:ce348c002929
Child:
39:e9c2319de9c5
--- a/TextLCD_Config.h	Sun Mar 29 13:08:03 2015 +0000
+++ b/TextLCD_Config.h	Sat Apr 18 11:33:02 2015 +0000
@@ -5,6 +5,8 @@
  *               2014, v03: WH, Added LCD_SPI_N_3_8 define for ST7070
  *               2015, v04: WH, Added support for alternative fonttables (eg PCF21XX)
  *               2015, v05: WH, Clean up low-level _writeCommand() and _writeData(), Added support for alt fonttables (eg PCF21XX), Added ST7066_ACM for ACM1602 module, fixed contrast for ST7032 
+ *               2015, v06: WH, Performance improvement I2C portexpander
+ *               2015, v07: WH, Fixed Adafruit I2C/SPI portexpander pinmappings, fixed SYDZ Backlight
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -65,16 +67,30 @@
 //Note: LCD RW pin must be connected to GND
 //      E2 is used for LCD40x4 (second controller)
 //      BL may be used to control backlight
-#define D_LCD_PIN_D4   0
-#define D_LCD_PIN_D5   1
-#define D_LCD_PIN_D6   2
-#define D_LCD_PIN_D7   3
-#define D_LCD_PIN_RS   4
-#define D_LCD_PIN_E    5
-#define D_LCD_PIN_E2   6
-#define D_LCD_PIN_BL   7
+
+//I2C bus expander (PCF8574/PCF8574A or MCP23008) interface
+#define LCD_BUS_I2C_D4 (1 << 0)
+#define LCD_BUS_I2C_D5 (1 << 1)
+#define LCD_BUS_I2C_D6 (1 << 2)
+#define LCD_BUS_I2C_D7 (1 << 3)
+#define LCD_BUS_I2C_RS (1 << 4)
+#define LCD_BUS_I2C_E  (1 << 5)
+#define LCD_BUS_I2C_E2 (1 << 6)
+#define LCD_BUS_I2C_BL (1 << 7)
 
-#define D_LCD_PIN_RW   D_LCD_PIN_E2
+#define LCD_BUS_I2C_RW (1 << 6)
+
+//SPI bus expander (74595) interface, same as I2C 
+#define LCD_BUS_SPI_D4 LCD_BUS_I2C_D4
+#define LCD_BUS_SPI_D5 LCD_BUS_I2C_D5
+#define LCD_BUS_SPI_D6 LCD_BUS_I2C_D6
+#define LCD_BUS_SPI_D7 LCD_BUS_I2C_D7
+#define LCD_BUS_SPI_RS LCD_BUS_I2C_RS
+#define LCD_BUS_SPI_E  LCD_BUS_I2C_E
+#define LCD_BUS_SPI_E2 LCD_BUS_I2C_E2
+#define LCD_BUS_SPI_BL LCD_BUS_I2C_BL
+
+#define LCD_BUS_SPI_RW LCD_BUS_I2C_RW
 
 //Select I2C Portexpander type (one option only)
 #define PCF8574        1
@@ -93,16 +109,33 @@
 //Note: LCD RW pin must be kept LOW
 //      E2 is not available on this hardware and so it does not support LCD40x4 (second controller)
 //      BL is used to control backlight
-#define D_LCD_PIN_0    0
-#define D_LCD_PIN_RS   1
-#define D_LCD_PIN_E    2
-#define D_LCD_PIN_D4   3
-#define D_LCD_PIN_D5   4
-#define D_LCD_PIN_D6   5
-#define D_LCD_PIN_D7   6
-#define D_LCD_PIN_BL   7
+//Note: The pinmappings are different for the MCP23008 and the 74595!
+
+//I2C bus expander (MCP23008) interface
+#define LCD_BUS_I2C_0  (1 << 0)
+#define LCD_BUS_I2C_RS (1 << 1)
+#define LCD_BUS_I2C_E  (1 << 2)
+#define LCD_BUS_I2C_D4 (1 << 3)
+#define LCD_BUS_I2C_D5 (1 << 4)
+#define LCD_BUS_I2C_D6 (1 << 5)
+#define LCD_BUS_I2C_D7 (1 << 6)
+#define LCD_BUS_I2C_BL (1 << 7)
 
-#define D_LCD_PIN_E2   D_LCD_PIN_0
+#define LCD_BUS_I2C_E2 (1 << 0)
+#define LCD_BUS_I2C_RW (1 << 0)
+
+//SPI bus expander (74595) interface
+#define LCD_BUS_SPI_0  (1 << 0)
+#define LCD_BUS_SPI_RS (1 << 1)
+#define LCD_BUS_SPI_E  (1 << 2)
+#define LCD_BUS_SPI_D7 (1 << 3)
+#define LCD_BUS_SPI_D6 (1 << 4)
+#define LCD_BUS_SPI_D5 (1 << 5)
+#define LCD_BUS_SPI_D4 (1 << 6)
+#define LCD_BUS_SPI_BL (1 << 7)
+
+#define LCD_BUS_SPI_E2 (1 << 0)
+#define LCD_BUS_SPI_RW (1 << 0)
 
 //Force I2C portexpander type
 #define PCF8574        0
@@ -127,16 +160,31 @@
 //Note: LCD RW pin must be kept LOW
 //      E2 is not available on default Arduino hardware and so it does not support LCD40x4 (second controller)
 //      BL is used to control backlight
-#define D_LCD_PIN_RS   0
-#define D_LCD_PIN_RW   1
-#define D_LCD_PIN_E    2
-#define D_LCD_PIN_BL   3
-#define D_LCD_PIN_D4   4
-#define D_LCD_PIN_D5   5
-#define D_LCD_PIN_D6   6
-#define D_LCD_PIN_D7   7
+
+//I2C bus expander PCF8574 interface
+#define LCD_BUS_I2C_RS (1 << 0)
+#define LCD_BUS_I2C_RW (1 << 1)
+#define LCD_BUS_I2C_E  (1 << 2)
+#define LCD_BUS_I2C_BL (1 << 3)
+#define LCD_BUS_I2C_D4 (1 << 4)
+#define LCD_BUS_I2C_D5 (1 << 5)
+#define LCD_BUS_I2C_D6 (1 << 6)
+#define LCD_BUS_I2C_D7 (1 << 7)
+
+#define LCD_BUS_I2C_E2 (1 << 1)
 
-#define D_LCD_PIN_E2   D_LCD_PIN_RW
+//SPI bus expander (74595) interface, same as I2C
+#define LCD_BUS_SPI_RS LCD_BUS_I2C_RS
+#define LCD_BUS_SPI_RW LCD_BUS_I2C_RW
+#define LCD_BUS_SPI_E  LCD_BUS_I2C_E
+#define LCD_BUS_SPI_BL LCD_BUS_I2C_BL
+#define LCD_BUS_SPI_D4 LCD_BUS_I2C_D4
+#define LCD_BUS_SPI_D5 LCD_BUS_I2C_D5
+#define LCD_BUS_SPI_D6 LCD_BUS_I2C_D6
+#define LCD_BUS_SPI_D7 LCD_BUS_I2C_D7
+
+#define LCD_BUS_SPI_E2 LCD_BUS_I2C_E2
+
 
 //Force I2C portexpander type
 #define PCF8574        1
@@ -155,16 +203,30 @@
 //Note: LCD RW pin must be kept LOW
 //      E2 is not available on default hardware and so it does not support LCD40x4 (second controller)
 //      BL is used to control backlight, reverse logic: Low turns on Backlight. This is handled in setBacklight()
-#define D_LCD_PIN_RS   0
-#define D_LCD_PIN_RW   1
-#define D_LCD_PIN_E    2
-#define D_LCD_PIN_BL   3
-#define D_LCD_PIN_D4   4
-#define D_LCD_PIN_D5   5
-#define D_LCD_PIN_D6   6
-#define D_LCD_PIN_D7   7
+
+//I2C bus expander PCF8574 interface
+#define LCD_BUS_I2C_RS (1 << 0)
+#define LCD_BUS_I2C_RW (1 << 1)
+#define LCD_BUS_I2C_E  (1 << 2)
+#define LCD_BUS_I2C_BL (1 << 3)
+#define LCD_BUS_I2C_D4 (1 << 4)
+#define LCD_BUS_I2C_D5 (1 << 5)
+#define LCD_BUS_I2C_D6 (1 << 6)
+#define LCD_BUS_I2C_D7 (1 << 7)
 
-#define D_LCD_PIN_E2   D_LCD_PIN_RW
+#define LCD_BUS_I2C_E2 (1 << 1)
+
+//SPI bus expander (74595) interface, same as I2C
+#define LCD_BUS_SPI_RS LCD_BUS_I2C_RS
+#define LCD_BUS_SPI_RW LCD_BUS_I2C_RW
+#define LCD_BUS_SPI_E  LCD_BUS_I2C_E
+#define LCD_BUS_SPI_BL LCD_BUS_I2C_BL
+#define LCD_BUS_SPI_D4 LCD_BUS_I2C_D4
+#define LCD_BUS_SPI_D5 LCD_BUS_I2C_D5
+#define LCD_BUS_SPI_D6 LCD_BUS_I2C_D6
+#define LCD_BUS_SPI_D7 LCD_BUS_I2C_D7
+
+#define LCD_BUS_SPI_E2 LCD_BUS_I2C_E2
 
 //Force I2C portexpander type
 #define PCF8574        1
@@ -183,16 +245,30 @@
 //Note: LCD RW pin must be kept LOW
 //      E2 is not available on default hardware and so it does not support LCD40x4 (second controller)
 //      BL is used to control backlight, reverse logic: Low turns on Backlight. This is handled in setBacklight()
-#define D_LCD_PIN_D4   0
-#define D_LCD_PIN_D5   1
-#define D_LCD_PIN_D6   2
-#define D_LCD_PIN_D7   3
-#define D_LCD_PIN_EN   4
-#define D_LCD_PIN_RW   5
-#define D_LCD_PIN_RS   6
-#define D_LCD_PIN_BL   7
+
+//I2C bus expander PCF8574 interface
+#define LCD_BUS_I2C_D4 (1 << 0)
+#define LCD_BUS_I2C_D5 (1 << 1)
+#define LCD_BUS_I2C_D6 (1 << 2)
+#define LCD_BUS_I2C_D7 (1 << 3)
+#define LCD_BUS_I2C_E  (1 << 4)
+#define LCD_BUS_I2C_RW (1 << 5)
+#define LCD_BUS_I2C_RS (1 << 6)
+#define LCD_BUS_I2C_BL (1 << 7)
 
-#define D_LCD_PIN_E2   D_LCD_PIN_RW
+#define LCD_BUS_I2C_E2 (1 << 5)
+
+//SPI bus expander (74595) interface
+#define LCD_BUS_SPI_D4 LCD_BUS_I2C_D4
+#define LCD_BUS_SPI_D5 LCD_BUS_I2C_D5
+#define LCD_BUS_SPI_D6 LCD_BUS_I2C_D6
+#define LCD_BUS_SPI_D7 LCD_BUS_I2C_D7
+#define LCD_BUS_SPI_E  LCD_BUS_I2C_E
+#define LCD_BUS_SPI_RW LCD_BUS_I2C_RW
+#define LCD_BUS_SPI_RS LCD_BUS_I2C_RS
+#define LCD_BUS_SPI_BL LCD_BUS_I2C_BL
+
+#define LCD_BUS_SPI_E2 LCD_BUS_I2C_E2
 
 //Force I2C portexpander type
 #define PCF8574        1
@@ -204,47 +280,62 @@
 
 #if (SYDZ==1)
 //Definitions for SYDZ Module mapping between serial port expander pins and LCD controller. 
-//Very similar to DFROBOT. This hardware uses PCF8574A and uses inverted Backlight control
-//Slaveaddress may be set by switches (default 0x40). SDA/SCL has pullup Resistors onboard.
+//Very similar to DFROBOT. This hardware uses PCF8574A.
+//Slaveaddress may be set by switches (default 0x70). SDA/SCL has pullup Resistors onboard.
 //See ebay
 //
 //Note: LCD RW pin must be kept LOW
 //      E2 is not available on default hardware and so it does not support LCD40x4 (second controller)
-//      BL is used to control backlight, reverse logic: Low turns on Backlight. This is handled in setBacklight()
-#define D_LCD_PIN_RS   0
-#define D_LCD_PIN_RW   1
-#define D_LCD_PIN_E    2
-#define D_LCD_PIN_BL   3
-#define D_LCD_PIN_D4   4
-#define D_LCD_PIN_D5   5
-#define D_LCD_PIN_D6   6
-#define D_LCD_PIN_D7   7
+//      BL is used to control backlight
+
+//I2C bus expander PCF8574A interface
+#define LCD_BUS_I2C_RS (1 << 0)
+#define LCD_BUS_I2C_RW (1 << 1)
+#define LCD_BUS_I2C_E  (1 << 2)
+#define LCD_BUS_I2C_BL (1 << 3)
+#define LCD_BUS_I2C_D4 (1 << 4)
+#define LCD_BUS_I2C_D5 (1 << 5)
+#define LCD_BUS_I2C_D6 (1 << 6)
+#define LCD_BUS_I2C_D7 (1 << 7)
 
-#define D_LCD_PIN_E2   D_LCD_PIN_RW
+#define LCD_BUS_I2C_E2 (1 << 1)
+
+//SPI bus expander (74595) interface, same as I2C
+#define LCD_BUS_SPI_RS LCD_BUS_I2C_RS
+#define LCD_BUS_SPI_RW LCD_BUS_I2C_RW
+#define LCD_BUS_SPI_E  LCD_BUS_I2C_E
+#define LCD_BUS_SPI_BL LCD_BUS_I2C_BL
+#define LCD_BUS_SPI_D4 LCD_BUS_I2C_D4
+#define LCD_BUS_SPI_D5 LCD_BUS_I2C_D5
+#define LCD_BUS_SPI_D6 LCD_BUS_I2C_D6
+#define LCD_BUS_SPI_D7 LCD_BUS_I2C_D7
+
+#define LCD_BUS_SPI_E2 LCD_BUS_I2C_E2
 
 //Force I2C portexpander type
 #define PCF8574        1
 #define MCP23008       0
 
 //Force Inverted Backlight control
-#define BACKLIGHT_INV  1
+#define BACKLIGHT_INV  0
 #endif
 
 //Bitpattern Defines for I2C PCF8574/PCF8574A, MCP23008 and SPI 74595 Bus expanders
 //Don't change!
-//
-#define D_LCD_D4       (1<<D_LCD_PIN_D4)
-#define D_LCD_D5       (1<<D_LCD_PIN_D5)
-#define D_LCD_D6       (1<<D_LCD_PIN_D6)
-#define D_LCD_D7       (1<<D_LCD_PIN_D7)
-#define D_LCD_RS       (1<<D_LCD_PIN_RS)
-#define D_LCD_E        (1<<D_LCD_PIN_E)
-#define D_LCD_E2       (1<<D_LCD_PIN_E2)
-#define D_LCD_BL       (1<<D_LCD_PIN_BL)
-//#define D_LCD_RW       (1<<D_LCD_PIN_RW)
+#define LCD_BUS_I2C_MSK (LCD_BUS_I2C_D4 | LCD_BUS_I2C_D5 | LCD_BUS_I2C_D6 | LCD_BUS_I2C_D7)
+#if (BACKLIGHT_INV == 1)
+#define LCD_BUS_I2C_DEF (0x00 | LCD_BUS_I2C_BL)
+#else
+#define LCD_BUS_I2C_DEF  0x00
+#endif
 
-#define D_LCD_BUS_MSK  (D_LCD_D4 | D_LCD_D5 | D_LCD_D6 | D_LCD_D7)
-#define D_LCD_BUS_DEF  0x00
+#define LCD_BUS_SPI_MSK (LCD_BUS_SPI_D4 | LCD_BUS_SPI_D5 | LCD_BUS_SPI_D6 | LCD_BUS_SPI_D7)
+#if (BACKLIGHT_INV == 1)
+#define LCD_BUS_SPI_DEF (0x00 | LCD_BUS_SPI_BL)
+#else
+#define LCD_BUS_SPI_DEF  0x00
+#endif
+
 
 /* PCF8574/PCF8574A I2C portexpander slave address */
 #define PCF8574_SA0    0x40