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:
22:35742ec80c24
Parent:
21:9eb628d9e164
Child:
23:d47f226efb24
--- a/TextLCD.h	Tue Apr 01 21:30:25 2014 +0000
+++ b/TextLCD.h	Wed Apr 02 17:49:55 2014 +0000
@@ -37,7 +37,7 @@
 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
  *
  * Currently supports 8x1, 8x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels
- * Interface options include direct mbed pins, I2C portexpander (PCF8474) or SPI bus shiftregister (74595)
+ * Interface options include direct mbed pins, I2C portexpander (PCF8474, PCF8574A) or SPI bus shiftregister (74595)
  * Supports some controllers that provide internal DC/DC converters for VLCD or VLED. 
  *
  * @code
@@ -50,9 +50,9 @@
  * // 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, CS pin, LCD Type  
- * //TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type
+ * //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, 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
  * 
  * int main() {
@@ -161,7 +161,8 @@
 
 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
  *
- * Currently supports 8x1, 8x2, 12x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels
+ * @brief Currently supports 8x1, 8x2, 12x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels
+ *        Interface options include direct mbed pins, I2C portexpander (PCF8474, PCF8574A) or SPI bus shiftregister (74595) 
  *
  */
 class TextLCD_Base : public Stream {
@@ -194,8 +195,6 @@
     };
 
 
-
-
     /** LCD Cursor control */
     enum LCDCursor {
         CurOff_BlkOff = 0x00,  /**<  Cursor Off, Blinking Char Off */    
@@ -258,7 +257,8 @@
     void setAddress(int column, int row);        
 
 
-    /** Clear the screen and locate to 0,0 */
+    /** Clear the screen and locate to 0,0
+     */
     void cls();
 
     /** Return the number of rows
@@ -365,6 +365,11 @@
     LCDCursor _currentCursor;    
 };
 
+///--------- End TextLCD_Base -----------
+
+
+
+///--------- Start TextLCD Bus -----------
 
 /** Create a TextLCD interface for using regular mbed pins
   *
@@ -383,6 +388,14 @@
      */
     TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2, PinName bl = NC, PinName e2 = NC, LCDCtrl ctrl = HD44780);
 
+
+   /** Destruct a TextLCD interface for using regular mbed pins
+     *
+     * @param  none
+     * @return none
+     */ 
+    virtual ~TextLCD();
+
 private:    
 //Low level writes to LCD Bus (serial or parallel)
     virtual void _setEnable(bool value);
@@ -390,26 +403,37 @@
     virtual void _setBL(bool value);
     virtual void _setData(int value);
 
-// Regular mbed pins bus
-    DigitalOut _rs, _e, _bl, _e2;
-    BusOut _d;   
+/** Regular mbed pins bus
+  */
+    DigitalOut _rs, _e;
+    BusOut _d;
+    
+/** Optional Hardware pins for the Backlight and LCD40x4 device
+  * Default PinName value is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
+  */
+    DigitalOut *_bl, *_e2;       
 };
 
+    
+///----------- End TextLCD ---------------
 
 
-/** Create a TextLCD interface using an I2C PC8574 portexpander
+///--------- Start TextLCD_I2C -----------
+
+
+/** Create a TextLCD interface using an I2C PC8574 or PCF8574A portexpander
   *
   */
 class TextLCD_I2C : public TextLCD_Base {    
 public:
-    /** Create a TextLCD interface using an I2C PC8574 portexpander
+    /** Create a TextLCD interface using an I2C PC8574 or PCF8574A portexpander
      *
      * @param i2c             I2C Bus
-     * @param deviceAddress   I2C slave address (PCF8574)
+     * @param deviceAddress   I2C slave address (PCF8574 or PCF8574A, default = 0x40)
      * @param type            Sets the panel size/addressing mode (default = LCD16x2)
      * @param ctrl            LCD controller (default = HD44780)                
      */
-    TextLCD_I2C(I2C *i2c, char deviceAddress, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780);
+    TextLCD_I2C(I2C *i2c, char deviceAddress = 0x40, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780);
 
 private:
 //Low level writes to LCD Bus (serial or parallel)
@@ -428,6 +452,12 @@
 };
 
 
+///---------- End TextLCD_I2C ------------
+
+
+
+///--------- Start TextLCD_SPI -----------
+
 
 /** Create a TextLCD interface using an SPI 74595 portexpander
   *
@@ -464,5 +494,6 @@
 
 };
 
+///---------- End TextLCD_SPI ------------
 
 #endif