I have developed a simple code for learning about SPI in Arduino Due. The code is compiling properly and now I would like to learn the next step on how to test the code with the board and get the master to communicate with the slave. Here is the code:
#include"SPI.h"
int mosi = 75; //assigning variables to pins
int sck = 76;
int ss = 10;
int miso = 74;
void setup()
{
pinMode(mosi,OUTPUT); //Configuring pins as input and output
pinMode(sck,OUTPUT);
pinMode(ss,OUTPUT);
pinMode(miso,INPUT);
SPI.begin(10); // waking up SPI bus
SPI.setDataMode(10,SPI_MODE_0); //setting mode for clk phase & pol
SPI.setBitOrder(10, MSBFIRST); // setting bit order for transfer
SPI.setClockDivider(10,42); //setting clock to 2 MHz
digitalWrite(10,HIGH); //keeping slave device unactive
}
void set_value(int value);
{
digitalWrite(10,LOW); //activate slave select line
SPI.transfer(0);
SPI.transfer(value); // transfer values from 0 to 255
digitalWrite(10,HIGH); //deactivate slave line after transfer
}
void loop()
{
for(int i=0 ; i<256 ;i++) //values to transfer
{
set_value(i); //call function to transfer values
delay(100);
}
}
IDE used : Arduino,
Board used : Arduino Due
I have some doubts regarding this code:
1) How to test this code with my borad to check if its working correctly?
2) Why isn't the mosi, sck and ss pins used anywhere in the code, while all operations/functions are being done with respect to the slave line ss = 10?. Does the SPI module automatically take care of the data transmission and reception through MOSI and MISO?
3) What are the possible hardware connections if I am using one due board as my master and another due board as my slave?.
4) Should I dump this code into the master as well as slave for it work? Or Since master has to transmit and receive data from the slave board, I need to do some changes in this code?
5) If I am missing out anything or any directions regarding this would be helpful. I am newbie to embedded systems and totally confused & full of stupid doubts. PLz dont mind. Thanks!
Best Answer
The Arduino DUE uses a slightly different methodology for SPI compared to the rest of the Arduino boards. It handles the operation of the SS line for you, so you don't have to.
While you can use the manual software method, it's better to use the hardware facilities where available.
What this means is you initialize the SPI with the SS pin you are using:
Then you send and receive using the "continuation" versions of the SPI transfer function:
The first parameter is the SS pin (it's used as a key to connect the function to the right SPI instance), the second is the value to transfer, and the third tells SPI not to raise the SS pin after transfer.
The SPI on the Due is a single SPI bus with multiple hardware SS pins. The configuration of the bus is attached to that SS pin, so when you activate an SS pin it automatically configures the bus to the settings you have associated with that SS pin. This allows one bus to have multiple different settings (speed, clock polarity, bit order, etc) and switch between them properly without any manual intervention on your part.
You can read more about this way of working here: http://arduino.cc/en/Reference/DueExtendedSPI
As for testing your code, when I need to test an SPI port I usually create a loopback connection (connect MOSI to MISO) and send random values through it, receiving what is sent. The sent value and received value should match if the SPI is working properly.