Arduino SPI Master receive data

Hi, I have a project where there will be an Arduino chip acting as the main micro controller. The Arduino chip will be controlling a synthesizer (slave) which will produce a frequency from what the user specifies. The user will input their desired frequency over a SPI bus. So therefore, the Arduino chip will have to receive SPI data and then send out SPI data to the slave (synthesizer) in the system. Is this possible? Any example code will be helpful.

A good place to start would be the examples that come with the SPI library. In the IDE, File->examples->SPI->...

The user will input their desired frequency over a SPI bus. So therefore, the Arduino chip will have to receive SPI data

Please explain more about input of desired frequency to the Arduino over SPI. What is sending that data? How is it configured?

Assuming both the master and slave are Arduinos, you can use SerialTransfer.h to communicate quickly and reliably over SPI (or UART or I2C).

Install instructions

Example Master:

#include "SPITransfer.h"


SPITransfer myTransfer;

struct STRUCT {
  char z;
  float y;
} testStruct;

char arr[] = "hello";


void setup()
{
  Serial.begin(115200);
  
  digitalWrite(SS, HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8);

  myTransfer.begin(SPI);

  testStruct.z = '

Example Slave:

#include "SPITransfer.h"


SPITransfer myTransfer;

struct STRUCT {
  char z;
  float y;
} testStruct;

char arr[6];

volatile bool procNewPacket = false;


void setup()
{
  Serial.begin(115200);
  
  SPCR |= bit (SPE);
  pinMode(MISO, OUTPUT);
  SPI.attachInterrupt();
  
  myTransfer.begin(SPI);
}


void loop()
{
  if(procNewPacket)
  {
    procNewPacket = false;
    
    // use this variable to keep track of how many
    // bytes we've processed from the receive buffer
    uint16_t recSize = 0;

    recSize = myTransfer.rxObj(testStruct, recSize);
    Serial.print(testStruct.z);
    Serial.print(testStruct.y);
    Serial.print(" | ");

    recSize = myTransfer.rxObj(arr, recSize);
    Serial.println(arr);
  }
}


ISR (SPI_STC_vect)
{
  if(myTransfer.available())
    procNewPacket = true;
}

;
 testStruct.y = 4.5;
}

void loop()
{
 // use this variable to keep track of how many
 // bytes we're stuffing in the transmit buffer
 uint16_t sendSize = 0;

///////////////////////////////////////// Stuff buffer with struct
 sendSize = myTransfer.txObj(testStruct, sendSize);

///////////////////////////////////////// Stuff buffer with array
 sendSize = myTransfer.txObj(arr, sendSize);

///////////////////////////////////////// Send buffer
 myTransfer.sendData(sendSize);
 delay(500);
}


Example Slave:

§DISCOURSE_HOISTED_CODE_1§

I've been doing some research and to just demonstrate it I have two Arduinos. One Arduino is connected to the PC and you input your frequency via the serial monitor and it should send it to the other Arduino which is controlling the slave devices. However, I don't know which is the master and which is the slave. Can't seem to find any example code on the matter. It's almost like you want the Arduino connected to the PC being a slave and then telling the master that it has code to send. However, I have no idea how to do this.

So it sort of goes like this;

Serial monitor >>>data>>> Arduino >>>SPI Extender >>> MicroController (Another Arduino) >>> SPI Slaves

Why do you need >3 Arduinos? (Assuming the SPI slaves are Arduinos) Can you explain your project idea in detail?

Yes of course. So this is exactly what I've got. Customer sends in an SPI command, I then need to take that SPI command, decode it and control multiple slaves.

Callum - your duplicate question in Programming Questions has been deleted

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

One Arduino is connected to the PC and you input your frequency via the serial monitor

Customer sends in an SPI command, I then need to take that SPI command, decode it and control multiple slaves.

There is conflict between the two statements. First you talk about data sent from the serial monitor, then you say that the data sent from the PC to the Arduino needs to use the SPI bus and its communication protocol? Which is it?

ignore the statement about the serial monitor, it was more for demonstration purposes and to understand how it might work

What is the device sending the frequency setting to the Arduino? Is it a master or slave? Can you provide documentation? Do you have control over its configuration?

That's the bit I don't know :smiley:
My device just has to accept incoming SPI data

Callum_Austin11:
That's the bit I don't know :smiley:
My device just has to accept incoming SPI data

Did you see my first reply?

It explains how to transmit and accept packetized SPI data (again, assuming both master and slaves are Arduinos).