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:
31:ef31cd8a00d1
Parent:
30:033048611c01
Child:
32:59c4b8f648d4
--- a/TextLCD.cpp	Sat Jun 28 14:27:32 2014 +0000
+++ b/TextLCD.cpp	Sun Jun 29 14:55:50 2014 +0000
@@ -812,191 +812,6 @@
   return 0x80 | getAddress(column, row);
 }
 
-#if(0)
-// This is new method to return the memory address based on row, column and displaytype.
-//
-/** Return the memoryaddress of screen column and row location
-   *
-   * @param column  The horizontal position from the left, indexed from 0
-   * @param row     The vertical position from the top, indexed from 0
-   * @param return  The memoryaddress of screen column and row location
-   *
-   *  Note: some configurations are commented out because they have not yet been tested due to lack of hardware   
-   */
-int TextLCD_Base::getAddress(int column, int row) {
-
-    switch (_type) {
-        case LCD8x1:
-//        case LCD12x1:        
-//        case LCD16x1B:        
-//        case LCD20x1:        
-        case LCD24x1:
-//        case LCD40x1:                        
-            return 0x00 + column;                        
-
-        case LCD16x1:
-            // LCD16x1 is a special layout of LCD8x2
-            if (column<8) 
-              return 0x00 + column;                        
-            else   
-              return 0x40 + (column - 8);                        
-
-        case LCD8x2D:
-            // LCD8x2B is a special layout of LCD16x1
-            if (row==0) 
-              return 0x00 + column;                        
-            else   
-              return 0x08 + column;                        
-
-        case LCD8x2:               
-        case LCD12x2:                
-        case LCD16x2:
-        case LCD20x2:
-        case LCD24x2:        
-        case LCD40x2:                
-            return 0x00 + (row * 0x40) + column;
-
-// Not sure about this one, seems wrong.
-// Left in for compatibility with original library
-        case LCD16x2B:      
-            return 0x00 + (row * 40) + column;
-    
-
-// Special mode for ST7036 
-//        case LCD16x3:
-
-// Special mode for PCF2116 
-        case LCD12x3B:
-            //Display bottom three rows of four
-            switch (row) {
-                case 0:
-                    return 0x20 + column;
-                case 1:
-                    return 0x40 + column;
-                case 2:
-                    return 0x60 + column;
-            }
-
-#if(0)
-        case LCD12x3C:
-            //Display top three rows of four        
-            switch (row) {
-                case 0:
-                    return 0x00 + column;
-                case 1:
-                    return 0x20 + column;
-                case 2:
-                    return 0x40 + column;
-            }
-#endif
-
-        case LCD12x4:
-            switch (row) {
-                case 0:
-                    return 0x00 + column;
-                case 1:
-                    return 0x40 + column;
-                case 2:
-                    return 0x0C + column;
-                case 3:
-                    return 0x4C + column;
-            }
-
-// Special mode for PCF2116 (and KS0078) 
-        case LCD12x4B:
-            switch (row) {
-                case 0:
-                    return 0x00 + column;
-                case 1:
-                    return 0x20 + column;
-                case 2:
-                    return 0x40 + column;
-                case 3:
-                    return 0x60 + column;                   
-            }
-
-        case LCD16x4:
-            switch (row) {
-                case 0:
-                    return 0x00 + column;
-                case 1:
-                    return 0x40 + column;
-                case 2:
-                    return 0x10 + column;
-                case 3:
-                    return 0x50 + column;
-            }
-
-        case LCD20x4:
-            switch (row) {
-                case 0:
-                    return 0x00 + column;
-                case 1:
-                    return 0x40 + column;
-                case 2:
-                    return 0x14 + column;
-                case 3:
-                    return 0x54 + column;
-            }
-
-// Special mode for KS0078
-        case LCD24x4B:
-            switch (row) {
-                case 0:
-                    return 0x00 + column;
-                case 1:
-                    return 0x20 + column;
-                case 2:
-                    return 0x40 + column;
-                case 3:
-                    return 0x60 + column;
-            }
-
-        case LCD40x4:                
-          // LCD40x4 is a special case since it has 2 controllers
-          // Each controller is configured as 40x2
-          if (row<2) { 
-            // Test to see if we need to switch between controllers  
-            if (_ctrl_idx != _LCDCtrl_0) {
-
-              // Second LCD controller Cursor Off
-              _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);    
-
-              // Select primary controller
-              _ctrl_idx = _LCDCtrl_0;
-
-              // Restore cursormode on primary LCD controller
-              _setCursorAndDisplayMode(_currentMode, _currentCursor);    
-            }           
-            
-            return 0x00 + (row * 0x40) + column;          
-          }
-          else {
-
-            // Test to see if we need to switch between controllers  
-            if (_ctrl_idx != _LCDCtrl_1) {
-              // Primary LCD controller Cursor Off
-              _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);    
-
-              // Select secondary controller
-              _ctrl_idx = _LCDCtrl_1;
-
-              // Restore cursormode on secondary LCD controller
-              _setCursorAndDisplayMode(_currentMode, _currentCursor);    
-            }           
-                                   
-            return 0x00 + ((row-2) * 0x40) + column;          
-          } 
-            
-// Should never get here.
-        default:            
-            return 0x00;        
-    }
-}
-
-#else
-
-//Test of Addressing Mode encoded in LCDType
 
 // This is new method to return the memory address based on row, column and displaytype.
 //
@@ -1128,10 +943,6 @@
 }
 
 
-#endif
-
-
-
 /** Set the memoryaddress of screen column and row location
   *
   * @param column  The horizontal position from the left, indexed from 0
@@ -1175,50 +986,7 @@
     
   // Columns encoded in b7..b0
   //return (_type & 0xFF);          
-  return _nr_cols;          
-  
-#if(0)    
-    switch (_type) {
-        case LCD8x1:
-        case LCD8x2:
-        case LCD8x2B:                
-            return 8;
-        
-        case LCD12x2:        
-        case LCD12x3B:                
-//        case LCD12x3C:                        
-        case LCD12x4:        
-        case LCD12x4B:                
-            return 12;        
-
-        case LCD16x1:        
-        case LCD16x2:
-        case LCD16x2B:
-//        case LCD16x3:                        
-        case LCD16x4:        
-            return 16;
-            
-//        case LCD20x1:
-        case LCD20x2:
-        case LCD20x4:
-            return 20;
-
-        case LCD24x1:
-        case LCD24x2:
-//        case LCD24x3B:                
-        case LCD24x4B:        
-            return 24;        
-
-//        case LCD40x1:        
-        case LCD40x2:
-        case LCD40x4:
-            return 40;        
-        
-// Should never get here.
-        default:
-            return 0;
-    }
-#endif    
+  return _nr_cols;           
 }
 
 /** Return the number of rows
@@ -1232,45 +1000,6 @@
   // Rows encoded in b15..b8  
   //return ((_type >> 8) & 0xFF); 
   return _nr_rows;          
-    
-#if(0)    
-    switch (_type) {
-        case LCD8x1: 
-        case LCD16x1:         
-//        case LCD20x1:                 
-        case LCD24x1:                 
-//        case LCD40x1:                         
-            return 1;           
-
-        case LCD8x2:  
-        case LCD8x2B:                        
-        case LCD12x2:                      
-        case LCD16x2:
-        case LCD16x2B:
-        case LCD20x2:
-        case LCD24x2:        
-        case LCD40x2:                
-            return 2;
-
-        case LCD12x3B:                
-//        case LCD12x3C:                        
-//        case LCD16x3:                
-//        case LCD24x3B:                
-            return 3;
-                    
-        case LCD12x4:        
-        case LCD12x4B:                
-        case LCD16x4:
-        case LCD20x4:
-        case LCD24x4B:        
-        case LCD40x4:
-            return 4;
-
-// Should never get here.      
-        default:
-            return 0;        
-    }
-#endif    
 }
 
 /** Set the Cursormode
@@ -1283,8 +1012,7 @@
   _currentCursor = cursorMode;
     
   // Configure only current LCD controller
-  _setCursorAndDisplayMode(_currentMode, _currentCursor);
-    
+  _setCursorAndDisplayMode(_currentMode, _currentCursor);    
 }
 
 /** Set the Displaymode
@@ -1748,21 +1476,6 @@
     _bl = NULL;                 //Construct dummy pin     
   }  
   
-#if(0)
-  //Sanity check
-  switch (_ctrl) {
-    case ST7032_3V3:
-    case ST7032_5V:    
-    case PCF21XX_3V3:    
-//    case PCF21XX_5V:        
-      _init();
-      break;
-    
-    default: 
-      error("Error: LCD Controller type does not support native I2C interface\n\r"); 
-  }  
-#endif
-
   //Sanity check
   if (_ctrl & LCD_C_I2C) {
     _init();      
@@ -2013,20 +1726,6 @@
     _bl = NULL;                 //Construct dummy pin     
   }  
 
-#if(0)
-  //Sanity check
-  switch (_ctrl) {
-    case ST7032_3V3:
-    case ST7032_5V:    
-    case WS0010:        
-      _init();
-      break;
-    
-    default: 
-      error("Error: LCD Controller type does not support native SPI4 interface\n\r"); 
-  }     
-#endif
-
   //Sanity check
   if (_ctrl & LCD_C_SPI4) {
     _init();