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.cpp	Tue Apr 01 21:30:25 2014 +0000
+++ b/TextLCD.cpp	Wed Apr 02 17:49:55 2014 +0000
@@ -7,7 +7,7 @@
  *               2013, v05: WH, Added support for 8x2B, added some UDCs   
  *               2013, v06: WH, Added support for devices that use internal DC/DC converters 
  *               2013, v07: WH, Added support for backlight and include portdefinitions for LCD2004 Module from DFROBOT 
- *               2014, v08: WH, Refactored in Base in Derived Classes to deal with mbed lib change regarding 'NC' defined pins 
+ *               2014, v08: WH, Refactored in Base and Derived Classes to deal with mbed lib change regarding 'NC' defined pins 
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -723,9 +723,11 @@
   
 }
 
+///--------- End TextLCD_Base -----------
 
-///-------------------------------------------------------------------
+
 
+///--------- Start TextLCD Bus -----------
 
 /* Create a TextLCD interface for using regular mbed pins
  *
@@ -741,29 +743,56 @@
                  PinName d4, PinName d5, PinName d6, PinName d7,
                  LCDType type, PinName bl, PinName e2, LCDCtrl ctrl) :
                  TextLCD_Base(type, ctrl), 
-                 _rs(rs), _e(e), _bl(bl), _e2(e2),
-                 _d(d4, d5, d6, d7) {
+                 _rs(rs), _e(e), _d(d4, d5, d6, d7) {
+
+  // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
+  if (bl != NC) {
+    _bl = new DigitalOut(bl);   //Construct new pin 
+    _bl->write(0);              //Deactivate    
+  }
+  else {
+    // No Hardware Backlight pin       
+    _bl = NULL;                 //Construct dummy pin     
+  }  
+
+  // The hardware Enable2 pin is only needed for LCD40x4. Test and make sure whether it exists or not to prevent illegal access.
+  if (e2 != NC) {
+    _e2 = new DigitalOut(e2);   //Construct new pin 
+    _e2->write(0);              //Deactivate    
+  }
+  else {
+    // No Hardware Enable pin       
+    _e2 = NULL;                 //Construct dummy pin     
+  }  
                                                                            
   _init();
 
 }
 
-// Set E pin (or E2 pin)
-// Used for mbed pins, I2C bus expander or SPI shiftregister
+/** Set E pin (or E2 pin)
+  * Used for mbed pins, I2C bus expander or SPI shiftregister
+  * Default PinName value for E2 is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
+  *   @param  value true or false
+  *   @return none 
+  */
 void TextLCD::_setEnable(bool value) {
 
-                    if(_ctrl_idx==_LCDCtrl_0) {
-                      if (value)
-                        _e  = 1;    // Set E bit 
-                      else  
-                        _e  = 0;    // Reset E bit  
-                    }    
-                    else {   
-                      if (value)
-                        _e2 = 1;    // Set E2 bit 
-                      else  
-                        _e2 = 0;    // Reset E2 bit  
-                    }    
+  if(_ctrl_idx==_LCDCtrl_0) {
+    if (value) {
+      _e  = 1;    // Set E bit 
+    }  
+    else { 
+      _e  = 0;    // Reset E bit  
+    }  
+  }    
+  else { 
+    if (value) {
+      if (_e2 != NULL) {_e2->write(1);}  //Set E2 bit
+    }  
+    else { 
+      if (_e2 != NULL) {_e2->write(0);}  //Reset E2 bit     
+    }  
+  }    
 
 }    
 
@@ -771,50 +800,66 @@
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD::_setRS(bool value) {
 
-  if (value)
+  if (value) {
     _rs  = 1;    // Set RS bit 
-  else  
+  }  
+  else  {
     _rs  = 0;    // Reset RS bit 
+  }  
 
 }    
 
-// Set BL pin
-// Used for mbed pins, I2C bus expander or SPI shiftregister
+/** Set BL pin
+  * Used for mbed pins, I2C bus expander or SPI shiftregister
+  * Default PinName value is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
+  *   @param  value true or false
+  *   @return none  
+  */
 void TextLCD::_setBL(bool value) {
 
-  if (value)
-    _bl  = 1;    // Set BL bit 
-  else  
-    _bl  = 0;    // Reset BL bit 
+  if (value) {
+    if (_bl != NULL) {_bl->write(1);}  //Set BL bit
+  }  
+  else { 
+    if (_bl != NULL) {_bl->write(0);}  //Reset BL bit  
+  }  
 
 }    
 
-
-
 // Place the 4bit data on the databus
 // Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_setData(int value) {
   _d = value & 0x0F;   // Write Databits 
 }    
 
-
+/** Destruct a TextLCD interface for using regular mbed pins
+  *
+  * @param  none
+  * @return none
+  */ 
+TextLCD::~TextLCD() {
+   if (_bl != NULL) {delete _bl;}  // BL pin
+   if (_e2 != NULL) {delete _e2;}  // E2 pin
+}
 
-
-///----------------------------------------------------------------------------------------
+    
+///----------- End TextLCD ---------------
 
 
-/* Create a TextLCD interface using an I2C PC8574 portexpander
- *
- * @param i2c             I2C Bus
- * @param deviceAddress   I2C slave address (PCF8574)
- * @param type            Sets the panel size/addressing mode (default = LCD16x2)
- * @param ctrl            LCD controller (default = HD44780)    
- */
+///--------- Start TextLCD_I2C -----------
+
+/** Create a TextLCD interface using an I2C PC8574 or PCF8574A portexpander
+  *
+  * @param i2c             I2C Bus
+  * @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::TextLCD_I2C(I2C *i2c, char deviceAddress, LCDType type, LCDCtrl ctrl) :
                          TextLCD_Base(type, ctrl), 
                          _i2c(i2c){
                               
-  _slaveAddress = deviceAddress;
+  _slaveAddress = deviceAddress & 0xFE;
   
   // Init the portexpander bus
   _lcd_bus = D_LCD_BUS_DEF;
@@ -830,21 +875,21 @@
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_I2C::_setEnable(bool value) {
 
-                   if(_ctrl_idx==_LCDCtrl_0) {
-                     if (value)
-                       _lcd_bus |= D_LCD_E;     // Set E bit 
-                     else                     
-                       _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
-                   }
-                   else {
-                     if (value)
-                       _lcd_bus |= D_LCD_E2;    // Set E2 bit 
-                     else                     
-                       _lcd_bus &= ~D_LCD_E2;   // Reset E2bit                     
-                   }    
+  if(_ctrl_idx==_LCDCtrl_0) {
+    if (value)
+      _lcd_bus |= D_LCD_E;     // Set E bit 
+    else                     
+      _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
+  }
+  else {
+    if (value)
+      _lcd_bus |= D_LCD_E2;    // Set E2 bit 
+    else                     
+      _lcd_bus &= ~D_LCD_E2;   // Reset E2bit                     
+    }    
 
-                   // write the new data to the I2C portexpander
-                   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+  // write the new data to the I2C portexpander
+  _i2c->write(_slaveAddress, &_lcd_bus, 1);    
 
 }    
 
@@ -852,13 +897,13 @@
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_I2C::_setRS(bool value) {
 
-                   if (value)
-                     _lcd_bus |= D_LCD_RS;    // Set RS bit 
-                   else                     
-                     _lcd_bus &= ~D_LCD_RS;   // Reset RS bit                     
+  if (value)
+    _lcd_bus |= D_LCD_RS;    // Set RS bit 
+  else                     
+    _lcd_bus &= ~D_LCD_RS;   // Reset RS bit                     
 
-                   // write the new data to the I2C portexpander
-                   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+  // write the new data to the I2C portexpander
+  _i2c->write(_slaveAddress, &_lcd_bus, 1);    
                   
 }    
 
@@ -882,55 +927,48 @@
 // Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD_I2C::_setData(int value) {
   int data;
+
+  // Set bit by bit to support any mapping of expander portpins to LCD pins
   
-                    data = value & 0x0F;
-                    if (data & 0x01)
-                      _lcd_bus |= D_LCD_D4;   // Set Databit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_D4;  // Reset Databit                     
+  data = value & 0x0F;
+  if (data & 0x01)
+    _lcd_bus |= D_LCD_D4;   // Set Databit 
+  else                     
+    _lcd_bus &= ~D_LCD_D4;  // Reset Databit                     
 
-                    if (data & 0x02)
-                      _lcd_bus |= D_LCD_D5;   // Set Databit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_D5;  // Reset Databit                     
+  if (data & 0x02)
+    _lcd_bus |= D_LCD_D5;   // Set Databit 
+  else                     
+    _lcd_bus &= ~D_LCD_D5;  // Reset Databit                     
 
-                    if (data & 0x04)
-                      _lcd_bus |= D_LCD_D6;   // Set Databit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_D6;  // Reset Databit                     
+  if (data & 0x04)
+    _lcd_bus |= D_LCD_D6;   // Set Databit 
+  else                     
+    _lcd_bus &= ~D_LCD_D6;  // Reset Databit                     
 
-                    if (data & 0x08)
-                      _lcd_bus |= D_LCD_D7;   // Set Databit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_D7;  // Reset Databit                     
+  if (data & 0x08)
+    _lcd_bus |= D_LCD_D7;   // Set Databit 
+  else                     
+    _lcd_bus &= ~D_LCD_D7;  // Reset Databit                     
                     
-                    // write the new data to the I2C portexpander
-                    _i2c->write(_slaveAddress, &_lcd_bus, 1);  
-                   
+  // write the new data to the I2C portexpander
+  _i2c->write(_slaveAddress, &_lcd_bus, 1);  
+                 
+}    
 
-}    
+///---------- End TextLCD_I2C ------------
 
 
 
-
-
-///------------------------------------------------------------------------------------
-
-
-
-
-
+///--------- Start TextLCD_SPI -----------
 
-// TextLCD_SPI Implementation
-
-
- /* Create a TextLCD interface using an SPI 74595 portexpander
-  *
-  * @param spi             SPI Bus
-  * @param cs              chip select pin (active low)
-  * @param type            Sets the panel size/addressing mode (default = LCD16x2)
-  * @param ctrl            LCD controller (default = HD44780)      
-  */
+ /** Create a TextLCD interface using an SPI 74595 portexpander
+   *
+   * @param spi             SPI Bus
+   * @param cs              chip select pin (active low)
+   * @param type            Sets the panel size/addressing mode (default = LCD16x2)
+   * @param ctrl            LCD controller (default = HD44780)      
+   */
 TextLCD_SPI::TextLCD_SPI(SPI *spi, PinName cs, LCDType type, LCDCtrl ctrl) :
                          TextLCD_Base(type, ctrl), 
                          _spi(spi),        
@@ -959,23 +997,23 @@
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_SPI::_setEnable(bool value) {
 
-                   if(_ctrl_idx==_LCDCtrl_0) {
-                     if (value)
-                       _lcd_bus |= D_LCD_E;     // Set E bit 
-                     else                     
-                       _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
-                   }
-                   else {
-                     if (value)
-                       _lcd_bus |= D_LCD_E2;    // Set E2 bit 
-                     else                     
-                       _lcd_bus &= ~D_LCD_E2;   // Reset E2 bit                     
-                   }
+  if(_ctrl_idx==_LCDCtrl_0) {
+    if (value)
+      _lcd_bus |= D_LCD_E;     // Set E bit 
+    else                     
+      _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
+  }
+  else {
+    if (value)
+      _lcd_bus |= D_LCD_E2;    // Set E2 bit 
+    else                     
+      _lcd_bus &= ~D_LCD_E2;   // Reset E2 bit                     
+  }
                   
-                   // write the new data to the SPI portexpander
-                   _setCS(false);  
-                   _spi->write(_lcd_bus);   
-                   _setCS(true);  
+  // write the new data to the SPI portexpander
+  _setCS(false);  
+  _spi->write(_lcd_bus);   
+  _setCS(true);  
   
 }    
 
@@ -983,10 +1021,12 @@
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_SPI::_setRS(bool value) {
 
-  if (value)
+  if (value) {
     _lcd_bus |= D_LCD_RS;    // Set RS bit 
-  else                     
+  }  
+  else {                    
     _lcd_bus &= ~D_LCD_RS;   // Reset RS bit                     
+  }
      
   // write the new data to the SPI portexpander
   _setCS(false);  
@@ -999,10 +1039,12 @@
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_SPI::_setBL(bool value) {
 
-  if (value)
+  if (value) {
     _lcd_bus |= D_LCD_BL;    // Set BL bit 
-  else                     
+  }  
+  else {
     _lcd_bus &= ~D_LCD_BL;   // Reset BL bit                     
+  }
       
   // write the new data to the SPI portexpander
   _setCS(false);  
@@ -1017,32 +1059,34 @@
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_SPI::_setData(int value) {
   int data;
-  
-                    data = value & 0x0F;
-                    if (data & 0x01)
-                      _lcd_bus |= D_LCD_D4;   // Set Databit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_D4;  // Reset Databit                     
 
-                    if (data & 0x02)
-                      _lcd_bus |= D_LCD_D5;   // Set Databit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_D5;  // Reset Databit                     
+  // Set bit by bit to support any mapping of expander portpins to LCD pins
+    
+  data = value & 0x0F;
+  if (data & 0x01)
+    _lcd_bus |= D_LCD_D4;   // Set Databit 
+  else                     
+    _lcd_bus &= ~D_LCD_D4;  // Reset Databit                     
+
+  if (data & 0x02)
+    _lcd_bus |= D_LCD_D5;   // Set Databit 
+  else                     
+    _lcd_bus &= ~D_LCD_D5;  // Reset Databit                     
 
-                    if (data & 0x04)
-                      _lcd_bus |= D_LCD_D6;   // Set Databit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_D6;  // Reset Databit                     
+  if (data & 0x04)
+    _lcd_bus |= D_LCD_D6;   // Set Databit 
+  else                     
+    _lcd_bus &= ~D_LCD_D6;  // Reset Databit                     
 
-                    if (data & 0x08)
-                      _lcd_bus |= D_LCD_D7;   // Set Databit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_D7;  // Reset Databit                     
+  if (data & 0x08)
+    _lcd_bus |= D_LCD_D7;   // Set Databit 
+  else                     
+    _lcd_bus &= ~D_LCD_D7;  // Reset Databit                     
                     
-                   // write the new data to the SPI portexpander
-                   _setCS(false);  
-                   _spi->write(_lcd_bus);   
-                   _setCS(true);  
+  // write the new data to the SPI portexpander
+  _setCS(false);  
+  _spi->write(_lcd_bus);   
+  _setCS(true);  
         
 }    
 
@@ -1054,18 +1098,17 @@
   if (value) {   
     _cs  = 1;    // Set CS pin 
   }  
-  else  
+  else {
     _cs  = 0;    // Reset CS pin 
-  
+  }
 }
 
 
+///---------- End TextLCD_SPI ------------
+
+
 
 
 
 
 
-
-
-
-