Communication between two slaves via single SPI bus
Posted: Tue Oct 27, 2020 9:37 pm
I am trying to control both RC522 and lcd st7920 (128x64) via a single SPI bus on the NodeMCU esp8266. I am using Arduino core for my esp8266. Now in this portion of the code, I only want to display the UID of the card on the lcd each time a card is scanned on the RC522. The code is as follows:
I am new to using the esp8266 and this is my first time using multiple slaves on a single SPI bus so I may not know the correct way to using multiple slaves. I tried connecting only the rfid rc522 with esp8266 via SPI and it worked flawlessly with the schematic. Similarly the lcd also works flawlessly individually on the SPI bus with the same schematic.
The issue only exists if I try to connect both via SPI. The problem is that only the first time a card is scanned, is it displayed on the lcd. After that no more cards are scanned and the only the first card scanned is displayed on the lcd.
What I am trying to do is that every time a card is scanned, its UID be displayed on the lcd. I've posted the schematic and my actual setup which you can check.
Code: Select all
#include <SPI.h>
#include <MFRC522.h>
#include <U8g2lib.h>
#define SPI_SCK_pin 14 // Clock pin of SPI
#define SPI_MOSI_pin 13 // MOSI pin of SPI
#define SPI_RST_pin 0 // RST-PIN for RC522 - RFID - SPI - Modul GPIO5
#define SPI_rfid_SS_pin 15 // SDA-PIN for RC522 - RFID - SPI - Modul GPIO4
MFRC522 mfrc522(SPI_rfid_SS_pin, SPI_RST_pin); // Create MFRC522 instance
#define SPI_lcd_SS_pin 5 // Slave Select pin of SPI for lcd<->RS
U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R2, SPI_SCK_pin, SPI_MOSI_pin, SPI_lcd_SS_pin, U8X8_PIN_NONE);
void setup() {
SPI.begin(); // Init SPI bus
Serial.begin(115200); // Initialize serial communications
u8g2.setFont(u8g2_font_amstrad_cpc_extended_8f);
}
void loop() {
mfrc522.PCD_Init(); // Init MFRC522
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
delay(50);
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
delay(50);
return;
}
Serial.print(F("Card UID:"));
String uid = dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
digitalWrite(SPI_rfid_SS_pin, HIGH); //return rfid slave pin back to normal
u8g2.begin();
u8g2.clearBuffer();
u8g2.setCursor(3,10);
u8g2.print (uid);
u8g2.sendBuffer();
digitalWrite(SPI_lcd_SS_pin, HIGH); //return lcd slave pin back to normal
// delay (10);
}
// Helper routine to dump a byte array as hex values to Serial
String dump_byte_array(byte *buffer, byte bufferSize) {
String userid;
for (byte i = 0; i < bufferSize; i++) {
userid += String(buffer[i]);
}
Serial.print(userid);
return userid;
}
I am new to using the esp8266 and this is my first time using multiple slaves on a single SPI bus so I may not know the correct way to using multiple slaves. I tried connecting only the rfid rc522 with esp8266 via SPI and it worked flawlessly with the schematic. Similarly the lcd also works flawlessly individually on the SPI bus with the same schematic.
The issue only exists if I try to connect both via SPI. The problem is that only the first time a card is scanned, is it displayed on the lcd. After that no more cards are scanned and the only the first card scanned is displayed on the lcd.
What I am trying to do is that every time a card is scanned, its UID be displayed on the lcd. I've posted the schematic and my actual setup which you can check.