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:
25:6162b31128c9
Parent:
24:fb3399713710
Child:
26:bd897a001012
--- a/TextLCD.cpp	Sat May 10 13:50:25 2014 +0000
+++ b/TextLCD.cpp	Sat May 10 21:34:51 2014 +0000
@@ -8,6 +8,7 @@
  *               2013, v06: WH, Added support for devices that use internal DC/DC converters 
  *               2013, v07: WH, Added support for backlight and include portdefinitions for LCD2004 Module from DFROBOT 
  *               2014, v08: WH, Refactored in Base and Derived Classes to deal with mbed lib change regarding 'NC' defined pins 
+ *               2014, v09: WH/EO, Added Class for Native SPI controllers such as ST7032 
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -90,8 +91,7 @@
     wait_us(40);         // most instructions take 40us
 
     // Display is now in 4-bit mode
-
-    
+   
     // Device specific initialisations for DC/DC converter to generate VLCD or VLED
     switch (_ctrl) {
       case ST7036:
@@ -108,37 +108,36 @@
           _writeByte( 0x78 );    // Set Contrast C3, C2, C1, C0
           wait_ms(30);           // > 26,3ms
           _writeByte( 0x28 );    // Return to Instruction table 0
-          wait_ms(50);
-        
+          wait_ms(50);      
           break;
+          
       case ST7032:
-          _writeByte( 0x1c );   //Internal OSC frequency adjustment 183HZ    bias will be 1/4 
+          _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 turn on.    /ICON display off. /Contrast control   high byte
+          _writeByte( 0x57 );    //booster circuit is turn 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
           wait_us(30);
           break;
+          
       case WS0010:         
           // WS0010 OLED controller: Initialise DC/DC Voltage converter for LEDs
           // Note: supports 1 or 2 lines (and 16x100 graphics)
           //       supports 4 fonts (English/Japanese (default), Western European-I, English/Russian, Western European-II)
-
                            // Cursor/Disp shift set 0001 SC RL  0 0
                            //
                            // Mode en Power set     0001 GC PWR 1 1                           
                            //  GC  = 0 (Graph Mode=1, Char Mode=0)             
                            //  PWR =   (DC/DC On/Off)
     
-          //_writeCommand(0x13);   // DC/DC off            
-    
+          //_writeCommand(0x13);   // DC/DC off              
           _writeCommand(0x17);   // DC/DC on
+          
           wait_ms(10);
-          
           break;
         
         default:
@@ -1114,24 +1113,25 @@
   }
 }
 
-
 //---------- End TextLCD_SPI ------------
 
 
-//--------- Start TextLCD_NativeSPI -----------
+//--------- Start TextLCD_SPI_N ---------
 
- /** Create a TextLCD interface using an SPI 74595 portexpander
+ /** Create a TextLCD interface using a controller with a native SPI interface
    *
    * @param spi             SPI Bus
    * @param cs              chip select pin (active low)
+   * @param rs              Instruction/data control line
    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
-   * @param ctrl            LCD controller (default = HD44780)      
-   */
-TextLCD_NativeSPI::TextLCD_NativeSPI(SPI *spi, PinName cs, PinName rs, LCDType type, PinName bl, LCDCtrl ctrl) :
-                         TextLCD_Base(type, ctrl), 
-                         _spi(spi),        
-                         _cs(cs),
-                         _rs(rs) {      
+   * @param bl              Backlight control line (optional, default = NC)  
+   * @param ctrl            LCD controller (default = ST7032) 
+   */       
+TextLCD_SPI_N::TextLCD_SPI_N(SPI *spi, PinName cs, PinName rs, LCDType type, PinName bl, LCDCtrl ctrl) :
+                             TextLCD_Base(type, ctrl), 
+                             _spi(spi),        
+                             _cs(cs),
+                             _rs(rs) {      
         
   // Setup the spi for 8 bit data, low steady state clock,
   // rising edge capture, with a 500KHz or 1MHz clock rate  
@@ -1153,28 +1153,28 @@
   _init();
 }
 
-TextLCD_NativeSPI::~TextLCD_NativeSPI() {
+TextLCD_SPI_N::~TextLCD_SPI_N() {
    if (_bl != NULL) {delete _bl;}  // BL pin
 }
 
 // Not used in this mode
-void TextLCD_NativeSPI::_setEnable(bool value) {
+void TextLCD_SPI_N::_setEnable(bool value) {
 }    
 
 // Set RS pin
 // Used for mbed pins, I2C bus expander or SPI shiftregister
-void TextLCD_NativeSPI::_setRS(bool value) {
+void TextLCD_SPI_N::_setRS(bool value) {
     _rs = value;
 }    
 
 // Set BL pin
-void TextLCD_NativeSPI::_setBL(bool value) {
+void TextLCD_SPI_N::_setBL(bool value) {
     if (_bl)
         _bl->write(value);   
 }    
 
 // Write a byte using SPI
-void TextLCD_NativeSPI::_writeByte(int value) {
+void TextLCD_SPI_N::_writeByte(int value) {
     _cs = 0;
     wait_us(1);
     _spi->write(value);
@@ -1182,13 +1182,12 @@
     _cs = 1;
 }
     
-
 // Not used in this mode
-void TextLCD_NativeSPI::_setData(int value) {
+void TextLCD_SPI_N::_setData(int value) {
 }    
 
 
-//---------- End TextLCD_NativeSPI ------------
+//-------- End TextLCD_SPI_N ------------