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:
37:ce348c002929
Parent:
36:9f5f86dfd44a
Child:
38:cbe275b0b647
--- a/TextLCD.cpp	Tue Nov 25 19:21:18 2014 +0000
+++ b/TextLCD.cpp	Sun Mar 29 13:08:03 2015 +0000
@@ -1,4 +1,4 @@
-/* mbed TextLCD Library, for a 4-bit LCD based on HD44780
+/* mbed TextLCD Library, for LCDs based on HD44780 controllers
  * Copyright (c) 2007-2010, sford, http://mbed.org
  *               2013, v01: WH, Added LCD types, fixed LCD address issues, added Cursor and UDCs 
  *               2013, v02: WH, Added I2C and SPI bus interfaces  
@@ -17,6 +17,7 @@
  *                              added 16 UDCs for supported devices (eg PCF2103), moved UDC defines to TextLCD_UDC file, added TextLCD_Config.h for feature and footprint settings.
  *               2014, v15: WH, Added AC780 support, added I2C expander modules, fixed setBacklight() for inverted logic modules. Fixed bug in LCD_SPI_N define 
  *               2014, v16: WH, Added ST7070 and KS0073 support, added setIcon(), clrIcon() and setInvert() method for supported devices 
+ *               2015, v17: WH, Clean up low-level _writeCommand() and _writeData(), Added support for alternative fonttables (eg PCF21XX), Added ST7066_ACM controller for ACM1602 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
@@ -57,6 +58,9 @@
 
   // Addressing mode encoded in b19..b16  
   _addr_mode = _type & LCD_T_ADR_MSK;
+  
+  // Font table, encoded in LCDCtrl  
+  _font = _type & LCD_C_FNT_MSK;
 }
 
 /**  Init the LCD Controller(s)
@@ -100,33 +104,35 @@
     
     wait_ms(20);         // Wait 20ms to ensure powered up
 
-    // The Controller could be in 8 bit mode (power-on reset) or in 4 bit mode (warm reboot) at this point.
-    // Follow this procedure to make sure the Controller enters the correct state. The hardware interface
-    // between the uP and the LCD can only write the 4 most significant bits (Most Significant Nibble, MSN).
-    // In 4 bit mode the LCD expects the MSN first, followed by the LSN.
-    //
-    //    Current state:               8 bit mode                |  4 bit mode, MSN is next      | 4 bit mode, LSN is next          
-                         //-------------------------------------------------------------------------------------------------                          
-    _writeNibble(0x3);   //  set 8 bit mode (MSN) and dummy LSN, |   set 8 bit mode (MSN),       |    set dummy LSN, 
-                         //  remains in 8 bit mode               |    change to 8 bit mode       |  remains in 4 bit mode
-    wait_ms(15);         //                           
+    if (dl == _LCD_DL_4) {
+      // The Controller could be in 8 bit mode (power-on reset) or in 4 bit mode (warm reboot) at this point.
+      // Follow this procedure to make sure the Controller enters the correct state. The hardware interface
+      // between the uP and the LCD can only write the 4 most significant bits (Most Significant Nibble, MSN).
+      // In 4 bit mode the LCD expects the MSN first, followed by the LSN.
+      //
+      //    Current state:               8 bit mode                |  4 bit mode, MSN is next      | 4 bit mode, LSN is next          
+                           //-------------------------------------------------------------------------------------------------                          
+      _writeNibble(0x3);   //  set 8 bit mode (MSN) and dummy LSN, |   set 8 bit mode (MSN),       |    set dummy LSN, 
+                           //  remains in 8 bit mode               |    change to 8 bit mode       |  remains in 4 bit mode
+      wait_ms(15);         //                           
+     
+      _writeNibble(0x3);   //  set 8 bit mode and dummy LSN,       | set 8 bit mode and dummy LSN, |    set 8bit mode (MSN), 
+                           //  remains in 8 bit mode               |   remains in 8 bit mode       |  remains in 4 bit mode
+      wait_ms(15);         // 
     
-    _writeNibble(0x3);   //  set 8 bit mode and dummy LSN,       | set 8 bit mode and dummy LSN, |    set 8bit mode (MSN), 
-                         //  remains in 8 bit mode               |   remains in 8 bit mode       |  remains in 4 bit mode
-    wait_ms(15);         // 
-    
-    _writeNibble(0x3);   //  set 8 bit mode and dummy LSN,       | set 8 bit mode and dummy LSN, |    set dummy LSN, 
-                         //  remains in 8 bit mode               |   remains in 8 bit mode       |  change to 8 bit mode
-    wait_ms(15);         // 
-
-    // Controller is now in 8 bit mode
-
-    _writeNibble(0x2);   // Change to 4-bit mode (MSN), the LSN is undefined dummy
-    wait_us(40);         // most instructions take 40us
-
-    // Display is now in 4-bit mode
-    // Note: 4/8 bit mode is ignored for most native SPI and I2C devices. They dont use the parallel bus.
-    //       However, _writeNibble() method is void anyway for native SPI and I2C devices.
+      _writeNibble(0x3);   //  set 8 bit mode and dummy LSN,       | set 8 bit mode and dummy LSN, |    set dummy LSN, 
+                           //  remains in 8 bit mode               |   remains in 8 bit mode       |  change to 8 bit mode
+      wait_ms(15);         // 
+
+      // Controller is now in 8 bit mode
+
+      _writeNibble(0x2);   // Change to 4-bit mode (MSN), the LSN is undefined dummy
+      wait_us(40);         // most instructions take 40us
+
+      // Controller is now in 4-bit mode
+      // Note: 4/8 bit mode is ignored for most native SPI and I2C devices. They dont use the parallel bus.
+      //       However, _writeNibble() method is void anyway for native SPI and I2C devices.
+    }      
    
     // Device specific initialisations: DC/DC converter to generate VLCD or VLED, number of lines etc
     switch (_ctrl) {
@@ -1193,6 +1199,7 @@
           _writeCommand(0x20 | _function | ((~_contrast) >> 4));        // Invert and shift to use 2 MSBs     
           break; // case PT6314 Controller (VFD)
            
+        case ST7066_ACM:                                                // ST7066 4/8 bit, I2C on ACM1602 using a PIC        
         default:
           // Devices fully compatible to HD44780 that do not use any DC/DC Voltage converters but external VLCD, no icons etc
 
@@ -1329,9 +1336,12 @@
       }      
     }
     else {
-      //Character to write      
-      _writeData(value); 
-              
+      //Character to write
+#if (LCD_DEFAULT_FONT == 1)      
+      _writeData(value);
+#else
+      _writeData(ASCII_2_LCD(value));
+#endif              
       //Update Cursor
       _column++;
       if (_column >= columns()) {
@@ -1356,6 +1366,30 @@
     return -1;
 }
 
+/** Convert ASCII character code to the LCD fonttable code
+  *
+  * @param c The character to write to the display
+  * @return The character code for the specific fonttable of the controller
+  */
+int TextLCD_Base::ASCII_2_LCD (int c) {
+    
+//LCD_C_FT0 is default for HD44780 and compatible series
+    if (_font == LCD_C_FT0) return c;
+
+//LCD_C_FT1 for PCF21XXC series    
+//Used code from Suga koubou library for PCF2119
+    if (((c >= ' ') && (c <= '?')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
+        c |= 0x80;
+    } else if (c >= 0xf0 && c <= 0xff) {
+        c &= 0x0f;
+    }
+    return c;
+
+//LCD_C_FT2 ...
+//@TODO add more, eg cyrillic 
+//@TODO add method to switch between fonts for controllers that support this
+}
+
 
 #if(LCD_PRINTF != 1)
 /** Write a character to the LCD
@@ -1383,17 +1417,15 @@
 #endif    
 
 
-
 // Write a nibble using the 4-bit interface
 void TextLCD_Base::_writeNibble(int value) {
 
 // Enable is Low
     this->_setEnable(true);        
-    this->_setData(value & 0x0F);   // Low nibble
+    this->_setData(value);        // Low nibble
     wait_us(1); // Data setup time        
     this->_setEnable(false);    
     wait_us(1); // Datahold time
-
 // Enable is Low
 }
 
@@ -1408,7 +1440,7 @@
     wait_us(1); // Data hold time
     
     this->_setEnable(true);        
-    this->_setData(value >> 0);   // Low nibble
+    this->_setData(value);        // Low nibble
     wait_us(1); // Data setup time        
     this->_setEnable(false);    
     wait_us(1); // Datahold time
@@ -2644,8 +2676,8 @@
     // No Hardware Enable pin       
     _e2 = NULL;                 //Construct dummy pin     
   }  
-                                                                           
-  _init();
+
+  _init(_LCD_DL_4);   // Set Datalength to 4 bit for mbed bus interfaces
 }
 
 /** Destruct a TextLCD interface for using regular mbed pins
@@ -2742,23 +2774,27 @@
   
 #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  
+  _writeRegister(IODIR,   0x00);  // All pins are outputs
+  _writeRegister(IPOL,    0x00);  // No reverse polarity on inputs
+  _writeRegister(GPINTEN, 0x00);  // No interrupt on change of input pins
+  _writeRegister(DEFVAL,  0x00);  // Default value to compare against for interrupts
+  _writeRegister(INTCON,  0x00);  // No interrupt on changes, compare against previous pin value 
+  _writeRegister(IOCON,   0x20);  // b1=0 - Interrupt polarity active low  
+                                  // b2=0 - Interrupt pin active driver output  
+                                  // b4=0 - Slew rate enable on SDA
+                                  // b5=0 - Auto-increment on registeraddress                                  
+                                  // b5=1 - No auto-increment on registeraddress => needed for performance improved I2C expander mode
+  _writeRegister(GPPU,    0x00);  // No Pullup 
+//               INTF             // Interrupt flags read (Read-Only)
+//               INTCAP           // Captured inputpins at time of interrupt (Read-Only)   
+//  _writeRegister(GPIO,    0x00);  // Output/Input pins   
+//  _writeRegister(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);      
+  _writeRegister(GPIO, _lcd_bus);      
 #else
   // PCF8574 of PCF8574A portexpander
 
@@ -2769,12 +2805,12 @@
   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
 #endif
 
-  _init();    
+  _init(_LCD_DL_4);   // Set Datalength to 4 bit for all serial expander interfaces
 }
 
-// Set E pin (or E2 pin)
-// Used for mbed pins, I2C bus expander or SPI shiftregister
-void TextLCD_I2C::_setEnable(bool value) {
+// Set E bit (or E2 bit) in the databus shadowvalue
+// Used for mbed I2C bus expander
+void TextLCD_I2C::_setEnableBit(bool value) {
 
   if(_ctrl_idx==_LCDCtrl_0) {
     if (value) {
@@ -2792,12 +2828,20 @@
       _lcd_bus &= ~D_LCD_E2;   // Reset E2bit                     
     }  
   }    
+}    
+
+// Set E pin (or E2 pin)
+// Used for mbed pins, I2C bus expander or SPI shiftregister
+void TextLCD_I2C::_setEnable(bool value) {
+
+  // Place the E or E2 bit data on the databus shadowvalue
+  _setEnableBit(value);
 
 #if (MCP23008==1)
   // MCP23008 portexpander
   
   // write the new data to the portexpander
-  _write_register(GPIO, _lcd_bus);      
+  _writeRegister(GPIO, _lcd_bus);      
 #else
   // PCF8574 of PCF8574A portexpander
 
@@ -2806,6 +2850,7 @@
 #endif
 }    
 
+
 // Set RS pin
 // Used for mbed pins, I2C bus expander or SPI shiftregister
 void TextLCD_I2C::_setRS(bool value) {
@@ -2821,7 +2866,7 @@
   // MCP23008 portexpander
   
   // write the new data to the portexpander
-  _write_register(GPIO, _lcd_bus);      
+  _writeRegister(GPIO, _lcd_bus);      
 #else
   // PCF8574 of PCF8574A portexpander
 
@@ -2845,7 +2890,63 @@
   // MCP23008 portexpander
   
   // write the new data to the portexpander
-  _write_register(GPIO, _lcd_bus);      
+  _writeRegister(GPIO, _lcd_bus);      
+#else
+  // PCF8574 of PCF8574A portexpander
+
+  // write the new data to the I2C portexpander
+  _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+#endif                 
+}    
+
+
+// Place the 4bit data in the databus shadowvalue
+// Used for mbed I2C bus expander
+void TextLCD_I2C::_setDataBits(int value) {
+
+  // Set bit by bit to support any mapping of expander portpins to LCD pins 
+  if (value & 0x01){
+    _lcd_bus |= D_LCD_D4;   // Set Databit 
+  }  
+  else { 
+    _lcd_bus &= ~D_LCD_D4;  // Reset Databit
+  }  
+
+  if (value & 0x02){
+    _lcd_bus |= D_LCD_D5;   // Set Databit 
+  }  
+  else {
+    _lcd_bus &= ~D_LCD_D5;  // Reset Databit
+  }  
+
+  if (value & 0x04) {
+    _lcd_bus |= D_LCD_D6;   // Set Databit 
+  }  
+  else {                    
+    _lcd_bus &= ~D_LCD_D6;  // Reset Databit
+  }  
+
+  if (value & 0x08) {
+    _lcd_bus |= D_LCD_D7;   // Set Databit 
+  }  
+  else {
+    _lcd_bus &= ~D_LCD_D7;  // Reset Databit
+  }                                      
+}    
+
+// Place the 4bit data on the databus
+// Used for mbed pins, I2C bus expander or SPI shifregister
+void TextLCD_I2C::_setData(int value) {
+
+  // Place the 4bit data on the databus shadowvalue
+  _setDataBits(value); 
+  
+  // Place the 4bit data on the databus
+#if (MCP23008==1)
+  // MCP23008 portexpander
+  
+  // write the new data to the portexpander
+  _writeRegister(GPIO, _lcd_bus);      
 #else
   // PCF8574 of PCF8574A portexpander
 
@@ -2854,65 +2955,207 @@
 #endif                 
 }    
 
+// Write data to MCP23008 I2C portexpander
+// Used for mbed I2C bus expander
+void TextLCD_I2C::_writeRegister (int reg, int value) {
+  char data[] = {reg, value};
+    
+  _i2c->write(_slaveAddress, data, 2); 
+}
+
+//New optimized
+//Test faster _writeByte 0.11s vs 0.27s for a 20x4 fillscreen (PCF8574)
+//Test faster _writeByte 0.14s vs 0.34s for a 20x4 fillscreen (MCP23008)
+
+// Write a byte using I2C
+void TextLCD_I2C::_writeByte(int value) {
+  char data[6];
+  
+#if (MCP23008==1)
+  // MCP23008 portexpander
+
+  data[0] = GPIO;                 // set registeraddres
+                                  // Note: auto-increment is disabled so all data will go to GPIO register
+  
+  _setEnableBit(true);            // set E 
+  _setDataBits(value >> 4);       // set data high  
+  data[1] = _lcd_bus;
+  
+  _setEnableBit(false);           // clear E   
+  data[2] = _lcd_bus;
+  
+  _setEnableBit(true);            // set E   
+  _setDataBits(value);            // set data low    
+  data[3] = _lcd_bus;
+  
+  _setEnableBit(false);           // clear E     
+  data[4] = _lcd_bus;
+  
+  // write the packed data to the I2C portexpander
+  _i2c->write(_slaveAddress, data, 5);    
+#else
+  // PCF8574 of PCF8574A portexpander
+  
+  _setEnableBit(true);            // set E 
+  _setDataBits(value >> 4);       // set data high  
+  data[0] = _lcd_bus;
+  
+  _setEnableBit(false);           // clear E   
+  data[1] = _lcd_bus;
+  
+  _setEnableBit(true);            // set E   
+  _setDataBits(value);            // set data low    
+  data[2] = _lcd_bus;
+  
+  _setEnableBit(false);           // clear E     
+  data[3] = _lcd_bus;
+  
+  // write the packed data to the I2C portexpander
+  _i2c->write(_slaveAddress, data, 4);    
+#endif
+}
+
+#endif /* I2C Expander PCF8574/MCP23008 */
+//---------- End TextLCD_I2C ------------
+
+
+//--------- Start TextLCD_SPI -----------
+#if(LCD_SPI == 1) /* SPI Expander SN74595          */
+
+ /** 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),        
+                         _cs(cs) {      
+        
+  // Init cs
+  _cs = 1;  
+
+  // Setup the spi for 8 bit data, low steady state clock,
+  // rising edge capture, with a 500KHz or 1MHz clock rate  
+  _spi->format(8,0);
+  _spi->frequency(500000);    
+  //_spi.frequency(1000000);    
+
+  // Init the portexpander bus
+  _lcd_bus = D_LCD_BUS_DEF;
+  
+  // write the new data to the portexpander
+  _cs = 0;  
+  _spi->write(_lcd_bus);   
+  _cs = 1;  
+
+  _init(_LCD_DL_4);   // Set Datalength to 4 bit for all serial expander interfaces
+}
+
+// Set E pin (or E2 pin)
+// 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                     
+    }  
+  }
+                  
+  // write the new data to the SPI portexpander
+  _cs = 0;  
+  _spi->write(_lcd_bus);   
+  _cs = 1;    
+}    
+
+// Set RS pin
+// Used for mbed pins, I2C bus expander or SPI shiftregister and SPI_N
+void TextLCD_SPI::_setRS(bool value) {
+
+  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 SPI portexpander
+  _cs = 0;  
+  _spi->write(_lcd_bus);   
+  _cs = 1;     
+}    
+
+// Set BL pin
+// Used for mbed pins, I2C bus expander or SPI shiftregister
+void TextLCD_SPI::_setBL(bool value) {
+
+  if (value) {
+    _lcd_bus |= D_LCD_BL;    // Set BL bit 
+  }  
+  else {
+    _lcd_bus &= ~D_LCD_BL;   // Reset BL bit                     
+  }
+      
+  // write the new data to the SPI portexpander
+  _cs = 0;  
+  _spi->write(_lcd_bus);   
+  _cs = 1;      
+}    
 
 // Place the 4bit data on the databus
-// Used for mbed pins, I2C bus expander or SPI shifregister
-void TextLCD_I2C::_setData(int value) {
-  int data;
+// Used for mbed pins, I2C bus expander or SPI shiftregister
+void TextLCD_SPI::_setData(int value) {
 
   // Set bit by bit to support any mapping of expander portpins to LCD pins
-  
-  data = value & 0x0F;
-  if (data & 0x01){
+  if (value & 0x01) {
     _lcd_bus |= D_LCD_D4;   // Set Databit 
   }  
-  else { 
-    _lcd_bus &= ~D_LCD_D4;  // Reset Databit
-  }  
-
-  if (data & 0x02){
+  else {                    
+    _lcd_bus &= ~D_LCD_D4;  // Reset Databit                     
+  }
+  
+  if (value & 0x02) {
     _lcd_bus |= D_LCD_D5;   // Set Databit 
   }  
   else {
-    _lcd_bus &= ~D_LCD_D5;  // Reset Databit
-  }  
-
-  if (data & 0x04) {
+    _lcd_bus &= ~D_LCD_D5;  // Reset Databit                     
+  }
+  
+  if (value & 0x04) {
     _lcd_bus |= D_LCD_D6;   // Set Databit 
   }  
-  else {                    
-    _lcd_bus &= ~D_LCD_D6;  // Reset Databit
-  }  
-
-  if (data & 0x08) {
+  else {
+    _lcd_bus &= ~D_LCD_D6;  // Reset Databit                     
+  }
+  
+  if (value & 0x08) {
     _lcd_bus |= D_LCD_D7;   // Set 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);    
-#endif
-                 
+  // write the new data to the SPI portexpander
+  _cs = 0;  
+  _spi->write(_lcd_bus);   
+  _cs = 1;       
 }    
 
-// Write data to MCP23008 I2C portexpander
-void TextLCD_I2C::_write_register (int reg, int value) {
-  char data[] = {reg, value};
-    
-  _i2c->write(_slaveAddress, data, 2); 
-}
-#endif /* I2C Expander PCF8574/MCP23008 */
-//---------- End TextLCD_I2C ------------
+#endif /* SPI Expander SN74595          */
+//---------- End TextLCD_SPI ------------
 
 
 //--------- Start TextLCD_I2C_N ---------
@@ -3015,6 +3258,7 @@
   _i2c->write(_slaveAddress, data, 2); 
 #else  
 //Controllers that dont support ACK
+//Note: This may be issue with some mbed platforms that dont fully/correctly support I2C byte operations.
   _i2c->start(); 
   _i2c->write(_slaveAddress);   
   _i2c->write(data[0]); 
@@ -3026,159 +3270,6 @@
 //-------- End TextLCD_I2C_N ------------
 
 
-//--------- Start TextLCD_SPI -----------
-#if(LCD_SPI == 1) /* SPI Expander SN74595          */
-
- /** 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),        
-                         _cs(cs) {      
-        
-  // Init cs
-  _setCS(true);  
-
-  // Setup the spi for 8 bit data, low steady state clock,
-  // rising edge capture, with a 500KHz or 1MHz clock rate  
-  _spi->format(8,0);
-  _spi->frequency(500000);    
-  //_spi.frequency(1000000);    
-
-  // Init the portexpander bus
-  _lcd_bus = D_LCD_BUS_DEF;
-  
-  // write the new data to the portexpander
-  _setCS(false);  
-  _spi->write(_lcd_bus);   
-  _setCS(true);  
-
-  _init();   
-}
-
-// Set E pin (or E2 pin)
-// 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                     
-    }  
-  }
-                  
-  // write the new data to the SPI portexpander
-  _setCS(false);  
-  _spi->write(_lcd_bus);   
-  _setCS(true);    
-}    
-
-// Set RS pin
-// Used for mbed pins, I2C bus expander or SPI shiftregister and SPI_N
-void TextLCD_SPI::_setRS(bool value) {
-
-  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 SPI portexpander
-  _setCS(false);  
-  _spi->write(_lcd_bus);   
-  _setCS(true);     
-}    
-
-// Set BL pin
-// Used for mbed pins, I2C bus expander or SPI shiftregister
-void TextLCD_SPI::_setBL(bool value) {
-
-  if (value) {
-    _lcd_bus |= D_LCD_BL;    // Set BL bit 
-  }  
-  else {
-    _lcd_bus &= ~D_LCD_BL;   // Reset BL bit                     
-  }
-      
-  // write the new data to the SPI portexpander
-  _setCS(false);  
-  _spi->write(_lcd_bus);   
-  _setCS(true);      
-}    
-
-// Place the 4bit data on the databus
-// Used for mbed pins, I2C bus expander or SPI shiftregister
-void TextLCD_SPI::_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                     
-  }
-  
-  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 & 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);          
-}    
-
-// Set CS line.
-// Only used for SPI bus
-void TextLCD_SPI::_setCS(bool value) {
-
-  if (value) {   
-    _cs  = 1;    // Set CS pin 
-  }  
-  else {
-    _cs  = 0;    // Reset CS pin 
-  }
-}
-#endif /* SPI Expander SN74595          */
-//---------- End TextLCD_SPI ------------
-
-
 //--------- Start TextLCD_SPI_N ---------
 #if(LCD_SPI_N == 1) /* Native SPI bus     */
  /** Create a TextLCD interface using a controller with a native SPI4 interface
@@ -3202,12 +3293,12 @@
   // Setup the spi for 8 bit data, high steady state clock,
   // rising edge capture, with a 500KHz or 1MHz clock rate    
 //  _spi->format(8,3);  
+//  _spi->frequency(500000); 
 //  _spi->frequency(1000000);    
   
   // Setup the spi for 8 bit data, low steady state clock,
   // rising edge capture, with a 500KHz or 1MHz clock rate  
   _spi->format(8,0);
-//  _spi->frequency(300000);    
 //  _spi->frequency(500000); 
   _spi->frequency(1000000);    
     
@@ -3292,13 +3383,12 @@
   // Setup the spi for 8 bit data, high steady state clock,
   // rising edge capture, with a 500KHz or 1MHz clock rate  
 //  _spi->format(8,3);  
-//  _spi->frequency(300000);    
+//  _spi->frequency(500000);    
 //  _spi->frequency(1000000);    
 
   // Setup the spi for 8 bit data, low steady state clock,
   // rising edge capture, with a 500KHz or 1MHz clock rate  
   _spi->format(8,0);
-//  _spi->frequency(300000);    
 //  _spi->frequency(500000); 
   _spi->frequency(1000000);