Parts

ST7920 12864 graphic LCD

The blue 128x64 LCD from 3D printer controller kits, on a Sitronix ST7920, with a three-wire serial mode. Aliases: 12864, LCD12864, ST7920, 128x64 graphic LCD.

White dots on blue backlit glass, twenty pins along the top, and a controller that knows both text (a built-in font) and graphics. U8g2 drives it over three wires.

Pins

Pin What it does
GND Ground.
VCC Supply, 5 V on the module (the chip runs from 2.7 V to 5.5 V).
V0 The LCD drive voltage, set by the module's own trimmer. Leave it unwired.
RS Parallel: low for an instruction, high for data. Serial: chip select, active high.
RW (printed R/W) Parallel: low to write, high to read. Serial: the data line, SID.
E Parallel: the strobe. Serial: the clock, SCLK.
DB0 to DB7 The parallel data bus. Unused in serial mode.
PSB High (or unwired) for the parallel bus, low for the serial bus.
NC Not connected.
RST Reset, active low. Unwired, the module's own reset circuit holds it high.
VOUT The voltage the module makes for V0. Leave it unwired.
BLA, BLK The backlight LED's anode and cathode: BLA to 5 V, BLK to ground.

Properties

None.

What the model gets right

Two buses. With PSB high it is a parallel bus: a write is latched on the falling edge of E, a read is on DB0 to DB7 while E is high, and the function set's DL bit picks eight bits or four (high nibble first on DB7 to DB4). Reads work, with the dummy read after every address set, and an instruction read returns the busy flag and the address counter. With PSB low it is the serial bus, write only: SID is sampled on the rising edge of SCLK while CS is high, a transfer starts with five ones then RW, RS and a zero (0xF8 for an instruction, 0xFA for data), and each byte comes as two bytes, the high nibble then the low one, in the top four bits. CS low drops a byte half sent.

Both instruction sets. Basic: clear, home, entry mode, display on and off, cursor move, function set, CGRAM and DDRAM address. Extended: standby, scroll or IRAM select, line reverse, sleep, graphics on (G), the vertical scroll address and the graphic RAM address. As the datasheet says, an instruction that changes RE changes nothing else, which is why U8g2 sends 0x3E twice.

The 256x32 layout. The chip draws 256 dots by 32 rows, and the module folds that in half: rows 0 to 31 of graphic RAM, words 0 to 7, are the top half of the glass, and words 8 to 15 are the bottom half. Set the vertical address, then the horizontal one, then write two bytes a word; the horizontal address steps after each word and wraps from 15 to 0 on the same row.

Text mode. DDRAM lines start at 0x80, 0x90, 0x88 and 0x98 for the four text rows of 16 characters. A two-byte word is two 8x16 half-width characters (codes 0x02 to 0x7F) or one 16x16 character; codes 0x0000 to 0x0006 are the four characters you define in CGRAM. Clear fills DDRAM with spaces. Text and graphics show together once G is set.

What it does not model

The half-width font is Mokxi's own 5x8 font drawn twice as tall in the 8x16 cell, not the ROM's glyphs, and the ROM's symbols below 0x20 draw blank. The 8192 Chinese characters are not in the model: one shows as an empty box. Where text and graphics overlap, the model combines them with XOR, which is how these modules are reported to behave; the datasheet does not say. The cursor, blink and display shift are kept but not drawn, and the icon RAM shows nothing (this module has no icon segments). An instruction sent while the busy flag is set is still taken, where a real chip may miss it. The contrast is the module's trimmer, shown at its best, and the graphic RAM starts blank rather than random.

Code

#include <U8g2lib.h>

// clock (E), data (R/W), chip select (RS), no reset pin; PSB to GND
U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, 13, 11, 10, U8X8_PIN_NONE);

void setup() {
  u8g2.begin();
}

void loop() {
  u8g2.clearBuffer();
  u8g2.drawFrame(0, 0, 128, 64);
  u8g2.setFont(u8g2_font_ncenB14_tr);
  u8g2.drawStr(10, 22, "12864 LCD");
  u8g2.sendBuffer();
  delay(1000);
}

U8g2lib.h is Mokxi's own, with upstream's constructors: _SW_SPI (clock, data, cs, reset), _HW_SPI (cs, reset, on the board's SPI pins) and _8080 or _6800 (DB0 to DB7, enable, cs, dc, reset) for the parallel bus. U8glib.h's U8GLIB_ST7920_128X64 classes drive it too. The fonts are Mokxi's 5x8 at the nearest size.

Common mistakes

Leaving PSB unwired with a serial sketch: the module is then on its parallel bus and ignores the three wires. Swapping the pins: in serial mode RS is chip select, R/W is data and E is the clock. No backlight: without BLA and BLK wired, the blue glass is dark and nothing can be read.

See it in action

ST7920 12864 LCD on three wires at /templates draws a box, big text and a growing bar on an Uno with U8g2's software SPI constructor.