Pages: [1]   Go Down
Author Topic: ST7920 multiple SPI device problems  (Read 404 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,  I'm currently working on a project where I read in a bunch of thermistors and current sensors and use that information to control a set of relays.  It's for a residential heating application where solar heated water is being used to warm the house.

The problem  I am having is that I can't get the ST7920 screen that I'm using (http://www.robotshop.com/ca/en/dfrobot-spi-lcd-module-arduino-compatible.html#Useful Links) to work reliably when other devices are communicating on the SPI line.   As soon as I initialize the SD card the screen starts to display random Chinese characters and I can't figure out why this is happening.  

The u8glib library mentions something about the SPI communication being on software mode and that other devices use hardware SPI.  However, I use the 'u8g.setHardwareBackup(u8g_backup_spi);' function that I think should fix this.

Any ideas on how to fix this would be greatly appreciated.  Right now I'm thinking that I might have to set up a little logic gate on the MOSI pin of the screen so that is the CS pin for the screen is off the sceen won't receive any communications.  However, I'd expect there to be a way of fixing this in software.

Thanks!

Andre

Code:
#include <U8glib.h>
#include <SD.h>

const int CS_ST7920 = 8; //chip select for Screen   (LOW = OFF)
const int CS_SD = 4;     //chip select for SD card (HIGH = OFF)?

boolean card_state = 0; //0 = sd card failure

String PRINT_ME = "Hello Screen";

U8GLIB_ST7920_128X64_1X u8g(13, 11, CS_ST7920);

void setup()
{
   delay(100);
   
   //Initalize chip select pins
    pinMode(SS, OUTPUT);
    pinMode(CS_ST7920, OUTPUT);
    pinMode(CS_SD, OUTPUT);
   
    //Default to having the device communication lines turned off
    digitalWrite(CS_ST7920, LOW); 
    digitalWrite(CS_SD, HIGH);
   
    //Initalize Screen
    u8g.begin();
    delay(50);
    u8g.setHardwareBackup(u8g_backup_spi);
    delay(50);
   
    //Initalize SD card
  Serial.begin(9600);
  if (!SD.begin(CS_SD)) {
    card_state = 0;
    Serial.println("card failed!");
  }else{
    card_state = 1;
    Serial.println("card initialized.");
  }
}

void loop()
{

  write_info();
 
}

//===============================================================
// SCREEN DRAW
//===============================================================

void write_info(void) {
  // picture loop
//    st7920_init();
//    delay(50);
  u8g.firstPage(); 
  do {
    draw();
  } while( u8g.nextPage() );
 
  // rebuild the picture after some delay
  delay(500);
}



void draw(void) {
  // graphic commands to redraw the complete screen should be placed here 
  u8g.setFont(u8g_font_unifont);
  u8g.setPrintPos(0, 20);
  // call procedure from base class
  u8g.print(PRINT_ME);
  delay(100);
  digitalWrite(CS_ST7920, LOW);
}

« Last Edit: November 11, 2013, 04:34:06 pm by fore4runner » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a picture of what happens:  http://bayimg.com/iacEGaAfe
« Last Edit: November 11, 2013, 04:41:45 pm by fore4runner » Logged

Germany
Offline Offline
Edison Member
*
Karma: 100
Posts: 1229
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Did you try
Code:
U8GLIB_ST7920_128X64_1X u8g(CS_ST7920);
instead of
Code:
U8GLIB_ST7920_128X64_1X u8g(13, 11, CS_ST7920);
?

Oliver
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just tried it, I just get a massive array of random chinese characters.  Doesn't seem to help.  Think this might be an issue with me not using an Arduino Mega?

As far as I can tell, this issue is directly related to adding the SD card initialization into the code.  The datasheet mentions that 'transients' cannot be present on the communication lines even when the CS is off. 
« Last Edit: November 11, 2013, 06:16:38 pm by fore4runner » Logged

Germany
Offline Offline
Edison Member
*
Karma: 100
Posts: 1229
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh, Arduino Mega.

This will enable HW SPI:
Code:
U8GLIB_ST7920_128X64_1X u8g(CS_ST7920);
This will enable SW SPI:
Code:
U8GLIB_ST7920_128X64_1X u8g(13, 10, CS_ST7920);

As far as i remember u8glib will only work with SD cards if both are set to HW SPI. That means, you must use
Code:
U8GLIB_ST7920_128X64_1X u8g(CS_ST7920);
However, you need to use other pins than 13 and 10 for the Mega. You must connect both Display and SD to pins 51 (MOSI) and 52 (SCK).

Oliver
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That seems to have done it!  I just had to use the HW spi mode and I needed to be a careful about the order I initialize things in.  Thanks so much!!!
Logged

Pages: [1]   Go Up
Jump to: