0

I'm working on a project involving two Arduino boards communicating over SPI — one configured as master, the other as slave. The master sends a byte (0xA5) every 500ms, and the slave is supposed to receive it via an interrupt (SPI_STC_vect). However, the slave doesn't seem to receive any data — the interrupt is not triggered.

#include <SPI.h>

const uint8_t MOSI_PIN = 11;
const uint8_t MISO_PIN = 12;
const uint8_t SCK_PIN  = 13;
const uint8_t SS_PIN   = 10;

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

  pinMode(MOSI_PIN, OUTPUT);
  pinMode(MISO_PIN, INPUT);
  pinMode(SCK_PIN, OUTPUT);
  pinMode(SS_PIN, OUTPUT);

  SPI.begin();
  digitalWrite(SS_PIN, HIGH);
}

void loop() {
  uint8_t outByte = 0xA5;

  Serial.print("Master kirim: 0b");
  Serial.println(outByte, BIN);

  digitalWrite(SS_PIN, LOW);

  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
  SPI.transfer(outByte);
  SPI.endTransaction();

  digitalWrite(SS_PIN, HIGH);
  delay(500);
}


#include <SPI.h>

// Pin SPI
const uint8_t MOSI_PIN = 11;
const uint8_t MISO_PIN = 12;
const uint8_t SCK_PIN  = 13;
const uint8_t SS_PIN   = 10;

volatile uint8_t lastReceived = 0;
volatile uint32_t recvCount = 0;

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

  pinMode(MOSI_PIN, INPUT);
  pinMode(MISO_PIN, OUTPUT);
  pinMode(SCK_PIN, INPUT);
  pinMode(SS_PIN, INPUT_PULLUP);

  SPCR = _BV(SPE) | _BV(SPIE);  // Enable SPI + interrupt
}

ISR(SPI_STC_vect) {
  lastReceived = SPDR;  // Ambil data dari buffer SPI
  recvCount++;          // Hitung jumlah data yang diterima
}

void loop() {
  static uint32_t lastCount = 0;

  if (recvCount != lastCount) {
    Serial.print(">> ISR terjadi #");
    Serial.print(recvCount);
    Serial.print(", data diterima = 0b");
    Serial.println(lastReceived, BIN);
    lastCount = recvCount;
  }
}

I connected two Arduino boards using SPI. The master sends a byte (0xA5) every 500ms using SPI.transfer(), and the slave is configured with an interrupt (SPI_STC_vect) to read incoming data from SPDR. I expected the slave to receive the data and print it via Serial when recvCount increases. However, the slave doesn’t print anything, indicating the interrupt never triggers and the data is not being received as expected.

2
  • #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); } void loop() { if (Serial.available() > 0) { String message = Serial.readString(); Wire.beginTransmission(8); Wire.write(message.c_str()); Wire.endTransmission(); Serial.println("Message sent: " + message); } delay(1000); } Commented May 22 at 4:00
  • #include <Wire.h> void setup() { Wire.begin(8); // Inisialisasi sebagai slave dengan alamat 8 Wire.onReceive(receiveEvent); // Menetapkan fungsi callback saat menerima data Serial.begin(9600); } void loop() { delay(1000); // Delay untuk mencegah pembacaan berlebihan } void receiveEvent(int bytes) { String message = ""; while (Wire.available()) { char c = Wire.read(); // Membaca karakter satu per satu message += c; // Menyusun pesan dari karakter } Serial.println("Message received: " + message); } Commented May 22 at 4:00

1 Answer 1

0

You forgot to attach interrupt handler on slave

#include <SPI.h>

// Pin SPI
const uint8_t MOSI_PIN = 11;
const uint8_t MISO_PIN = 12;
const uint8_t SCK_PIN  = 13;
const uint8_t SS_PIN   = 10;

volatile uint8_t lastReceived = 0;
volatile uint32_t recvCount = 0;

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

  pinMode(MOSI_PIN, INPUT);
  pinMode(MISO_PIN, OUTPUT);
  pinMode(SCK_PIN, INPUT);
  pinMode(SS_PIN, INPUT_PULLUP);

//I'm not sure it you have to touch other bits
  //SPCR = _BV(SPE) | _BV(SPIE);  // Enable SPI + interrupt
  SPCR |= _BV(SPE) | _BV(SPIE);  // Enable SPI + interrupt

//This was forgotten
  SPI.attachInterrupt();
}

ISR(SPI_STC_vect) {
  lastReceived = SPDR;  // Ambil data dari buffer SPI
  recvCount++;          // Hitung jumlah data yang diterima
}

void loop() {
  static uint32_t lastCount = 0;

  if (recvCount != lastCount) {
    Serial.print(">> ISR terjadi #");
    Serial.print(recvCount);
    Serial.print(", data diterima = 0b");
    Serial.println(lastReceived, BIN);
    lastCount = recvCount;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.