You are viewing an older revision! See the latest version

Text LCD Enhanced

This page describes an enhanced forked version of the basic driver code library for Text LCD panels using the 4-bit HD44780 lcd display driver.

New features

  • Support for more LCD types
    • TextLCD::LCD8x1 8x1 LCD panel
    • TextLCD::LCD8x2 8x2 LCD panel
    • TextLCD::LCD16x1 16x1 LCD panel (actually 8x2 in different layout)
    • TextLCD::LCD16x4 16x4 LCD panel
    • TextLCD::LCD24x2 24x2 LCD panel
    • TextLCD::LCD24x4 24x4 LCD panel (for KS0078 controller)
    • TextLCD::LCD40x2 40x2 LCD panel
  • Support for Cursor and/or Blinking current character
  • Support for User Defined Characters to display special symbols
  • Improved methods to get and set the memory address of the current character
  • Support for I2C PCF8574 Portexpander or SPI as alternate interface to save on mbed pins

Background

The LCD driver HD44780 is designed for 2 rows of max 40 chars. Note that it needs some support drivers to control that maximum number of chars.

The address for the first char on the first line is 0x00. The address for the first char on the second line is 0x40. Four line displays are generally created by splitting the two line display and arranging the two parts above eachother. For example 2x40 is split in 2 parts of 2x20 and arranged as 4x20. The adddresses for the 3rd and 4th line just continue where the split was made:

The address for the first char on the first line is 0x00, the last char is 0x13.

The address for the first char on the second line is 0x40, the last char is 0x53.

The address for the first char on the third line is 0x14, the last char is 0x27.

The address for the first char on the fourth line is 0x54, the last char is 0x67.

The original TextLCD lib uses the address(column,row) method to return the memory address. However, the method adds 0x80 to the returned addressvalue to include the 'set display datamemory address' command. This is rather confusing when you try to make sense of the actual address. See discussion here.

The enhanced library provides the getAddress(column,row) method to return the correct address. The original method is still provided for compatibility reasons.

Notes: The HD44780 is pretty flexible and variations of driverhardware and LCD glass layout may result in differences. Some 4 line displays actually use two controllers with separate enable pins. The HD44780 supports left/right shifting of memory locations (address) wrt displayposition. That changes the address of the char that is shown on a displaylocation.

LCD wiring

The new lib supports several ways of connecting with mbed:

  • Direct connection with mbed pins using a parallel bus
  • Serial connection using mbed I2C bus and a PCF8574 portexpander
  • Serial connection using mbed SPI bus and a 74595 shiftregister (Coming soon..)

Parallel bus wiring

TextLCD pinsmbed pins
p1 GNDp1 0V
p2 VCCp40 5V Vout
p3 VOvia wiper of 10k pot
p4 RSp15
p5 RWp1 0V
p6 Ep16
p7 D0not connected
p8 D1not connected
p9 D2not connected
p10 D3not connected
p11 D4p17
p12 D5p18
p13 D6p19
p14 D7p20

The mbed pins can be supplied in the LCDText constructor and may be changed. Some LCD displays will work OK with a 3V3 powersupply voltage. You may have to provide a small negative Vo in that case to get good contrast.

Serial I2C bus wiring

The advantage of using the I2C bus is that you save on mbed pins. This is even more true when you use the bus anyhow for other purposes. The small disadvantage that the I2C may be slower than a parallel bus is not a big problem for a slow device like a text display.

TextLCD pinsPCF8574 pinsmbed pins
p1 GNDp8 0Vp1 0V
p2 VCCp16 Vccp40 5V Vout
p3 VOvia wiper of 10k potnot connected
p4 RSp9 P4not connected
p5 RWp8 0Vp1 0V
p6 Ep10 P5not connected
p7 D0not connectednot connected
p8 D1not connectednot connected
p9 D2not connectednot connected
p10 D3not connectednot connected
p11 D4p4 P0not connected
p12 D5p5 P1not connected
p13 D6p6 P2not connected
p14 D7p7 P3not connected

The mbed I2C bus can be supplied in the LCDText constructor and may be changed (p9,p10) or (p28,p27). Note that you must provide pullup Rs of about 4K7. PCF8574 addresspins (p1,p2,p3) must be strapped to GND or Vcc to set the deviceAddress. That address must also be supplied in the LCDText constructor. The PCF8574 supports 8 different addresses (0x40..0x4E). The PCF8574A is similar but has a different baseaddress (0x70..0x7E). So in total you can connect upto 16 LCDs to one I2C bus!

  • Note1: Several other vendors supply clones of the PCF8574 (eg Texas Instruments)
  • Note2: You can change the connections between the PCF8574 pins and the LCD. Just update the appropriate pin definitions in LCDText.h
  • Note3: You can roll-your-own PCF8574 interface or try to find one ready made on ebay. Search for TWI or I2C and LCD. You may have to change the pin definitions.

Example of ready made I2C expander for LCDs /media/uploads/wim/_scaled_image2.jpg

Serial SPIbus wiring

Coming soon..

Text LCD Enhanced Library

Import library

Public Types

enum LCDType {
LCD8x1 , LCD8x2 , LCD16x1 , LCD16x2 ,
LCD16x2B , LCD16x4 , LCD20x2 , LCD20x4 ,
LCD24x2 , LCD24x4 , LCD40x2
}

LCD panel format.

More...
enum LCDCursor { CurOff_BlkOff , CurOn_BlkOff , CurOff_BlkOn , CurOn_BlkOn }

LCD Cursor control.

More...

Public Member Functions

TextLCD (PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type=LCD16x2)
Create a TextLCD interface for using regural mbed pins.
TextLCD (I2C *i2c, char deviceAddress, LCDType type=LCD16x2)
Create a TextLCD interface using an I2C PC8574 portexpander.
int putc (int c)
Create a TextLCD interface using an SPI 74595 portexpander.
int printf (const char *format,...)
Write a formated string to the LCD.
void locate (int column, int row)
Locate to a screen column and row.
int getAddress (int column, int row)
Return the memoryaddress of screen column and row location.
void setAddress (int column, int row)
Set the memoryaddress of screen column and row location.
void cls ()
Clear the screen and locate to 0,0.
int rows ()
Return the number of rows.
int columns ()
Return the number of columns.
void setCursor ( LCDCursor show)
Set the Cursormode.
void setUDC (unsigned char c, char *udc_data)
Set User Defined Characters.

Protected Types

enum _LCDBus { _PinBus , _I2CBus , _SPIBus }

LCD Bus control.

More...

Protected Member Functions

void _init ()
Init the LCD controller 4-bit mode, number of lines, no cursor etc Clear display.

Example Code

// 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

//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x4); // rs, e, d4-d7 ok
//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x2); // rs, e, d4-d7 ok
TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7 ok
//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD24x4); // rs, e, d4-d7 ok
//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD24x2); // rs, e, d4-d7 ok
//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD40x2); // rs, e, d4-d7 ok

//I2C interface
//TextLCD lcd(&i2c_lcd, 0x42, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type 


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);    
    lcd.setUDC(2, (char *) udc_2);
    lcd.putc(2);    
    lcd.setUDC(3, (char *) udc_3);
    lcd.putc(3);    
    lcd.setUDC(4, (char *) udc_4);
    lcd.putc(4);    
    lcd.setUDC(5, (char *) udc_5);
    lcd.putc(5);    
    lcd.setUDC(6, (char *) udc_6);
    lcd.putc(6);    
    lcd.setUDC(7, (char *) udc_7);
    lcd.putc(7);  
 
}

The terminal will print the actual memory addresses like this:

LCD Test. Columns=20, Rows=4

MemAddr(Col=0, Row=0)=0x00

MemAddr(Col=19, Row=0)=0x13

MemAddr(Col=0, Row=1)=0x40

MemAddr(Col=19, Row=1)=0x53

MemAddr(Col=0, Row=2)=0x14

MemAddr(Col=19, Row=2)=0x27

MemAddr(Col=0, Row=3)=0x54

MemAddr(Col=19, Row=3)=0x67

Examples

Some results are shown here:

LCD20x2

/media/uploads/wim/_scaled_img_2636.jpg

LCD24x2

/media/uploads/wim/_scaled_img_2638.jpg

LCD40x2

/media/uploads/wim/_scaled_img_2637.jpg

LCD16x4

/media/uploads/wim/_scaled_img_2640.jpg

LCD20x4

/media/uploads/wim/_scaled_img_2642.jpg

LCD24x4

/media/uploads/wim/_scaled_img_2646.jpg

User Defined Characters

/media/uploads/wim/_scaled_img_2645_-_copy.jpg

References


All wikipages