Please, the project I am currently doing involves sending data to an RH850 controller. How it works is this, first, the RH850, which is the master sends a request and the slave, the Arduino, sends a response, which is a Hex code (0x400000) to the master (RH850). However, I am now confused as to what to do,
In the examples provided, I saw no instance of that. With nick gammon's example, I have been able to successfully make 2 arduinos talk to each other, but, I am not able to transmit hex value 0x400000.
/*
This project demonstrates SPI communication protocol by listening to status requests from a master controller, then, the slave device, an arduino,
after getting the status message, sends a response message to the master, telling it it's status. We will use the SPI library.
*/
#include <SPI.h>
#define cycle_start 3
unsigned long startCycleTime;
unsigned long endCycleTime;
const long CycleTime = 5000;
long time = 0;
long debounce = 200;
volatile boolean received_Data;
volatile byte command = 0;
uint32_t display[4] = {};
int myMessages[4] = {0x400000, 0x200000, 0x002000, 0x100000};
uint16_t value = 400000;
ISR(SPI_STC_vect) {
byte c = SPDR;
command = c;
if (command == 0) {
command = c;
SPDR = 0;
} else {
if (command == 1) {
SPDR = value;
}
}
}
void setup() {
Serial.begin(115200);
pinMode(MISO, OUTPUT);
pinMode(cycle_start, INPUT_PULLUP);
SPCR |= _BV(SPE);
SPCR |= _BV(SPIE);
}
void loop() {
if (digitalRead(SS) == HIGH){
command = 0;
}
Serial.println(value, DEC);
}
Which arduino are you using? On a uno or mega for example and int is only 2 bytes. You won’t store those values.
To your question, it would be something like this
digitalWrite(SS, HIGH);
SPI.begin ();
…
digitalWrite(SS, LOW);
SPI.transfer (0x40);
SPI.transfer (0x00);
SPI.transfer (0x00);
digitalWrite(SS, HIGH);
….
SPI.end (); // this turns SPI hardware off, so may be not what you want to do always
@J-M-L, I am using an uno, but, it is serving as a slave device. The examples that i have seen so far, I haven't seen SPI.transfer(val) for slave devices, hence the confusing. Is it possible to do that on a slave device?
I have worked that out and configured it as a slave device, but, I need to send data in this format (0x400000) to the master. SPDR can only send 8 bits of data at the same time. That's what I really need help with.
You need to call SPI.transfer() multiple times (3 for 3 bytes at least) on the master and the slave must manage through a state machine what the next ask is for.