I try to send "hello world", but I have to send "ello, world!H", I don't have experience with C/C++
what's wrong with the code.
Master
#include <SPI.h>
void setup (void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8
}
void loop (void) {
char c;
digitalWrite(SS, LOW); // enable Slave Select
// send test string
for (const char * p = "ello, world!H" ; c = *p; p++)
{
SPI.transfer (c);
Serial.print(c);
}
digitalWrite(SS, HIGH); // disable Slave Select
delay(1);
}
Slave
#include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;
void setup (void) {
Serial.begin (115200);
pinMode(MISO, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect) // SPI interrupt routine
{
byte c = SPDR; // read byte from SPI Data Register
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == '\r') //check for the end of the word
process = true;
}
}
void loop (void) {
if (process) {
process = false; //reset the process
Serial.println (buff); //print the array on serial monitor
indx= 0; //reset button to zero
}
}
2. Upload the following sketches (Your sketches with slight simplification): (Hello World! appears on SM of Slave at 1-sec interval) Master-UNO Sketch:
#include <SPI.h>
char myMsg[] = "Hello World!";
void setup ()
{
Serial.begin(115200); //set baud rate to 115200 for usart
SPI.begin ();
digitalWrite(SS, HIGH); // disable Slave Select
SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8
//----------------------------
digitalWrite(SS, LOW); // enable Slave Select
}
void loop ()
{
for (byte i = 0; i < sizeof myMsg; i++)
{
SPI.transfer (myMsg[i]);
delayMicroseconds(10); //give time to Slave to process data
Serial.print(myMsg[i]);
}
SPI.transfer('\r'); //CR as terminating charcater
Serial.println();
delay(1000);
}
Slave-NANO Sketch:
#include <SPI.h>
char buff [50];
volatile byte indx = 0;
volatile bool process = false;
void setup ()
{
Serial.begin (115200);
pinMode(MISO, OUTPUT); // have to send on master in so it set as output
pinMode(SS, INPUT_PULLUP);
SPCR |= _BV(SPE); // turn on SPI in slave mode
SPI.attachInterrupt(); // turn on interrupt
}
void loop()
{
if (process == true)
{
Serial.println (buff); //print the array on serial monitor
process = false; //reset the process
indx = 0; //reset button to zero
}
}
ISR (SPI_STC_vect) // SPI interrupt routine
{
byte ch = SPDR; // read byte from SPI Data Register
buff [indx++] = ch; // save data in the next index in the array buff
if (ch == '\r') //check for the end of the word
{
process = true;
buff[indx] = '\0'; //insert null charcater
}
}
Hi GolamMostafa and all,
Thanks for the help! Especially for perfect document. I have to study more to understand the code.
This code seems put the \r in the front of the string if view as HEX, that seems to be issue same with what I have before.