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:
16:c276b75e6585
Parent:
15:b70ebfffb258
Child:
17:652ab113bc2e
--- a/TextLCD.cpp	Tue Feb 19 22:09:09 2013 +0000
+++ b/TextLCD.cpp	Wed Feb 20 19:37:53 2013 +0000
@@ -220,8 +220,7 @@
 }
 
 
- 
-#if(LCD40x4Test)
+// Clear the screen, Cursor home. 
 void TextLCD::cls() {
 
   // Select and configure second LCD controller when needed
@@ -256,17 +255,7 @@
   _column=0;
 }
 
-#else
-//standard
-void TextLCD::cls() {
-    _writeCommand(0x01); // cls, and set cursor to 0
-
-    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);
-}
-#endif
-
+// Move cursor to selected row and column
 void TextLCD::locate(int column, int row) {
     
    // setAddress() does all the heavy lifting:
@@ -279,17 +268,7 @@
 }
     
 
-//Not needed in new version, is now part of _putc()
-void TextLCD::_character(int column, int row, int c) {
-  int addr = getAddress(column, row);
-    
-  _writeCommand(0x80 | addr);
-  _writeData(c);
-}
-
-
-#if(LCD40x4Test)
-
+// Write a single character (Stream implementation)
 int TextLCD::_putc(int value) {
   int addr;
     
@@ -324,46 +303,19 @@
             
     return value;
 }
-#else
-//Standard
-int TextLCD::_putc(int value) {
-    if (value == '\n') {
-        _column = 0;
-        _row++;
-        if (_row >= rows()) {
-            _row = 0;
-        }
-    } else {
-        _character(_column, _row, value);
-        _column++;
-        if (_column >= columns()) {
-            _column = 0;
-            _row++;
-            if (_row >= rows()) {
-                _row = 0;
-            }
-        }
-    }
-       
-    return value;
-}
-
-#endif
 
 
-
-
-
+// get a single character (Stream implementation)
 int TextLCD::_getc() {
     return -1;
 }
 
-
+// Set E pin (or E2 pin)
+// Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_setEnable(bool value) {
 
   switch(_busType) {
     case _PinBus : 
-#if(LCD40x4Test)
                     if(_ctrl==TextLCD::_LCDCtrl_0) {
                       if (value)
                         _e  = 1;    // Set E bit 
@@ -377,17 +329,10 @@
                         _e2 = 0;    // Reset E2 bit  
                     }    
 
-#else
-                    if (value)
-                      _e  = 1;    // Set E bit 
-                    else  
-                      _e  = 0;    // Reset E bit  
-#endif
                     break;  
     
     case _I2CBus : 
     
-#if(LCD40x4Test)
                    if(_ctrl==TextLCD::_LCDCtrl_0) {
                      if (value)
                        _lcd_bus |= D_LCD_E;     // Set E bit 
@@ -401,20 +346,12 @@
                        _lcd_bus &= ~D_LCD_E2;   // Reset E2bit                     
                    }    
 
-#else
-                   if (value)
-                     _lcd_bus |= D_LCD_E;     // Set E bit 
-                   else                     
-                     _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
-
-#endif                   
                    // write the new data to the I2C portexpander
                    _i2c->write(_slaveAddress, &_lcd_bus, 1);    
 
                    break;  
     
     case _SPIBus :
-#if(LCD40x4Test)
                    if(_ctrl==TextLCD::_LCDCtrl_0) {
                      if (value)
                        _lcd_bus |= D_LCD_E;     // Set E bit 
@@ -428,13 +365,6 @@
                        _lcd_bus &= ~D_LCD_E2;   // Reset E2 bit                     
                    }
                   
-#else    
-                   if (value)
-                     _lcd_bus |= D_LCD_E;     // Set E bit 
-                   else                     
-                     _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
-#endif         
-
                    // write the new data to the SPI portexpander
                    _setCS(false);  
                    _spi->write(_lcd_bus);   
@@ -444,6 +374,8 @@
   }
 }    
 
+// Set RS pin
+// Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_setRS(bool value) {
 
   switch(_busType) {
@@ -482,6 +414,8 @@
 
 }    
 
+// Place the 4bit data on the databus
+// Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_setData(int value) {
   int data;
   
@@ -552,7 +486,8 @@
 }    
 
 
-// Set CS line. Only used for SPI bus
+// Set CS line.
+// Only used for SPI bus
 void TextLCD::_setCS(bool value) {
 
   if (value) {   
@@ -564,18 +499,19 @@
 }
 
 
-
+// Write a byte using the 4-bit interface
+// Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_writeByte(int value) {
 
 // Enable is Low
     _setEnable(true);          
-    _setData(value >> 4);   
+    _setData(value >> 4);   // High nibble
     wait_us(1); // Data setup time    
     _setEnable(false);   
     wait_us(1); // Data hold time
     
     _setEnable(true);        
-    _setData(value >> 0);    
+    _setData(value >> 0);   // Low nibble
     wait_us(1); // Data setup time        
     _setEnable(false);    
     wait_us(1); // Datahold time
@@ -587,7 +523,7 @@
 void TextLCD::_writeCommand(int command) {
 
     _setRS(false);        
-    wait_us(1);  // Data setup time            
+    wait_us(1);  // Data setup time for RS       
     
     _writeByte(command);   
     wait_us(40); // most instructions take 40us            
@@ -596,7 +532,7 @@
 void TextLCD::_writeData(int data) {
 
     _setRS(true);            
-    wait_us(1);  // Data setup time        
+    wait_us(1);  // Data setup time for RS 
         
     _writeByte(data);
     wait_us(40); // data writes take 40us                
@@ -604,7 +540,7 @@
 
 
 #if (0)
-// This is the original method.
+// This is the original _address() method.
 // It is confusing since it returns the memoryaddress or-ed with the set memorycommand 0x80.
 // Left it in here for compatibility with older code. New applications should use getAddress() instead.
 // 
@@ -632,7 +568,7 @@
 #endif
 
 
-// This replaces the original method.
+// This replaces the original _address() method.
 // Left it in here for compatibility with older code. New applications should use getAddress() instead.
 int TextLCD::_address(int column, int row) {
   return 0x80 | getAddress(column, row);
@@ -714,7 +650,6 @@
         case LCD40x2:                
             return 0x00 + (row * 0x40) + column;
 
-#if(LCD40x4Test)
         case LCD40x4:                
           // LCD40x4 is a special case since it has 2 controllers
           // Each controller is configured as 40x2
@@ -749,8 +684,6 @@
                                    
             return 0x00 + ((row-2) * 0x40) + column;          
           } 
-          
-#endif
             
 // Should never get here.
         default:            
@@ -813,10 +746,7 @@
             return 24;        
 
         case LCD40x2:
-
-#if(LCD40x4Test)
         case LCD40x4:
-#endif        
             return 40;        
         
 // Should never get here.
@@ -844,9 +774,7 @@
         case LCD16x4:
         case LCD20x4:
         case LCD24x4:        
-#if(LCD40x4Test)
         case LCD40x4:
-#endif        
             return 4;
 
 // Should never get here.      
@@ -856,9 +784,6 @@
 }
 
 
-
-#if(LCD40x4Test)
-
 void TextLCD::setCursor(TextLCD::LCDCursor show) { 
 
   // Save new cursor mode, needed when 2 controllers are in use
@@ -888,42 +813,11 @@
 // Should never get here.
       default : 
                            break;
-                      
+                     
     }
-
 }
 
-#else
-//standard
-void TextLCD::setCursor(TextLCD::LCDCursor show) { 
-    
-    switch (show) {
-      case CurOff_BlkOff : _writeCommand(0x0C); // Cursor off and Blink Off
-                           break;
 
-      case CurOn_BlkOff  : _writeCommand(0x0E); // Cursor on and Blink Off
-                           break;
-
-      case CurOff_BlkOn :  _writeCommand(0x0D); // Cursor off and Blink On
-                           break;
-
-      case CurOn_BlkOn   : _writeCommand(0x0F); // Cursor on and Blink char
-                           break;
-
-// Should never get here.
-      default : 
-                           break;
-                      
-    }
-
-}
-
-#endif
-
-
-
-
-#if(LCD40x4Test)
 void TextLCD::setUDC(unsigned char c, char *udc_data) {
   
   // Select and configure second LCD controller when needed
@@ -967,22 +861,3 @@
   _writeCommand(0x80 | addr);
   
 }
-
-#else
-//standard
-void TextLCD::setUDC(unsigned char c, char *udc_data) {
-  // Select CG RAM for current LCD controller
-  _writeCommand(0x40 + ((c & 0x07) << 3)); //Set CG-RAM address
-                                           //8 sequential locations needed per UDC
-  // Store UDC pattern 
-  for (int i=0; i<8; i++) {
-    _writeData(*udc_data++);
-  }
-  
-  //Select DD RAM again for current LCD controller
-  addr = getAddress(_column, _row);
-  _writeCommand(0x80 | addr);
-  
-}
-
-#endif