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:
29:a3663151aa65
Parent:
28:30fa94f7341c
Child:
30:033048611c01
--- a/TextLCD.h	Sat Jun 14 17:04:25 2014 +0000
+++ b/TextLCD.h	Tue Jun 17 17:41:47 2014 +0000
@@ -10,6 +10,7 @@
  *               2014, v08: WH, Refactored in Base and Derived Classes to deal with mbed lib change regarding 'NC' defined DigitalOut pins
  *               2014, v09: WH/EO, Added Class for Native SPI controllers such as ST7032 
  *               2014, v10: WH, Added Class for Native I2C controllers such as ST7032i, Added support for MCP23008 I2C portexpander, Added support for Adafruit module  
+ *               2014, v11: WH, Added support for native I2C controllers such as PCF21XX, improved the _initCtrl() method to deal with differences between all supported controllers  
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -69,14 +70,15 @@
 //LCD and serial portexpanders should be wired accordingly 
 //
 //Select Hardware module (one option only)
-#define DEFAULT        1
-#define ADAFRUIT       0
+#define DEFAULT        0
+#define ADAFRUIT       1
 #define DFROBOT        0
 
 
 #if (DEFAULT==1)
-//Definitions for hardware used by WH 
+//Definitions for default (WH) mapping between serial port expander pins and LCD controller
 //This hardware supports the I2C bus expander (PCF8574/PCF8574A or MCP23008) and SPI bus expander (74595) interfaces
+//See https://mbed.org/cookbook/Text-LCD-Enhanced
 //
 //Note: LCD RW pin must be connected to GND
 //      E2 is used for LCD40x4 (second controller)
@@ -98,8 +100,9 @@
 #endif
 
 #if (ADAFRUIT==1)
-//Definitions for i2cspilcdbackpack from Adafruit, See http://www.ladyada.net/products/i2cspilcdbackpack
+//Definitions for Adafruit i2cspilcdbackpack mapping between serial port expander pins and LCD controller
 //This hardware supports both an I2C expander (MCP23008) and an SPI expander (74595) selectable by a jumper.
+//See http://www.ladyada.net/products/i2cspilcdbackpack
 //
 //Note: LCD RW pin must be kept LOW
 //      E2 is not available on this hardware and so it does not support LCD40x4 (second controller)
@@ -121,8 +124,9 @@
 #endif
 
 #if (DFROBOT==1)
-//Definitions for LCD2004 Module from DFROBOT, See http://arduino-info.wikispaces.com/LCD-Blue-I2C
+//Definitions for DFROBOT LCD2004 Module mapping between serial port expander pins and LCD controller
 //This hardware uses PCF8574 and is different from earlier/different Arduino I2C LCD displays
+//See http://arduino-info.wikispaces.com/LCD-Blue-I2C
 //
 //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)
@@ -205,6 +209,9 @@
 /* ST7032I I2C slave address */
 #define ST7032_SA     0x7C
 
+/* PCF21XX I2C slave address */
+#define PCF21XX_SA0   0x74
+#define PCF21XX_SA1   0x76
 
 /** Some sample User Defined Chars 5x7 dots */
 const char udc_ae[] = {0x00, 0x00, 0x1B, 0x05, 0x1F, 0x14, 0x1F, 0x00};  //æ
@@ -252,10 +259,10 @@
 //const char udc_ch_2[]  =  {0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f};  // Hor bars 4 (inverted)
 //const char udc_ch_3[]  =  {0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15};  // Ver bars 3
 //const char udc_ch_4[]  =  {0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a};  // Ver bars 3 (inverted)
-//const char udc_ch_yr[] =  {0x08, 0x0f, 0x12, 0x0f, 0x0a, 0x1f, 0x02, 0x02};  // Year   (kanji)
-//const char udc_ch_mo[] =  {0x0f, 0x09, 0x0f, 0x09, 0x0f, 0x09, 0x09, 0x13};  // Month  (kanji)
-//const char udc_ch_dy[] =  {0x1f, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11, 0x1F};  // Day    (kanji)
-//const char udc_ch_mi[] =  {0x0C, 0x0a, 0x11, 0x1f, 0x09, 0x09, 0x09, 0x13};  // minute (kanji)
+//const char udc_ch_yr[] =  {0x08, 0x0f, 0x12, 0x0f, 0x0a, 0x1f, 0x02, 0x02};  // Year   (kana)
+//const char udc_ch_mo[] =  {0x0f, 0x09, 0x0f, 0x09, 0x0f, 0x09, 0x09, 0x13};  // Month  (kana)
+//const char udc_ch_dy[] =  {0x1f, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11, 0x1F};  // Day    (kana)
+//const char udc_ch_mi[] =  {0x0C, 0x0a, 0x11, 0x1f, 0x09, 0x09, 0x09, 0x13};  // minute (kana)
 
 
 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
@@ -272,32 +279,37 @@
         LCD8x1,     /**<  8x1 LCD panel */    
         LCD8x2,     /**<  8x2 LCD panel */          
         LCD8x2B,    /**<  8x2 LCD panel (actually 16x1) */                  
+//        LCD12x1,    /**< 12x1 LCD panel */                          
+//        LCD12x1B,   /**< 12x1 LCD panel, special mode PCF21XX */                                  
         LCD12x2,    /**< 12x2 LCD panel */                          
-//        LCD12x3,    /**< 12x3 LCD panel, special mode PCF21XX */                                  
+        LCD12x3B,   /**< 12x3 LCD panel, special mode PCF21XX */                                  
         LCD12x4,    /**< 12x4 LCD panel */                  
-//        LCD12x4B,   /**< 12x4 LCD panel, special mode PCF21XX */                                          
+        LCD12x4B,   /**< 12x4 LCD panel, special mode PCF21XX */                                          
         LCD16x1,    /**< 16x1 LCD panel (actually 8x2) */          
         LCD16x2,    /**< 16x2 LCD panel (default) */
         LCD16x2B,   /**< 16x2 LCD panel alternate addressing */
-//        LCD16x3,    /**< 16x3 LCD panel, special mode ST7036 */                
+//        LCD16x3B,   /**< 16x3 LCD panel, special mode ST7036 */                
         LCD16x4,    /**< 16x4 LCD panel */        
+//        LCD20x1,    /**< 20x1 LCD panel */
         LCD20x2,    /**< 20x2 LCD panel */
         LCD20x4,    /**< 20x4 LCD panel */
+        LCD24x4B,   /**< 24x4 LCD panel, special mode KS0078 */                                                  
+        LCD24x1,    /**< 24x1 LCD panel */        
         LCD24x2,    /**< 24x2 LCD panel */        
-        LCD24x4,    /**< 24x4 LCD panel, special mode KS0078 */                
         LCD40x2,    /**< 40x2 LCD panel */                
         LCD40x4     /**< 40x4 LCD panel, Two controller version */                        
     };
 
     /** LCD Controller Device */
     enum LCDCtrl {
-        HD44780,    /**<  HD44780 (default)        */    
-        WS0010,     /**<  WS0010  OLED Controller  */    
-        ST7036,     /**<  ST7036  3V3 with Booster */   
-        ST7032_3V3, /**<  ST7032  3V3 with Booster */   
-        ST7032_5V   /**<  ST7032  5V no Booster    */           
-//        PCF210X,    /**<  PCF210X 5V no Booster    */                   
-//        PCF211X     /**<  PCF211X 3V3 with Booster */                           
+        HD44780,     /**<  HD44780 (default)                           */    
+        WS0010,      /**<  WS0010  OLED Controller, 4/8 bit            */    
+        ST7036,      /**<  ST7036  3V3 with Booster, 4/8 bit, SPI      */   
+        ST7032_3V3,  /**<  ST7032  3V3 with Booster, 4/8 bit, SPI, I2C */   
+        ST7032_5V,   /**<  ST7032  5V no Booster, 4/8 bit, SPI, I2C    */           
+        KS0078,      /**<  KS0078  24x3 support                        */                   
+        PCF21XX_3V3, /**<  PCF21XX 3V3 with Booster, 4/8 bit, I2C      */                           
+//        PCF21XX_5V   /**<  PCF21XX 5V no Booster, 4/8 bit, I2C         */        
     };
 
 
@@ -338,7 +350,7 @@
     int printf(const char* format, ...);
 #endif
 
-    /** Locate to a screen column and row
+    /** Locate cursor to a screen column and row
      *
      * @param column  The horizontal position from the left, indexed from 0
      * @param row     The vertical position from the top, indexed from 0
@@ -406,7 +418,9 @@
      */
     void setUDC(unsigned char c, char *udc_data);
 
-
+//test
+    void _initCtrl();    
+    
 protected:
 
    /** LCD controller select, mainly used for LCD40x4
@@ -429,29 +443,70 @@
     virtual int _putc(int value);
     virtual int _getc();
 
-/** Low level methods for LCD controller
+/** Low level method for LCD controller
   */
     void _init();    
-    void _initCtrl();    
+
+/** Low level initialisation method for LCD controller
+  */
+//    void _initCtrl();    
+
+/** Low level character address set method
+  */  
     int  _address(int column, int row);
+    
+/** Low level cursor enable or disable method
+  */  
     void _setCursor(LCDCursor show);
+
+/** Low level method to store user defined characters for current controller
+  */     
     void _setUDC(unsigned char c, char *udc_data);   
+
+/** Low level method to restore the cursortype and display mode for current controller
+  */     
     void _setCursorAndDisplayMode(LCDMode displayMode, LCDCursor cursorType);       
     
-/** Low level write operations to LCD controller
+
+/** Low level nibble write operation to LCD controller (serial or parallel)
   */
     void _writeNibble(int value);
-    virtual void _writeByte(int value);
+   
+/** Low level command byte write operation to LCD controller.
+  * Methods resets the RS bit and provides the required timing for the command.
+  */
     void _writeCommand(int command);
+
+/** Low level data byte write operation to LCD controller (serial or parallel).
+  * Methods sets the RS bit and provides the required timing for the data.
+  */   
     void _writeData(int data);
 
+
+/** Pure Virtual Low level writes to LCD Bus (serial or parallel)
+  * Set the Enable pin.
+  */
+    virtual void _setEnable(bool value) = 0;
+
 /** Pure Virtual Low level writes to LCD Bus (serial or parallel)
-  */
-    virtual void _setEnable(bool value) = 0;
+  * Set the RS pin ( 0 = Command, 1 = Data).
+  */   
     virtual void _setRS(bool value) = 0;  
+
+/** Pure Virtual Low level writes to LCD Bus (serial or parallel)
+  * Set the BL pin (0 = Backlight Off, 1 = Backlight On).
+  */   
     virtual void _setBL(bool value) = 0;
+    
+/** Pure Virtual Low level writes to LCD Bus (serial or parallel)
+  * Set the databus value (4 bit).
+  */   
     virtual void _setData(int value) = 0;
 
+/** Low level byte write operation to LCD controller (serial or parallel)
+  * Depending on the RS pin this byte will be interpreted as data or command
+  */
+    virtual void _writeByte(int value);
     
 //Display type
     LCDType _type;
@@ -503,12 +558,28 @@
     virtual ~TextLCD();
 
 private:    
-//Low level writes to LCD Bus (serial or parallel)
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (parallel)
+  * Set the Enable pin.
+  */
     virtual void _setEnable(bool value);
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (parallel)
+  * Set the RS pin ( 0 = Command, 1 = Data).
+  */   
     virtual void _setRS(bool value);  
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (parallel)
+  * Set the BL pin (0 = Backlight Off, 1 = Backlight On).
+  */   
     virtual void _setBL(bool value);
+    
+/** Implementation of pure Virtual Low level writes to LCD Bus (parallel)
+  * Set the databus value (4 bit).
+  */   
     virtual void _setData(int value);
 
+
 /** Regular mbed pins bus
   */
     DigitalOut _rs, _e;
@@ -542,13 +613,34 @@
     TextLCD_I2C(I2C *i2c, char deviceAddress = PCF8574_SA0, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780);
 
 private:
-//Low level writes to LCD Bus (serial or parallel)
+    
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial expander)
+  * Set the Enable pin.
+  */
     virtual void _setEnable(bool value);
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial expander)
+  * Set the RS pin ( 0 = Command, 1 = Data).
+  */   
     virtual void _setRS(bool value);  
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial expander)
+  * Set the BL pin (0 = Backlight Off, 1 = Backlight On).
+  */   
     virtual void _setBL(bool value);
-    virtual void _setData(int value); 
-
-// Write data to MCP23008 I2C portexpander
+    
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial expander)
+  * Set the databus value (4 bit).
+  */   
+    virtual void _setData(int value);
+    
+    
+/** Write data to MCP23008 I2C portexpander
+  *  @param reg register to write
+  *  @param value data to write
+  *  @return none     
+  *
+  */
     void _write_register (int reg, int value);     
   
 //I2C bus
@@ -584,15 +676,35 @@
 
 
 private:
-//Low level writes to LCD Bus (serial or parallel)
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial expander)
+  * Set the Enable pin.
+  */
     virtual void _setEnable(bool value);
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial expander)
+  * Set the RS pin ( 0 = Command, 1 = Data).
+  */   
     virtual void _setRS(bool value);  
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial expander)
+  * Set the BL pin (0 = Backlight Off, 1 = Backlight On).
+  */   
     virtual void _setBL(bool value);
+    
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial expander)
+  * Set the databus value (4 bit).
+  */   
     virtual void _setData(int value);
+    
+/** Implementation of Low level writes to LCD Bus (serial expander)
+  * Set the CS pin (0 = select, 1 = deselect).
+  */   
     virtual void _setCS(bool value);
     
-//Low level writes to LCD serial bus only
-    void _writeBus();      
+///** Low level writes to LCD serial bus only (serial expander)
+//  */
+//    void _writeBus();      
    
 // SPI bus        
     SPI *_spi;
@@ -627,11 +739,31 @@
     virtual ~TextLCD_SPI_N(void);
 
 private:
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial native)
+  * Set the Enable pin.
+  */
     virtual void _setEnable(bool value);
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial native)
+  * Set the RS pin ( 0 = Command, 1 = Data).
+  */   
     virtual void _setRS(bool value);  
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial native)
+  * Set the BL pin (0 = Backlight Off, 1 = Backlight On).
+  */   
     virtual void _setBL(bool value);
+    
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial native)
+  * Set the databus value (4 bit).
+  */   
     virtual void _setData(int value);
+
+/** Low level writes to LCD serial bus only (serial native)
+  */
     virtual void _writeByte(int value);
+
    
 // SPI bus        
     SPI *_spi;
@@ -663,12 +795,32 @@
     virtual ~TextLCD_I2C_N(void);
 
 private:
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial native)
+  * Set the Enable pin.
+  */
     virtual void _setEnable(bool value);
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial native)
+  * Set the RS pin ( 0 = Command, 1 = Data).
+  */   
     virtual void _setRS(bool value);  
+
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial native)
+  * Set the BL pin (0 = Backlight Off, 1 = Backlight On).
+  */   
     virtual void _setBL(bool value);
+    
+/** Implementation of pure Virtual Low level writes to LCD Bus (serial native)
+  * Set the databus value (4 bit).
+  */   
     virtual void _setData(int value);
+
+/** Low level writes to LCD serial bus only (serial native)
+  */
     virtual void _writeByte(int value);
 
+
 //I2C bus
     I2C *_i2c;
     char _slaveAddress;