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:
40:d3496c3ea301
Parent:
39:e9c2319de9c5
Child:
41:111ca62e8a59
--- a/TextLCD.cpp	Tue May 19 18:13:00 2015 +0000
+++ b/TextLCD.cpp	Wed May 20 18:46:51 2015 +0000
@@ -21,7 +21,7 @@
  *               2015, v18: WH, Performance improvement I2C portexpander
  *               2015, v19: WH, Fixed Adafruit I2C/SPI portexpander pinmappings, fixed SYDZ Backlight 
  *               2015, v20: WH, Fixed occasional Init fail caused by insufficient wait time after ReturnHome command (0x02), Added defines to reduce memory footprint (eg LCD_ICON),
- *                              Fixed and Added more fonttable support for PCF2119K, Added HD66712 controller.
+ *                              Fixed and Added more fonttable support for PCF2119R_3V3, Added HD66712 controller.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -1225,49 +1225,46 @@
             case LCD20x1:
             case LCD24x1:
 //            case LCD32x1:        // EXT pin is High, extension driver needed
-              _function  = 0x02;    // Function set 001 DL N RE(0) DH REV (Std Regs)
+              _function  = 0x02;    // Function set 001 DL N RE(0) - - (Std Regs)
                                     //   DL=0  (4 bits bus)             
                                     //    N=0  (1-line mode, N=1 2-line mode)
-                                    //   RE=0  (Dis. Extended Regs, special mode for KS0073)
-                                    //   DH=1  (Disp shift enable, special mode for KS0073)                                
-                                    //   REV=0 (Reverse normal, special mode for KS0073)
+                                    //   RE=0  (Dis. Extended Regs, special mode for HD66712)
+                                    //   
                                     
               _function_1 = 0x04;   // Function set 001 DL N RE(1) BE LP (Ext Regs)
                                     //   DL=0  (4 bits bus)             
                                     //    N=0  (1-line mode, N=1 2-line mode)
-                                    //   RE=1  (Ena Extended Regs, special mode for KS0073)
-                                    //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0073)                                
-                                    //   LP=0  (LP=1 Low power mode, LP=0 Normal)
+                                    //   RE=1  (Ena Extended Regs; special mode for HD66712)
+                                    //   BE=0  (Blink Enable, CG/SEG RAM; special mode for HD66712)                                
+                                    //   LP=0  (LP=1 Low power mode, LP=0 Normal; special mode for HD66712)
 
               _function_x = 0x00;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
-                                    //    NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0073)                                
+                                    //    NW=0  (1,2 line), NW=1 (4 Line, special mode for HD66712)                                
               break;                                
 
-//            case LCD12x3D:         // Special mode for KS0073, KS0078 and PCF21XX            
-//            case LCD12x3D1:        // Special mode for KS0073, KS0078 and PCF21XX            
+//            case LCD12x3D:         // Special mode for KS0073, KS0078, PCF21XX and HD66712
+//            case LCD12x3D1:        // Special mode for KS0073, KS0078, PCF21XX and HD66712
             case LCD12x4D:         // Special mode for KS0073, KS0078, PCF21XX and HD66712
-//            case LCD16x3D:         // Special mode for KS0073, KS0078             
-//            case LCD16x4D:         // Special mode for KS0073, KS0078            
+//            case LCD16x3D:         // Special mode for KS0073, KS0078 and HD66712
+//            case LCD16x4D:         // Special mode for KS0073, KS0078 and HD66712            
             case LCD20x4D:         // Special mode for KS0073, KS0078 and HD66712            
-              _function  = 0x02;    // Function set 001 DL N RE(0) DH REV (Std Regs)
+              _function  = 0x02;    // Function set 001 DL N RE(0) - - (Std Regs)
                                     //   DL=0  (4 bits bus)             
-                                    //    N=0  (dont care for 4 line mode)              
-                                    //   RE=0  (Dis. Extended Regs, special mode for KS0073)
-                                    //   DH=1  (Disp shift enable, special mode for KS0073)                                
-                                    //   REV=0 (Reverse normal, special mode for KS0073)
+                                    //    N=0  (1-line mode, N=1 2-line mode)
+                                    //   RE=0  (Dis. Extended Regs, special mode for HD66712)
+                                    //   
                                     
               _function_1 = 0x04;   // Function set 001 DL N RE(1) BE LP (Ext Regs)
                                     //   DL=0  (4 bits bus)             
-                                    //    N=0  (1-line mode), N=1 (2-line mode)
-                                    //   RE=1  (Ena Extended Regs, special mode for KS0073)
-                                    //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0073)                                
-                                    //   LP=0  (LP=1 Low power mode, LP=0 Normal)                                    
+                                    //    N=0  (1-line mode, N=1 2-line mode)
+                                    //   RE=1  (Ena Extended Regs; special mode for HD66712)
+                                    //   BE=0  (Blink Enable, CG/SEG RAM; special mode for HD66712)                                
+                                    //   LP=0  (LP=1 Low power mode, LP=0 Normal; special mode for HD66712)
 
               _function_x = 0x01;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
-                                    //    NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0073)                                
+                                    //    NW=0  (1,2 line), NW=1 (4 Line, special mode for HD66712)                                
               break;                                
 
-
             case LCD16x3G:            // Special mode for ST7036                        
 //            case LCD24x3D:         // Special mode for KS0078
 //            case LCD24x3D1:        // Special mode for KS0078
@@ -1277,22 +1274,20 @@
 
             default:
               // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)            
-              _function  = 0x0A;    // Function set 001 DL N RE(0) DH REV (Std Regs)
+              _function  = 0x0A;    // Function set 001 DL N RE(0) - - (Std Regs)
                                     //   DL=0  (4 bits bus)             
                                     //    N=1  (2-line mode), N=0 (1-line mode)
-                                    //   RE=0  (Dis. Extended Regs, special mode for KS0073)
-                                    //   DH=1  (Disp shift enable, special mode for KS0073)                                
-                                    //   REV=0 (Reverse normal, special mode for KS0073)
+                                    //   RE=0  (Dis. Extended Regs, special mode for HD66712)
                                     
               _function_1 = 0x0C;   // Function set 001 DL N RE(1) BE LP (Ext Regs)
                                     //   DL=0  (4 bits bus)             
                                     //    N=1  (2 line mode), N=0 (1-line mode)
-                                    //   RE=1  (Ena Extended Regs, special mode for KS0073)
-                                    //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0073)                   
-                                    //   LP=0  (LP=1 Low power mode, LP=0 Normal)                                                                                     
+                                    //   RE=1  (Ena Extended Regs, special mode for HD66712)
+                                    //   BE=0  (Blink Enable, CG/SEG RAM, special mode for HD66712)
+                                    //   LP=0  (LP=1 Low power mode, LP=0 Normal)
 
               _function_x = 0x00;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
-                                    //   NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0073)                                
+                                    //   NW=0  (1,2 line), NW=1 (4 Line, special mode for HD66712)
               break;
           } // switch type
 
@@ -1300,27 +1295,27 @@
           _writeCommand(0x20 | _function_1);// Function set 001 DL N RE(1) BE LP (Ext Regs)
                                            //   DL=0 (4 bits bus), DL=1 (8 bits mode)            
                                            //    N=0 (1 line mode), N=1 (2 line mode)
-                                           //   RE=1 (Ena Extended Regs, special mode for KS0073)
-                                           //   BE=0 (Blink Enable/Disable, CG/SEG RAM, special mode for KS0073)                                
+                                           //   RE=1 (Ena Extended Regs, special mode for HD66712)
+                                           //   BE=0 (Blink Enable/Disable, CG/SEG RAM, special mode for HD66712)                                
                                            //   LP=0  (LP=1 Low power mode, LP=0 Normal)                                                                                                                                
 
           _writeCommand(0x08 | _function_x); // Ext Function set 0000 1 FW BW NW (Ext Regs)
-                                           //   FW=0  (5-dot font, special mode for KS0073)
-                                           //   BW=0  (Cur BW invert disable, special mode for KS0073)
-                                           //   NW=0  (1,2 Line), NW=1 (4 line, special mode for KS0073)                                
-
-          _writeCommand(0x10);             // Scroll/Shift set 0001 DS/HS4 DS/HS3 DS/HS2 DS/HS1 (Ext Regs)
-                                           //   Dotscroll/Display shift enable (Special mode for KS0073)
-
-          _writeCommand(0x80);             // Scroll Quantity set 1 0 SQ5 SQ4 SQ3 SQ2 SQ1 SQ0 (Ext Regs)
-                                           //   Scroll quantity (Special mode for KS0073)
+                                           //   FW=0  (5-dot font, special mode for HD66712)
+                                           //   BW=0  (Cur BW invert disable, special mode for HD66712)
+                                           //   NW=0  (1,2 Line), NW=1 (4 line, special mode for HD66712)                                
+
+          _writeCommand(0x10);             // Scroll/Shift set 0001 HS4 HS3 HS2 HS1 (Ext Regs)
+                                           //   Dotscroll/Display shift enable (Special mode for HD66712)
+
+          _writeCommand(0x80);             // Scroll Quantity set 1 0 HDS5 HDS4 HDS3 HDS2 HDS1 HDS0 (Ext Regs)
+                                           //   Scroll quantity (Special mode for HD66712)
 
           _writeCommand(0x20 | _function); // Function set 001 DL N RE(0) DH REV (Std Regs)
                                            //   DL=0  (4 bits bus), DL=1 (8 bits mode)             
                                            //    N=0  (1 line mode), N=1 (2 line mode)
-                                           //   RE=0  (Dis. Extended Regs, special mode for KS0073)
-                                           //   DH=1  (Disp shift enable/disable, special mode for KS0073)                                
-                                           //   REV=0 (Reverse/Normal, special mode for KS0073)
+                                           //   RE=0  (Dis. Extended Regs, special mode for HD66712)
+                                           //   DH=1  (Disp shift enable/disable, special mode for HD66712)
+                                           //   REV=0 (Reverse/Normal, special mode for HD66712)
           break; // case HD66712 Controller
 
            
@@ -1466,7 +1461,7 @@
     }
     else {
       //Character to write
-#if (LCD_DEFAULT_FONT == 1)      
+#if (LCD_DEF_FONT == 1)      
       _writeData(value);
 #else
       _writeData(ASCII_2_LCD(value));
@@ -1978,7 +1973,8 @@
     case PCF2113_3V3 : // Some UDCs may be used for Icons                      
     case PCF2116_3V3 :          
     case PCF2116_5V  :              
-    case PCF2119_3V3 : // Some UDCs may be used for Icons             
+    case PCF2119_3V3 : // Some UDCs may be used for Icons
+    case PCF2119R_3V3: // Some UDCs may be used for Icons                 
       c = c & 0x0F; // mask down to valid range
       break;
 
@@ -2036,13 +2032,14 @@
     case BlinkOn: 
       // Controllers that support UDC/Icon Blink  
       switch (_ctrl) {
-        case KS0073 :            
-        case KS0078 :            
+        case KS0073 :
+        case KS0078 :
+        case HD66712 :                    
           _function_1 |= 0x02; // Enable UDC/Icon Blink        
           _writeCommand(0x20 | _function_1);        // Function set 0 0 1 DL N RE(1) BE 0/LP (Ext Regs)
 
           _writeCommand(0x20 | _function);          // Function set 0 0 1 DL N RE(0) DH REV (Std Regs)
-          break; // case KS0073, KS0078 Controller
+          break; // case KS0073, KS0078, HD66712 Controller
     
         case US2066_3V3 :  
         case SSD1803_3V3 :  
@@ -2075,13 +2072,14 @@
     case BlinkOff:
       // Controllers that support UDC Blink  
       switch (_ctrl) {
-        case KS0073 :            
-        case KS0078 :            
+        case KS0073 :
+        case KS0078 :
+        case HD66712:
           _function_1 &= ~0x02; // Disable UDC/Icon Blink        
           _writeCommand(0x20 | _function_1);        // Function set 0 0 1 DL N RE(1) BE 0/LP (Ext Regs)
 
           _writeCommand(0x20 | _function);          // Function set 0 0 1 DL N RE(0) DH REV (Std Regs)
-          break; // case KS0073, KS0078 Controller
+          break; // case KS0073, KS0078, HD66712 Controller
     
         case US2066_3V3 :  
         case SSD1803_3V3 :