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!