SPI clash?

I’ve got a OLED program that runs a series of test patterns (some commented out) very well…

I have edited this into a RFID program using the same SPI bus pins (except the reset pins which are different).

#define sclk 52//oled
#define mosi 51//oled
#define dc   15 //oled
#define cs   53//OCS on  big display
#define rst  2//oled
#define SDA_DIO 53//rfid
#define RESET_DIO 5//rfid
// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SPI.h>
#include <RFID.h>

Adafruit_SSD1351 tft = Adafruit_SSD1351(cs, dc, mosi, sclk, rst);  
RFID RC522(SDA_DIO, RESET_DIO); 

float p = 3.1415926;
int powerPin = 4;
int powerpinRead;
int prevpowerpinRead = LOW;
int powerpinState = LOW;
long toggletime = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup(void) {
   
  Serial.begin(9600);
  Serial.print("hello!");
  tft.begin();
SPI.begin(); 
  /* Initialise the RFID reader */
  RC522.init();
  pinMode(powerPin, OUTPUT); 
 
  Serial.println("init");
 uint16_t time = millis();
  tft.fillRect(0, 0, 128, 128, BLACK);
  time = millis() - time;
  
  Serial.println(time, DEC);
  delay(500);

}

void tftPrintTest() {
  tft.fillScreen(BLACK);
  tft.setCursor(0, 5);
  tft.setTextColor(RED);  
  tft.setTextSize(1);
  tft.println("Hello ARSE and Mandy!");
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.println("Its nice to be alive!");
 // tft.setTextColor(BLUE);
 // tft.setTextSize(3);
 // tft.print(1234.567);
  delay(1500);
  tft.setCursor(0, 5);
  tft.fillScreen(BLACK);
 tft.setTextColor(WHITE);
  tft.setTextSize(0);
 tft.println("Hello World!");
  tft.setTextSize(1);
 tft.setTextColor(GREEN);
  tft.print(p, 6);
  tft.println(" Want pi?");
  tft.println(" ");
  tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  tft.println(" Print HEX!");
  tft.println(" ");
  tft.setTextColor(WHITE);
  tft.println("Sketch has been");
  tft.println("running for: ");
  tft.setTextColor(MAGENTA);
  tft.print(millis() / 1000);
  tft.setTextColor(WHITE);
  tft.print(" seconds.");
  delay(1000);

}

void lcdTestPattern(void)
{
  uint32_t i,j;
  tft.goTo(0, 0);
  
  for(i=0;i<128;i++)
  {
    for(j=0;j<128;j++)
    {
      if(i<16){
        tft.writeData(RED>>8); tft.writeData(RED);
      }
      else if(i<32) {
        tft.writeData(YELLOW>>8);tft.writeData(YELLOW);
      }
      else if(i<48){tft.writeData(GREEN>>8);tft.writeData(GREEN);}
      else if(i<64){tft.writeData(CYAN>>8);tft.writeData(CYAN);}
      else if(i<80){tft.writeData(BLUE>>8);tft.writeData(BLUE);}
      else if(i<96){tft.writeData(MAGENTA>>8);tft.writeData(MAGENTA);}
      else if(i<112){tft.writeData(BLACK>>8);tft.writeData(BLACK);}
      else {
        tft.writeData(WHITE>>8);      
        tft.writeData(WHITE);
       }
    }
  }
}


void loop() {
lcdTestPattern();
 delay(500);

tftPrintTest();
 delay(1000);

 
 powerpinRead =RC522.isCard();
 delay (300);
 Serial.println ( powerpinRead);
 
 if (RC522.isCard())
{
     // Serial.println("Hello kevin");
}
if (powerpinRead == HIGH && prevpowerpinRead == LOW && millis() - toggletime > debounce) 
{
    if (powerpinState == HIGH)
       powerpinState = LOW;
        else
    powerpinState = HIGH;
    toggletime = millis();    
}
  digitalWrite(powerPin, powerpinState);
    prevpowerpinRead = powerpinRead;
   
  /* Has a card been detected? */
if (RC522.isCard())
  {
      /* If so then get its serial number */
    RC522.readCardSerial();
    Serial.println("Hello kevin");
     for(int i=0;i<5;i++)
    {
    Serial.print(RC522.serNum[i],DEC);
    //Serial.print(RC522.serNum[i],HEX); //to print card detail in Hexa Decimal format
    }
    Serial.println();
    Serial.println();
  }
}

The display code now appears not to write to the display but I can see the two test patterns are being run by a delay that appears on the serial monitor, its just they aren’t displaying. Obviously having the two patterns at the start of the loop also delays the input from the RFID but it does still toggle.

I cant work out why the display isn’t refreshing???

Show your wiring diagram. You are using pin 53 for two different purposes. This is possible but unlikely.

Both parts of the program work when run separately so I think the wiring is OK.

Wiring image attached. Top module is the RFID device.

When I run the code included the display writes one page then stops so it doesnt loop through both test patterns???

Actually I think the page that does display at the start is just the last page written to the display from doing a test with 'good' display code.

When I run the full program with both the RFID and display code (posted earlier) the display just wakes up and reloads the last page it saw. So while the pattern code is running (I can see that in the serial monitor time delay) the display isn't getting written to so I must be doing something wrong with the pins on the display. The RFID parts works fine.

743e749d2543883e6a645741600b9a5954417ad7.jpg

You can not use 53 for both devices as ChipSelect, only MISO, MOSI and CLK are shared.

Thanks

So if I move one of the 53's away to another pin are the rest of the pinouts OK?

Who knows? The wiring diagram is incomplete. Good wiring and good code are both important, and the code must reflect the wiring.

It is probably OK, but I do not know your wiring.

kpg:
So if I move one of the 53's away to another pin are the rest of the pinouts OK?

Moving one connection should at least make it possible to work. :slight_smile:

I moved RFID (CS/SDA) to pin 49 and changed the code to call the RFID pin as 49.

New drawing is attached.

It is still only giving me snow on the OLED.

I will do some more research later but it must be a simple error. Both OLED and RFID sets of code work independently, I am just trying to get both working on the SPI together.

Appreciate the help

5cbbadf43cf4dc9d615fc64f49bdeea78a4d99c3.jpg

You wanted to move one pin away from 53. Connecting both now to 49 makes it worse.

The drawing shows three unconnected pins on the OLED, so it is no wonder that the OLED does not work.

The last program that I saw used pin 53 twice, but pin 53 is not showing on the drawing at all.
The last program that I saw did not do anything with pin 49, but the drawing shows it as in use.

If you cannot provide written information and you will not follow written instructions, this is never going to work.

vaj4088:
The drawing shows three unconnected pins on the OLED,

No, look more closely. :wink:

The drawing shows all the connections on the OLED, but in a very pale grey which is being missed by
people - redo the drawing in monochrome please.

I had to look very closely at my computer monitor’s screen before I could see the other connections, and I had to get the adjustments just right. I can see the wires if I reduce brightness and gamma, and increase contrast. It is a lot of work, and has been unnecessary until now.

I see that MOSI is shared. I see that CLK is shared. That is good because it makes good use of SPI.

I see that OCS and SDA are shared. This can’t be good. I admit that I do not know what SDA means here (SDA is often used for one of the I2C wires), but I am certain that it should not be shared with OCS of the OLED.

I still have not seen the code that could work with this wiring.

Why should anyone be expecting that three of the wires are depicted differently from the other seven?

Yeah - that image is not good due to the colors chosen. Never underestimate the impact of improper monitor adjustment on readability. With light grey and white, it is particularly bad - for years back when I was playing this online game, I was complaining to friends about how unreadable the text on the updater was. Eventually I complained on the forums for the game, and people were like "i can read it just fine". Turns out the contrast and brightness were misadjusted on my monitor; readjusting them not only fixed that specific problem, but made everything look way better. But the lesson for people making images and websites for others to view is to make sure you pick colors with high contrast! Don't assume that everyone else's monitors display colors the same as yours, and not everyone's eyes are equal.

So, the SDA line on the RFID is CS in SPI mode? Okay - you should have labeled the pins more clearly IMO (ideally, list the function of each pin in the post or on the image).

CS for each SPI device must be unique. That's how you tell the device you're talking to it with SPI, so OCS needs to be on a different pin (no matter what OCS means in this context, connecting it to the CS line of another device is definitely wrong).

New black line image attached…

The top module is the 522 rfid unit which has an SDA pin which I believe is the CS pin?

Also code updated below…

#define sclk 52//oled
#define mosi 51//oled
#define dc   15 //oled
#define cs   53//OCS on  big display
#define rst  2//oled
#define SDA_DIO 49//rfid
#define RESET_DIO 5//rfid
// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SPI.h>
#include <RFID.h>

Adafruit_SSD1351 tft = Adafruit_SSD1351(cs, dc, mosi, sclk, rst);  
RFID RC522(SDA_DIO, RESET_DIO); 

float p = 3.1415926;
int powerPin = 4;
int powerpinRead;
int prevpowerpinRead = LOW;
int powerpinState = LOW;
long toggletime = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup(void) {
   
  Serial.begin(9600);
  Serial.print("hello!");
  tft.begin();
SPI.begin(); 
  /* Initialise the RFID reader */
  RC522.init();
  pinMode(powerPin, OUTPUT); 
 
  Serial.println("init");
 uint16_t time = millis();
  tft.fillRect(0, 0, 128, 128, BLACK);
  time = millis() - time;
  
  Serial.println(time, DEC);
  delay(500);

}

void tftPrintTest() {
  tft.fillScreen(BLACK);
  tft.setCursor(0, 5);
  tft.setTextColor(RED);  
  tft.setTextSize(1);
  tft.println("Hello ARSE and Mandy!");
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.println("Its nice to be alive!");
 // tft.setTextColor(BLUE);
 // tft.setTextSize(3);
 // tft.print(1234.567);
  delay(1500);
  tft.setCursor(0, 5);
  tft.fillScreen(BLACK);
 tft.setTextColor(WHITE);
  tft.setTextSize(0);
 tft.println("Hello World!");
  tft.setTextSize(1);
 tft.setTextColor(GREEN);
  tft.print(p, 6);
  tft.println(" Want pi?");
  tft.println(" ");
  tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  tft.println(" Print HEX!");
  tft.println(" ");
  tft.setTextColor(WHITE);
  tft.println("Sketch has been");
  tft.println("running for: ");
  tft.setTextColor(MAGENTA);
  tft.print(millis() / 1000);
  tft.setTextColor(WHITE);
  tft.print(" seconds.");
  delay(1000);

}

void lcdTestPattern(void)
{
  uint32_t i,j;
  tft.goTo(0, 0);
  
  for(i=0;i<128;i++)
  {
    for(j=0;j<128;j++)
    {
      if(i<16){
        tft.writeData(RED>>8); tft.writeData(RED);
      }
      else if(i<32) {
        tft.writeData(YELLOW>>8);tft.writeData(YELLOW);
      }
      else if(i<48){tft.writeData(GREEN>>8);tft.writeData(GREEN);}
      else if(i<64){tft.writeData(CYAN>>8);tft.writeData(CYAN);}
      else if(i<80){tft.writeData(BLUE>>8);tft.writeData(BLUE);}
      else if(i<96){tft.writeData(MAGENTA>>8);tft.writeData(MAGENTA);}
      else if(i<112){tft.writeData(BLACK>>8);tft.writeData(BLACK);}
      else {
        tft.writeData(WHITE>>8);      
        tft.writeData(WHITE);
       }
    }
  }
}


void loop() {
lcdTestPattern();
 delay(500);

tftPrintTest();
  delay(1000);

 
 powerpinRead =RC522.isCard();
 Serial.println ( powerpinRead);
 delay (200);
 if (RC522.isCard())
{
     // Serial.println("Hello kevin");
}
if (powerpinRead == HIGH && prevpowerpinRead == LOW && millis() - toggletime > debounce) 
{
    if (powerpinState == HIGH)
       powerpinState = LOW;
        else
    powerpinState = HIGH;
    toggletime = millis();    
}
  digitalWrite(powerPin, powerpinState);
    prevpowerpinRead = powerpinRead;
   
  /* Has a card been detected? */
if (RC522.isCard())
  {
      /* If so then get its serial number */
    RC522.readCardSerial();
    Serial.println("Hello kevin");
     for(int i=0;i<5;i++)
    {
    Serial.print(RC522.serNum[i],DEC);
    //Serial.print(RC522.serNum[i],HEX); //to print card detail in Hexa Decimal format
    }
    Serial.println();
    Serial.println();
  }
}

It still seems to lock out the OLED???

Any help guys?

As both programs work independantly I must be doing something wrong in the wiring or definitions?

Thanks

You could also have a SPI device that does not release all pins when not selected.

I know of some SD card breakouts that block any other SPI device without modification,
I also had to isolate a RC522 with a level shifter/buffer with inhibit/enable to make it work alongside a NRF24L01+.

with both modules wired up and powered and connected as in the previous attached program, if I load the OLED code only it works. If I load the RFID code only then it works.

But when I load the combined code the OLED just display coloured snow???

If I disconnect the MOSI and CLk links to the RFID and load the combined program the OLED still doesn't run.

BTW everything runs from 3.3v and as each separate piece seems to run with the MEGA on its own I assume I don't have any level shift problems...

So this standalone OLED program runs fine on hardware SPI…

// These are hardware spi
#define sclk 52
#define mosi 51
#define dc   15 
#define cs   53//OCS on  big display
#define rst  2

// Color definitions
#define	BLACK           0x0000
#define	BLUE            0x001F
#define	RED             0xF800
#define	GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SPI.h>


Adafruit_SSD1351 tft = Adafruit_SSD1351(cs, dc, mosi, sclk, rst);  

float p = 3.1415926;

void fillpixelbypixel(uint16_t color) {
  for (uint8_t x=0; x < tft.width(); x++) {
    for (uint8_t y=0; y < tft.height(); y++) {
      tft.drawPixel(x, y, color);
    }
  }
  delay(100);
}

void setup(void) {
  Serial.begin(9600);
  Serial.print("hello!");
  tft.begin();

  Serial.println("init");

 

 uint16_t time = millis();
  tft.fillRect(0, 0, 128, 128, BLACK);
  time = millis() - time;
  
  Serial.println(time, DEC);
  delay(500);
  
  
  
 
}

void tftPrintTest() {
  tft.fillScreen(BLACK);
  tft.setCursor(0, 5);
  tft.setTextColor(RED);  
  tft.setTextSize(1);
  tft.println("Hello Kevin and Mandy!");
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.println("Its nice to be alive!");
 // tft.setTextColor(BLUE);
 // tft.setTextSize(3);
 // tft.print(1234.567);
  delay(1500);
  tft.setCursor(0, 5);
  tft.fillScreen(BLACK);
 tft.setTextColor(WHITE);
  tft.setTextSize(0);
 tft.println("Hello World!");
  tft.setTextSize(1);
 tft.setTextColor(GREEN);
  tft.print(p, 6);
  tft.println(" Want pi?");
  tft.println(" ");
  tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  tft.println(" Print HEX!");
  tft.println(" ");
  tft.setTextColor(WHITE);
  tft.println("Sketch has been");
  tft.println("running for: ");
  tft.setTextColor(MAGENTA);
  tft.print(millis() / 1000);
  tft.setTextColor(WHITE);
  tft.print(" seconds.");
  delay(1000);

}



void lcdTestPattern(void)
{
  uint32_t i,j;
  tft.goTo(0, 0);
  
  for(i=0;i<128;i++)
  {
    for(j=0;j<128;j++)
    {
      if(i<16){
        tft.writeData(RED>>8); tft.writeData(RED);
      }
      else if(i<32) {
        tft.writeData(YELLOW>>8);tft.writeData(YELLOW);
      }
      else if(i<48){tft.writeData(GREEN>>8);tft.writeData(GREEN);}
      else if(i<64){tft.writeData(CYAN>>8);tft.writeData(CYAN);}
      else if(i<80){tft.writeData(BLUE>>8);tft.writeData(BLUE);}
      else if(i<96){tft.writeData(MAGENTA>>8);tft.writeData(MAGENTA);}
      else if(i<112){tft.writeData(BLACK>>8);tft.writeData(BLACK);}
      else {
        tft.writeData(WHITE>>8);      
        tft.writeData(WHITE);
       }
    }
  }
}


void loop() {
lcdTestPattern();
 delay(500);

tftPrintTest();
  delay(1000);
 
}

But when I paste this into the Main program listed earlier, the display just freezes. The RFID code in the Main program runs OK on its own as well, its just the combination that is wrong. Both modules are 3.3v, both seem to have OK wiring in that when both are wired up together the OLED and RFID (separated) programs work OK.

This is driving me nuts…