LCD 12864B V2.0 with ST7920 - SDFat is interfering

Hello!

I am trying to build a data logger with the LCD 12864B V2.0 and an SD card reader, both SPI (after I have understood the wiring, thanks to this forum).
I have wired the SS of the display to pin 10 and the SS of the SD card to pin 5. It does work, but all kinds of weird stuff happen on the display.
I have tried experimenting with setting pin 10 high and low, but that did not help.

Any ideas what I could do?

This is the code:

#include <SdFat.h>
SdFat sd;
SdFile myFile;

#include <U8g2lib.h>
U8G2_ST7920_128X64_1_HW_SPI u8g2(U8G2_R0, /* CS=*/ 10, /* reset=*/ 8);


char fileName[] = "2468.txt";

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

void loop() 
{  
  sd.begin(5, SPI_HALF_SPEED);
  myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END);
  myFile.println("Test 2");
  myFile.close();  

  u8g2.firstPage();
    do {
      u8g2.setDrawColor(2);
      u8g2.setFont(u8g2_font_helvB12_tn);
      u8g2.setCursor(10, 32);
      u8g2.print("1234567890");
      } while ( u8g2.nextPage() );

 delay(1000);  
}

Shirley you would sd.begin(5, SPI_HALF_SPEED); and u8g2.begin(); in setup().

Write some data to your text file in setup().

Then read it in loop(). Display on u8g2.

Untested.

Both SD and U8G2 should work fine on the SPI bus. But only if they are both HW_SPI. (which you are doing)

David.

David, thanks for your reply, but I am afraid I don't quite understand it.

I have used the standard MISO, MOSI and SCK pins on the Arduino (Nano).

What difference does it make if I write something in setup?

Just try:

#include <SdFat.h>
SdFat sd;
SdFile myFile;

#include <U8g2lib.h>
U8G2_ST7920_128X64_1_HW_SPI u8g2(U8G2_R0, /* CS=*/ 10, /* reset=*/ 8);


char fileName[] = "2468.txt";

void setup()
{ 
  sd.begin(5, SPI_HALF_SPEED);
  u8g2.begin();

  myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END);
  myFile.println("Test 2");
  myFile.close(); 
}

void loop()
{ 
  u8g2.firstPage();
    do {
      u8g2.setDrawColor(2);
      u8g2.setFont(u8g2_font_helvB12_tn);
      u8g2.setCursor(10, 32);
      u8g2.print("1234567890");
      } while ( u8g2.nextPage() );

 delay(1000); 
}

Does that solve your problem?
If it does, I will explain.
If it does not, I will try it for myself.

David.

No, that did not help.
By the way, I have both the SD card reader and the backlight running on 3.3v from the arduino. The backlight is dimming slightly when the SD card is accessed. But that won't have anything to do with my problem?

My apologies. Look at this previous discussion especially #4.

Use HW SPI for SD. SW_SPI for ST7920

The ST7920 works at a reasonable speed with SW SPI. And it only costs you 3 GPIO pins.

David.

Uh! Thank you for your research. However, I have no idea how to do that, especially not with the U8g2lib.
Would it make sense to use a transistor to allow the MOSI signal to the display only when SS is high?

Most of the Internet solutions involve driving the "clock pin" i.e. E pin via a 3-state buffer like 74ACT125.

I suspect that you can do it with two 1N914 diodes and a 10k resistor.

But it seems much simpler to use SW SPI on some separate GPIO pins for the ST7920. e.g.

U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, 8, A1, A2, /* reset=*/ U8X8_PIN_NONE);

Hardware solutions would give you the best speed and save you two GPIO pins. (You need a separate CS pin whether you use SW SPI or HW SPI)

David.

It is that easy? Ok, thanks, I will try both options out tomorrow.

Thanks a lot, David!
I have tried fiddling around with diodes and a resistor, but that did nothing but introduce more problems.
But you were right, SW SPI works like a charm and solves all my problems. Well, at least those with the display. Great!