Tutorial 9
Character LCD

Character LCD usage

Character LCD based on HD44780-compartible controller is supported by external device library, so it's simple to work with it. MCU Java source works with LCDs with any number of characters arranged in 1, 2, 3 or 4 rows. Rows 3 and 4 are treated as continuation of rows 1 and 2.

Only write operations are performed, R/W pin of LCD is connected to GND.

LCD could be connected to MCU in following ways:

  1. 4-bit mode, data pins connected to one port sequentially. Sequentially means, if the DB4 is connected to bit n, DB5 must be connected to bit n+1 (next bit), DB6 to n+2, DB7 to n+3 (bit n can be from 0 to 4)
  2. 8-bit mode, data pins connected to one port (DB0 to bit 0, DB1 to bit 1 and go on).
  3. 4-bit mode, data pins connected to arbitrary port lines
  4. 8-bit mode, data pins connected to arbitrary port lines
  5. 8-bit mode, using external memory interface (though external memory interface is not supported yet)

In every mode, except external memory mode, pins RS and E of LCD could be connected to any port line of MCU. In following tutorial board character LCD is connected using mode 1 (4-bit, data pins connected to one port sequentially: DB4 to PD4, DB5 to PD5, DB6 to PD6, DB7 to PD7).


Schematic of board with character LCD

Download schematic of board with character LCD in TinyCad format (.dsn)

Test program

This simple test program shows "Hello, LCD!" text in the first line and "This is line 2" in the second. First string is loaded from Flash memory, and second is stored in RAM. Note that rows and character numbers (as arguments of setPosition method) are counted starting from 0

import mcujavasource.mcu.*;
import mcujavasource.mcu.external.lcd.CharacterLcd;

/** Tutorial 9: Character LCD.
 */

public class Main extends Microcontroller
{
  public static final String MESSAGE = "Hello, LCD!";
  
  private CharacterLcd lcd;
  private Port lcdPort = getHardware().getPort("D");
  private Port lcdControlPort = getHardware().getPort("C");
  private Pin rsPin = lcdControlPort.getPin(5);
  private Pin ePin = lcdControlPort.getPin(3);
  
  
  public void init()
  { getHardware().setAllPortsDirection(Pin.IN);
    getHardware().setAllPortsPullUp(true);
    lcd = new CharacterLcd(16, 2, rsPin, ePin, lcdPort, 4);
  }
  
  public void start()
  { lcd.init();
    lcd.setPosition(0, 0);
    lcd.write(MESSAGE);
    lcd.setPosition(1, 0);
    lcd.write("This is line 2");
    getHardware().setInterruptsEnabled(true);
  }
  
  
}

Download java source file