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:
14:0c32b66b14b8
Parent:
13:24506ba22480
Child:
15:b70ebfffb258
--- a/TextLCD.cpp	Sat Feb 09 15:10:36 2013 +0000
+++ b/TextLCD.cpp	Sun Feb 10 18:43:51 2013 +0000
@@ -1,6 +1,7 @@
 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
  * Copyright (c) 2007-2010, sford, http://mbed.org
- *               2013, WH, Added LCD types, fixed LCD address issues, added Cursor and UDCs
+ *               2013, v01: WH, Added LCD types, fixed LCD address issues, added Cursor and UDCs 
+ *               2013, v02: WH, Added I2C and SPI bus interfaces  
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -28,6 +29,7 @@
                  PinName d4, PinName d5, PinName d6, PinName d7,
                  LCDType type): _rs(rs), _e(e),
                                 _d(d4, d5, d6, d7),
+                                _cs(NC),                                                
                                 _type(type) {
 
 
@@ -40,6 +42,7 @@
 
 TextLCD::TextLCD(I2C *i2c, char deviceAddress, LCDType type) :
         _rs(NC), _e(NC), _d(NC),
+        _cs(NC),                
         _i2c(i2c),
         _type(type) {
         
@@ -57,7 +60,36 @@
     
 }
 
-/** Init the LCD controller
+
+TextLCD::TextLCD(SPI *spi, PinName cs, LCDType type) :
+        _rs(NC), _e(NC), _d(NC),
+        _spi(spi),
+        _cs(cs),        
+        _type(type) {
+        
+  _busType = _SPIBus;
+
+  // 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 = 0x80;
+  
+  // write the new data to the portexpander
+  _setCS(false);  
+  _spi->write(_lcd_bus);   
+  _setCS(true);  
+  
+  _init();
+    
+}
+
+
+/*  Init the LCD controller
  *  4-bit mode, number of lines, no cursor etc
  *  Clear display 
  */
@@ -68,16 +100,19 @@
     _setEnable(true);    
     _setRS(false);      // command mode
     
-    wait(0.015);        // Wait 15ms to ensure powered up
+//    wait(0.015);        // Wait 15ms to ensure powered up
+    wait_ms(15);        // Wait 15ms 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++) {
         _writeByte(0x3);
-        wait(0.00164);  // this command takes 1.64ms, so wait for it
+//        wait(0.00164);  // this command takes 1.64ms, so wait for it
+        wait_ms(10);    // this command takes 1.64ms, so wait for it 
     }
     _writeByte(0x2);     // 4-bit mode
-    wait(0.000040f);     // most instructions take 40us
-
+//    wait(0.000040f);     // most instructions take 40us
+    wait_us(40);         // most instructions take 40us
+    
     // Display is now in 4-bit mode
     switch (_type) {
         case LCD8x1:
@@ -144,7 +179,9 @@
 
 void TextLCD::cls() {
     _writeCommand(0x01); // cls, and set cursor to 0
-    wait(0.00164f);      // This command takes 1.64 ms
+//    wait(0.00164f);      // This command takes 1.64 ms
+    wait_ms(10);     // The CLS command takes 1.64 ms.
+                     // Since we are not using the Busy flag, Lets be safe and take 10 ms
     locate(0, 0);
 }
 
@@ -191,17 +228,27 @@
                     break;  
     
     case _I2CBus : 
-                    if (value)
-                      _lcd_bus |= D_LCD_E;     // Set E bit 
-                    else                     
-                      _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
+                   if (value)
+                     _lcd_bus |= D_LCD_E;     // Set E bit 
+                   else                     
+                     _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
 
-                    // write the new data to the portexpander
-                    _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+                   // write the new data to the portexpander
+                   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
                    
                    break;  
     
     case _SPIBus :
+                   if (value)
+                     _lcd_bus |= D_LCD_E;     // Set E bit 
+                   else                     
+                     _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
+         
+                   // write the new data to the portexpander
+                   _setCS(false);  
+                   _spi->write(_lcd_bus);   
+                   _setCS(true);  
+  
                    break;
   }
 }    
@@ -218,17 +265,27 @@
                     break;  
     
     case _I2CBus : 
-                    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 portexpander
-                    _i2c->write(_slaveAddress, &_lcd_bus, 1);    
+                   // write the new data to the portexpander
+                   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
                    
                    break;
                        
     case _SPIBus :
+                   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 portexpander
+                   _setCS(false);  
+                   _spi->write(_lcd_bus);   
+                   _setCS(true);  
+     
                    break;
   }
 
@@ -271,28 +328,70 @@
                     break;                    
                     
     case _SPIBus :
-                    break;
+    
+                    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 portexpander
+                   _setCS(false);  
+                   _spi->write(_lcd_bus);   
+                   _setCS(true);  
+        
+                   break;
   }
 
 }    
 
 
+// Set CS line. Only used for SPI bus
+void TextLCD::_setCS(bool value) {
+
+  if (value)
+    _cs  = 1;    // Set CS pin 
+  else  
+    _cs  = 0;    // Reset CS pin 
+
+}
+
+
 
 void TextLCD::_writeByte(int value) {
 //    _d = value >> 4;
     _setData(value >> 4);
-    wait(0.000040f); // most instructions take 40us
+//    wait(0.000040f); // most instructions take 40us
+    wait_us(40); // most instructions take 40us    
 //    _e = 0;
     _setEnable(false);
-    wait(0.000040f);
+//    wait(0.000040f);
+    wait_us(40); // most instructions take 40us        
 //    _e = 1;
     _setEnable(true);        
 //    _d = value >> 0;
     _setData(value >> 0);    
-    wait(0.000040f);
+//    wait(0.000040f);
+    wait_us(40); // most instructions take 40us        
 //    _e = 0;
     _setEnable(false);    
-    wait(0.000040f);  // most instructions take 40us
+//    wait(0.000040f);  // most instructions take 40us
+    wait_us(40); // most instructions take 40us        
 //    _e = 1;
     _setEnable(true);    
 }