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:
27:22d5086f6ba6
Parent:
26:bd897a001012
Child:
28:30fa94f7341c
--- a/TextLCD.cpp	Fri May 30 15:05:13 2014 +0000
+++ b/TextLCD.cpp	Fri Jun 13 19:02:26 2014 +0000
@@ -49,28 +49,25 @@
   
   // Select and configure second LCD controller when needed
   if(_type==LCD40x4) {
-    _ctrl_idx=_LCDCtrl_1; // Select 2nd controller
+    _ctrl_idx=_LCDCtrl_1;        // Select 2nd controller
     
-    _initCtrl();                   // Init 2nd controller
+    _initCtrl();                 // Init 2nd controller
     
     // Secondary LCD controller Clearscreen
-    _writeCommand(0x01);       // cls, and set cursor to 0    
+    _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
-    
+                     // Since we are not using the Busy flag, Lets be safe and take 10 ms   
   }
     
   // Select and configure primary LCD controller
-  _ctrl_idx=_LCDCtrl_0; // Select primary controller  
+  _ctrl_idx=_LCDCtrl_0;          // Select primary controller  
 
   _initCtrl();                   // Init primary controller
   
   // Primary LCD controller Clearscreen
-  _writeCommand(0x01);       // cls, and set cursor to 0
-
+  _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
-    
+                   // Since we are not using the Busy flag, Lets be safe and take 10 ms    
 } 
 
 /**  Init the LCD controller
@@ -92,54 +89,68 @@
     wait_us(40);         // most instructions take 40us
 
     // Display is now in 4-bit mode
+    // Note: 4 bit mode is ignored for native SPI and I2C devices
    
     // Device specific initialisations for DC/DC converter to generate VLCD or VLED
     switch (_ctrl) {
       case ST7036:
           // ST7036 controller: Initialise Voltage booster for VLCD. VDD=5V
           // Note: supports 1,2 or 3 lines
-          _writeByte( 0x29 );    // 4-bit Databus, 2 Lines, Select Instruction table 1
+          _writeByte( 0x29 );    // 4-bit Databus, 2 Lines, Select Instruction Set = 1
           wait_ms(30);           // > 26,3ms 
           _writeByte( 0x14 );    // Bias: 1/5, 2-Lines LCD 
           wait_ms(30);           // > 26,3ms
           _writeByte( 0x55 );    // Icon off, Booster on, Set Contrast C5, C4
           wait_ms(30);           // > 26,3ms
-          _writeByte( 0x6d );    // Voltagefollower On, Ampl ratio Rab2, Rab1, Rab0
+          _writeByte( 0x6D );    // Voltagefollower On, Ampl ratio Rab2, Rab1, Rab0
           wait_ms(200);          // > 200ms!
           _writeByte( 0x78 );    // Set Contrast C3, C2, C1, C0
           wait_ms(30);           // > 26,3ms
-          _writeByte( 0x28 );    // Return to Instruction table 0
+          _writeByte( 0x28 );    // Return to Instruction Set = 0
           wait_ms(50);      
           break;
           
       case ST7032_3V3:
           // ST7032 controller: Initialise Voltage booster for VLCD. VDD=3V3
-     
-          _writeByte( 0x1c );    //Internal OSC frequency adjustment 183HZ, bias will be 1/4 
+
+//          _writeByte( 0x39 );    //FUNCTION SET 8 bit,N=1 2-line display mode,5*7dot, Select Instruction Set = 1
+          _writeByte( 0x29 );    //FUNCTION SET 4 bit, N=1 2-line display mode, 5*7dot, Select Instruction Set = 1
+                                 //Note: 4 bit mode is ignored for native SPI and I2C devices
+          wait_us(30);     
+          _writeByte( 0x1C );    //Internal OSC frequency adjustment 183HZ, bias will be 1/4 
           wait_us(30);
           _writeByte( 0x73 );    //Contrast control low byte
           wait_us(30);  
           _writeByte( 0x57 );    //booster circuit is turned on. /ICON display off. /Contrast control high byte
           wait_us(30);
-          _writeByte( 0x6c );    //Follower control
+          _writeByte( 0x6C );    //Follower control
           wait_us(50);   
-          _writeByte( 0x0c );    //DISPLAY ON, not needed
+//          _writeByte( 0x0C );    //DISPLAY ON, not needed
+//          _writeByte( 0x38 );    //FUNCTION SET 8 bit,N=1 2-line display mode,5*7dot, Return to Instruction Set = 0        
+          _writeByte( 0x28 );    //FUNCTION SET 4 bit, N=1 2-line display mode, 5*7dot, Return to Instruction Set = 0                  
+                                 //Note: 4 bit mode is ignored for native SPI and I2C devices                   
           wait_us(30);
           break;
 
 //Check and fix booster disable
       case ST7032_5V:
           // ST7032 controller: Disable Voltage booster for VLCD. VDD=5V      
-          
-          _writeByte( 0x1c );    //Internal OSC frequency adjustment 183HZ, bias will be 1/4 
+//          _writeByte( 0x39 );    //FUNCTION SET 8 bit,N=1 2-line display mode,5*7dot, Instruction Set = 1
+          _writeByte( 0x29 );    //FUNCTION SET 4 bit, N=1 2-line display mode, 5*7dot, Select Instruction Set = 1
+                                 //Note: 4 bit mode is ignored for native SPI and I2C devices         
+          wait_us(30);               
+          _writeByte( 0x1C );    //Internal OSC frequency adjustment 183HZ, bias will be 1/4 
           wait_us(30);
           _writeByte( 0x73 );    //Contrast control low byte
           wait_us(30);  
           _writeByte( 0x57 );    //booster circuit is turned on. /ICON display off. /Contrast control high byte
           wait_us(30);
-          _writeByte( 0x6c );    //Follower control
+          _writeByte( 0x6C );    //Follower control
           wait_us(50);   
-          _writeByte( 0x0c );    //DISPLAY ON  --> remove, done later?
+//          _writeByte( 0x0C );    //DISPLAY ON  --> remove, done later?
+//          _writeByte( 0x38 );    //FUNCTION SET 8 bit,N=1 2-line display mode,5*7dot, Instruction Set = 0                  
+          _writeByte( 0x28 );    //FUNCTION SET 4 bit, N=1 2-line display mode, 5*7dot, Return to Instruction Set = 0                            
+                                 //Note: 4 bit mode is ignored for native SPI and I2C devices                   
           wait_us(30);
           break;
           
@@ -208,7 +219,8 @@
 // All other LCD types are initialised as 2 Line displays (including LCD40x4)
         default:
             _writeCommand(0x28); // Function set 001 DL N F - -
-                                 //  DL=0 (4 bits bus) 
+                                 //  DL=0 (4 bits bus)
+                                 //  Note: 4 bit mode is ignored for native SPI and I2C devices                                 
                                  //   N=1 (2 lines)
                                  //   F=0 (5x7 dots font, only option for 2 line display)
                                  //    -  (Don't care)                                
@@ -240,8 +252,7 @@
     _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
 
     // Second LCD controller Clearscreen
-    _writeCommand(0x01); // cls, and set cursor to 0    
-
+    _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
 
@@ -250,8 +261,7 @@
   }
   
   // Primary LCD controller Clearscreen
-  _writeCommand(0x01); // cls, and set cursor to 0
-
+  _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
 
@@ -893,17 +903,17 @@
 
 #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  
+  _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  
     
   // Init the portexpander bus
   _lcd_bus = D_LCD_BUS_DEF;
@@ -1264,8 +1274,6 @@
     _bl = NULL;                 //Construct dummy pin     
   }  
   
-  _writeByte( 0x39 );  //FUNCTION SET 8 bit,N=1 2-line display mode,5*7dot IS=1
-  wait_us(30);
   _init();
 }