Hi,
i have set up of 2 arduino's uno which communicating by SPI.
The master code :
// Written by Nick Gammon
// February 2011
#include <SPI.h>
unsigned short controlReg = 10000000000000;
uint16_t masterReceive;
int bitIndexToSet=0;
void setup (void)
{
Serial.begin(115200); //Starts Serial Communication at Baud Rate 115200
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_DIV32);
} // end of setup
void loop (void)
{
// enable Slave Select
digitalWrite(SS, LOW); // SS is pin 10
bitSet(controlReg,bitIndexToSet);
masterReceive = SPI.transfer16 (controlReg);
bitClear(controlReg,bitIndexToSet);
bitIndexToSet++;
if (bitIndexToSet==15)
{
bitIndexToSet=0;
}
Serial.println(masterReceive ,BIN);
// disable Slave Select
digitalWrite(SS, HIGH);
delayMicroseconds(2000);
} // end of loop
The Slave Code:
// Written by Nick Gammon
// February 2011
#include <SPI.h>
#include <SoftwareSerial.h>
#define SCK_PIN 13 //CLK is 13
#define MISO_PIN 12 //MSIO is 12
#define MOSI_PIN 11 //MOSI is 11
#define SS_PIN 10 //SS is 10
unsigned short controlReg = 0;
volatile boolean process_it;
uint16_t regBuffer;
int channel;
uint16_t sendToMaster = 111111110000000;
byte higherByte = 0;
byte lowerByte = 0;
byte myBuffer[2]={0};
int i=0;
long interuptCounter = 0;
void setup (void)
{
Serial.begin (115200); // debugging
// turn on SPI in slave mode
SPCR = (1<<SPIE)|(1<<SPE)|(0<<DORD)|(0<<MSTR)|(0<<CPOL)|(0<<CPHA)|(0<<SPR1)|(1<<SPR0);
// have to send on master in, *slave out*
pinMode(MISO_PIN, OUTPUT);
// now turn on interrupts
SPI.attachInterrupt();
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
myBuffer[i]= SPDR;
i++;
interuptCounter++;
SPDR = sendToMaster;
}
//end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if (i==2)
{
Serial.println((int)myBuffer[0]<<8 & 0xFF00|(int)myBuffer[1], BIN);
i=0;
}
} // end of loop
The master send 16 bits to the slave by the spi.transfer16() function.
The 16 bits change every interrupt:
10000000000000001
10000000000000010
10000000000000100
10000000000001000
10000000000010000
10000000000100000
10000000001000000 ect...
The communication from master --> slave works fine ,and i do get the right data on the slave side.
After receiving the 16 bits from the master i am trying to send 16 bits (constant 16 bit:1111111100000000) back to the master from the slave.
But unfortunately i do not get this 16 bits on the master side.
The result i get on the master is like this:
1000000010000001
1000000010000010
1000000010000100
1000000010001000
1000000010010000
1000000010100000
1000000011000000
1000000010000000
1000000010000001
I am sending the data from slave to master by setting the SPDR value.
What is wrong?
What do i have to do on the slave side in order to send back to the master 16 bits?
What do i have to do on the master side in order to get the 16 bit from the slave?