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:
26:bd897a001012
Parent:
25:6162b31128c9
Child:
27:22d5086f6ba6
--- a/TextLCD.cpp	Sat May 10 21:34:51 2014 +0000
+++ b/TextLCD.cpp	Fri May 30 15:05:13 2014 +0000
@@ -9,6 +9,7 @@
  *               2013, v07: WH, Added support for backlight and include portdefinitions for LCD2004 Module from DFROBOT 
  *               2014, v08: WH, Refactored in Base and Derived Classes to deal with mbed lib change regarding 'NC' defined 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  
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -78,9 +79,9 @@
   */
 void TextLCD_Base::_initCtrl() {
 
-    this->_setRS(false);      // command mode
+    this->_setRS(false); // command mode
     
-    wait_ms(20);        // Wait 20ms to ensure powered up
+    wait_ms(20);         // Wait 20ms to ensure powered up
 
     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)    
     for (int i=0; i<3; i++) {
@@ -111,16 +112,34 @@
           wait_ms(50);      
           break;
           
-      case ST7032:
+      case ST7032_3V3:
+          // ST7032 controller: Initialise Voltage booster for VLCD. VDD=3V3
+     
           _writeByte( 0x1c );    //Internal OSC frequency adjustment 183HZ, bias will be 1/4 
           wait_us(30);
-          _writeByte( 0x73 );    //Contrast control  low byte
+          _writeByte( 0x73 );    //Contrast control low byte
           wait_us(30);  
-          _writeByte( 0x57 );    //booster circuit is turn on. /ICON display off. /Contrast control   high byte
+          _writeByte( 0x57 );    //booster circuit is turned on. /ICON display off. /Contrast control high byte
           wait_us(30);
           _writeByte( 0x6c );    //Follower control
           wait_us(50);   
-          _writeByte( 0x0c );    //DISPLAY ON
+          _writeByte( 0x0c );    //DISPLAY ON, not needed
+          wait_us(30);
+          break;
+
+//Check and fix booster disable
+      case ST7032_5V:
+          // ST7032 controller: Disable Voltage booster for VLCD. VDD=5V      
+          
+          _writeByte( 0x1c );    //Internal OSC frequency adjustment 183HZ, bias will be 1/4 
+          wait_us(30);
+          _writeByte( 0x73 );    //Contrast control low byte
+          wait_us(30);  
+          _writeByte( 0x57 );    //booster circuit is turned on. /ICON display off. /Contrast control high byte
+          wait_us(30);
+          _writeByte( 0x6c );    //Follower control
+          wait_us(50);   
+          _writeByte( 0x0c );    //DISPLAY ON  --> remove, done later?
           wait_us(30);
           break;
           
@@ -858,10 +877,10 @@
 
 //--------- Start TextLCD_I2C -----------
 
-/** Create a TextLCD interface using an I2C PC8574 or PCF8574A portexpander
+/** Create a TextLCD interface using an I2C PC8574 (or PCF8574A) or MCP23008 portexpander
   *
   * @param i2c             I2C Bus
-  * @param deviceAddress   I2C slave address (PCF8574 or PCF8574A, default = 0x40)
+  * @param deviceAddress   I2C slave address (PCF8574, PCF8574A or MCP23008, default = 0x40)
   * @param type            Sets the panel size/addressing mode (default = LCD16x2)
   * @param ctrl            LCD controller (default = HD44780)    
   */
@@ -871,11 +890,35 @@
                               
   _slaveAddress = deviceAddress & 0xFE;
   
+
+#if (MCP23008==1)
+  // MCP23008 portexpander Init
+   _write_register(IODIR,   0x00);  // All outputs
+   _write_register(IPOL,    0x00);  // No reverse polarity 
+   _write_register(GPINTEN, 0x00);  // No interrupt 
+   _write_register(DEFVAL,  0x00);  // Default value to compare against for interrupts
+   _write_register(INTCON,  0x00);  // No interrupt on changes 
+   _write_register(IOCON,   0x00);  // Interrupt polarity   
+   _write_register(GPPU,    0x00);  // No Pullup 
+   _write_register(INTF,    0x00);  //    
+   _write_register(INTCAP,  0x00);  //    
+   _write_register(GPIO,    0x00);  // Output/Input pins   
+   _write_register(OLAT,    0x00);  // Output Latch  
+    
   // Init the portexpander bus
   _lcd_bus = D_LCD_BUS_DEF;
   
   // write the new data to the portexpander
+  _write_register(GPIO, _lcd_bus);      
+#else
+  // PCF8574 of PCF8574A portexpander
+
+  // Init the portexpander bus
+  _lcd_bus = D_LCD_BUS_DEF;
+
+  // write the new data to the portexpander
   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+#endif
 
   _init();
     
@@ -886,34 +929,59 @@
 void TextLCD_I2C::_setEnable(bool value) {
 
   if(_ctrl_idx==_LCDCtrl_0) {
-    if (value)
+    if (value) {
       _lcd_bus |= D_LCD_E;     // Set E bit 
-    else                     
+    }  
+    else {                    
       _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
+    }  
   }
   else {
-    if (value)
+    if (value) {
       _lcd_bus |= D_LCD_E2;    // Set E2 bit 
-    else                     
+    }  
+    else {
       _lcd_bus &= ~D_LCD_E2;   // Reset E2bit                     
-    }    
+    }  
+  }    
+
+
+#if (MCP23008==1)
+  // MCP23008 portexpander
+  
+  // write the new data to the portexpander
+  _write_register(GPIO, _lcd_bus);      
+#else
+  // PCF8574 of PCF8574A portexpander
 
   // write the new data to the I2C portexpander
   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
-
+#endif
 }    
 
 // Set RS pin
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_I2C::_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                     
+  }
+
+
+#if (MCP23008==1)
+  // MCP23008 portexpander
+  
+  // write the new data to the portexpander
+  _write_register(GPIO, _lcd_bus);      
+#else
+  // PCF8574 of PCF8574A portexpander
 
   // write the new data to the I2C portexpander
   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+#endif
                   
 }    
 
@@ -921,13 +989,24 @@
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_I2C::_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                     
+  }
+  
+#if (MCP23008==1)
+  // MCP23008 portexpander
+  
+  // write the new data to the portexpander
+  _write_register(GPIO, _lcd_bus);      
+#else
+  // PCF8574 of PCF8574A portexpander
 
   // write the new data to the I2C portexpander
   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+#endif
                  
 }    
 
@@ -941,31 +1020,56 @@
   // Set bit by bit to support any mapping of expander portpins to LCD pins
   
   data = value & 0x0F;
-  if (data & 0x01)
+  if (data & 0x01){
     _lcd_bus |= D_LCD_D4;   // Set Databit 
-  else                     
-    _lcd_bus &= ~D_LCD_D4;  // Reset Databit                     
+  }  
+  else { 
+    _lcd_bus &= ~D_LCD_D4;  // Reset Databit
+  }  
 
-  if (data & 0x02)
+  if (data & 0x02){
     _lcd_bus |= D_LCD_D5;   // Set Databit 
-  else                     
-    _lcd_bus &= ~D_LCD_D5;  // Reset Databit                     
+  }  
+  else {
+    _lcd_bus &= ~D_LCD_D5;  // Reset Databit
+  }  
 
-  if (data & 0x04)
+  if (data & 0x04) {
     _lcd_bus |= D_LCD_D6;   // Set Databit 
-  else                     
-    _lcd_bus &= ~D_LCD_D6;  // Reset Databit                     
+  }  
+  else {                    
+    _lcd_bus &= ~D_LCD_D6;  // Reset Databit
+  }  
 
-  if (data & 0x08)
+  if (data & 0x08) {
     _lcd_bus |= D_LCD_D7;   // Set Databit 
-  else                     
-    _lcd_bus &= ~D_LCD_D7;  // Reset Databit                     
+  }  
+  else {
+    _lcd_bus &= ~D_LCD_D7;  // Reset Databit
+  }  
                     
+#if (MCP23008==1)
+  // MCP23008 portexpander
+  
+  // write the new data to the portexpander
+  _write_register(GPIO, _lcd_bus);      
+#else
+  // PCF8574 of PCF8574A portexpander
+
   // write the new data to the I2C portexpander
-  _i2c->write(_slaveAddress, &_lcd_bus, 1);  
+  _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+#endif
                  
 }    
 
+// Write data to MCP23008 I2C portexpander
+void TextLCD_I2C::_write_register (int reg, int value) {
+  char data[] = {reg, value};
+    
+  _i2c->write(_slaveAddress, data, 2);
+ 
+}
+
 //---------- End TextLCD_I2C ------------
 
 
@@ -1008,16 +1112,20 @@
 void TextLCD_SPI::_setEnable(bool value) {
 
   if(_ctrl_idx==_LCDCtrl_0) {
-    if (value)
+    if (value) {
       _lcd_bus |= D_LCD_E;     // Set E bit 
-    else                     
+    }  
+    else {                    
       _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
+    }  
   }
   else {
-    if (value)
+    if (value) {
       _lcd_bus |= D_LCD_E2;    // Set E2 bit 
-    else                     
+    }  
+    else {
       _lcd_bus &= ~D_LCD_E2;   // Reset E2 bit                     
+    }  
   }
                   
   // write the new data to the SPI portexpander
@@ -1073,25 +1181,33 @@
   // Set bit by bit to support any mapping of expander portpins to LCD pins
     
   data = value & 0x0F;
-  if (data & 0x01)
+  if (data & 0x01) {
     _lcd_bus |= D_LCD_D4;   // Set Databit 
-  else                     
+  }  
+  else {                    
     _lcd_bus &= ~D_LCD_D4;  // Reset Databit                     
-
-  if (data & 0x02)
+  }
+  
+  if (data & 0x02) {
     _lcd_bus |= D_LCD_D5;   // Set Databit 
-  else                     
+  }  
+  else {
     _lcd_bus &= ~D_LCD_D5;  // Reset Databit                     
-
-  if (data & 0x04)
+  }
+  
+  if (data & 0x04) {
     _lcd_bus |= D_LCD_D6;   // Set Databit 
-  else                     
+  }  
+  else {
     _lcd_bus &= ~D_LCD_D6;  // Reset Databit                     
-
-  if (data & 0x08)
+  }
+  
+  if (data & 0x08) {
     _lcd_bus |= D_LCD_D7;   // Set Databit 
-  else                     
-    _lcd_bus &= ~D_LCD_D7;  // Reset Databit                     
+  }  
+  else {
+    _lcd_bus &= ~D_LCD_D7;  // Reset Databit
+  }  
                     
   // write the new data to the SPI portexpander
   _setCS(false);  
@@ -1125,7 +1241,7 @@
    * @param rs              Instruction/data control line
    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
    * @param bl              Backlight control line (optional, default = NC)  
-   * @param ctrl            LCD controller (default = ST7032) 
+   * @param ctrl            LCD controller (default = ST7032_3V3) 
    */       
 TextLCD_SPI_N::TextLCD_SPI_N(SPI *spi, PinName cs, PinName rs, LCDType type, PinName bl, LCDCtrl ctrl) :
                              TextLCD_Base(type, ctrl), 
@@ -1169,8 +1285,9 @@
 
 // Set BL pin
 void TextLCD_SPI_N::_setBL(bool value) {
-    if (_bl)
+    if (_bl) {
         _bl->write(value);   
+    }    
 }    
 
 // Write a byte using SPI