Failing to bit-bang parallel mode on an ST7920 (U8g2 works!)

I thought I'd try to manually bit-bang parallel mode on an ST7920 controller with my Arduino Uno because I'm planning to try and set it up with a Z80 microcomputer. Alas, after two evenings I am no closer to working out why my quite simple code doesn't work! Here's the code:

#define RS 0
#define EN 1
#define D0 2

void setup() {
  Serial.begin(9600);

  Serial.println("Restarting!");
  
  // Prepare pins for output
  pinMode(RS, OUTPUT);
  pinMode(EN, OUTPUT);
  for (int pin = D0; pin < D0 + 8; pin +=1) {
    pinMode(pin, OUTPUT);
  }
  digitalWrite(EN, LOW);
  
  lcd_init();

  // Send some characters
  lcd_send(0, 0x93);
  delayMicroseconds(80);
  lcd_send(1, 'A');
  delayMicroseconds(80);
  lcd_send(1, ' ');
  delayMicroseconds(80);
}

void lcd_init() {
  delay(1000);
  lcd_send(0, 0b00110000);  // Basic function set
  delayMicroseconds(200);
  lcd_send(0, 0b00110000);  // Basic function set
  delayMicroseconds(200);
  lcd_send(0, 0b00001111);  // Set display (display on, cursor on, blink on)
  delayMicroseconds(200);
  lcd_send(0, 0b00000001);  // Clear display (wait >1.6 ms after)
  delay(100);
  lcd_send(0, 0b00000110);  // Set entry mode
  delayMicroseconds(200);
  Serial.println("Done init");
}

void lcd_send(int lcd_mode, byte data) {
  digitalWrite(RS, lcd_mode);

  for (int pin = D0; pin < D0 + 8; pin +=1) {
    digitalWrite(pin, data & 1);
    data = data >> 1;
  }

  digitalWrite(EN, HIGH);
  delayMicroseconds(100);
  digitalWrite(EN, LOW);
}

Nothing appears on the LCD when I run this code, not even the cursor.

I feel like I must be missing something simple, because the display works perfectly with identical wiring, on the same board, with the following U8g2 code:

#include <U8g2lib.h>

U8G2_ST7920_128X64_1_8080 u8g2(
  /* Rotation */ U8G2_R2,
  /*D0 - D7*/ 2, 3, 4, 5, 6, 7, 8, 9,
  /*EN*/ 1,
  /*CS*/ U8X8_PIN_NONE,
  /*RS*/ 0);

void setup() {
  u8g2.begin();

  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_profont11_tf);
    u8g2.drawStr(0,20,"Hello World!");
  } while ( u8g2.nextPage() );
}

I'd appreciate any help - I feel like I've scoured every line of my code, and the datasheet, countless times.

Your "simple code" has no loop() function.

I would avoid digital #0, #1 because they are used by Serial. e.g. wire RS, EN on 10, 11 and test with U8g2 first. EDIT. changed from 9, 10 to avoid data bus pins.

Your code looks about right but I have not tested it.

Oh, you have chosen a very sensible "development" strategy. i.e. start with known library and known wiring.

Life is considerably easier with ST7920 in SPI mode. Less wires !

David.

David, that was it! Moving EN and RS to 12 and 13 made the display work immediately. I suppose the call to Serial.begin and the print statements were disturbing the display's mojo - especially I imagine EN was getting spurious hits.

Rightly noted - I took it out for the forum post, sorry.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.