OK, so i am really stuck and won't have much hair left soon! I am no longer trying to make the code in the above post work (using the SPI_anything.h header file). Instead i am focusing on the first example.
MASTER_ORIGINAL
// Written by Nick Gammon
// February 2011
#include <SPI.h>
void setup (void)
{
digitalWrite(SS, HIGH); // ensure SS stays high for now
// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();
// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
} // end of setup
void loop (void)
{
char c;
// enable Slave Select
digitalWrite(SS, LOW); // SS is pin 10
// send test string
for (const char * p = "Hello, world!\n" ; c = *p; p++)
SPI.transfer (c);
// disable Slave Select
digitalWrite(SS, HIGH);
delay (1000); // 1 seconds delay
} // end of loop
SLAVE_ORIGINAL
// Written by Nick Gammon
// February 2011
#include <SPI.h>
char buf [100];
volatile byte pos;
volatile boolean process_it;
void setup (void)
{
Serial.begin (115200); // debugging
// turn on SPI in slave mode
SPCR |= bit (SPE);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// get ready for an interrupt
pos = 0; // buffer empty
process_it = false;
// now turn on interrupts
SPI.attachInterrupt();
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR; // grab byte from SPI Data Register
// add to buffer if room
if (pos < (sizeof (buf) - 1))
buf [pos++] = c;
// example: newline means time to process buffer
if (c == '\n')
process_it = true;
} // end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if (process_it)
{
buf [pos] = 0;
Serial.println (buf);
pos = 0;
process_it = false;
} // end of flag set
} // end of loop
From the sound of it i am not alone where i am struggling with the pointers aspect. I have been going through pointer tutorials but i still can not make sense of it. At the moment my understanding is that the declaration of the pointer and the dereferencing is done in one statement i.e.
const char * p = "Hello, world!\n" ;
so the const char *p on the left hand side of the '=' sign is setting aside the amount of memory of size char at a location. then the actual string is assigned to that memory location. Then i get stuck with
c = *p; p++
i know that 'p++' is moving the memory location that the pointer is pointing at by 1, nut i don't understand the 'c = *p' part as when reading it said that this is illegal so i'm obviously missing something.
As i intend to send integers i have broken down three data values of 10,000, 20,000 and 30,000 into high bytes and low bytes as recommended and put them in an array. do i need to use pointers or can i just use a for loop? My non working attempt to send 3 integers broken down is below but isn't working.
MASTER
// Written by Nick Gammon
// February 2011
#include <SPI.h>
void setup (void)
{
digitalWrite(SS, HIGH); // ensure SS stays high for now
// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();
// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
} // end of setup
void loop (void)
{
// break down data into high and low bytes
//=======================================
int data_x = 10000;
byte high_byte_x = highByte(data_x);
byte low_byte_x = lowByte(data_x);
int data_y = 20000;
byte high_byte_y = highByte(data_y);
byte low_byte_y = lowByte(data_y);
int data_z = 30000;
byte high_byte_z = highByte(data_z);
byte low_byte_z = lowByte(data_z);
byte Stop = 0;
//=======================================
//declare c[] array
byte c[7] = {high_byte_x, low_byte_x, high_byte_y, low_byte_y, high_byte_z, low_byte_z, Stop};
// enable Slave Select
digitalWrite(SS, LOW); // SS is pin 10
// send test bytes one by one
for (int i = 0; i < 7; i++){
SPI.transfer(c[i]);
delay(1000);
}
// disable Slave Select
digitalWrite(SS, HIGH);
//delay (1000); // 1 seconds delay
} // end of loop
SLAVE
// Written by Nick Gammon
// February 2011
#include <SPI.h>
char buf [100];
volatile byte pos;
volatile boolean process_it;
void setup (void)
{
Serial.begin (115200); // debugging
// turn on SPI in slave mode
SPCR |= bit (SPE);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// get ready for an interrupt
pos = 0; // buffer empty
process_it = false;
// now turn on interrupts
SPI.attachInterrupt();
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte high_byte_x;
byte low_byte_x;
byte high_byte_y;
byte low_byte_y;
byte high_byte_z;
byte low_byte_z;
byte Stop;
byte c[7] = {high_byte_x, low_byte_x, high_byte_y, low_byte_y, high_byte_z, low_byte_z, Stop};
for(int i = 0; i < 7; i++){
c[i] = SPDR; // grab byte from SPI Data Register
// add to buffer if room
if (pos < (sizeof (buf) - 1)){
buf [pos++] = c[i];
{
// example: newline means time to process buffer
if (c[i] == 0)
process_it = true;
} // end of interrupt routine SPI_STC_vect
}
}
}
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if (process_it)
{
buf [pos] = 0;
for(int j = 0; j < 100; j++){
Serial.println (buf[j], BIN);
}
pos = 0;
process_it = false;
} // end of flag set
} // end of loop
Thanks
Ross