Problems with SPI communication on Arduino DUE

Hello all!
I 've been struggling to make Arduino DUE communicate with MAX7219 via SPI. It should use MAX display driver to control 40 LEDs. I am a newbie to programming, so the first attempt was unsuccessful, so I decided to test the SPI on board, attached the oscilloscope, but did not detect sending any bits through MOSI line on SPI header. Both lines (CLK and MOSI) seams dead (no changes on the lines). Code is pretty simple, any help is appreciated. I’ve tested code on two DUEs, the same happens.

#include <SPI.h>

#define CS1 4

void setup() {
// put your setup code here, to run once:
digitalWrite(CS,1);
SPI.begin(CS1);
}

void loop() {
// put your main code here, to run repeatedly:
delay(1000);
SPI.transfer(CS1,0xA1,SPI_LAST);
delay(2000);
}

Hi,
You’ve got a bug in your code. you define CS1 and then use CS. look below for highlighted code :wink:

EDIT: are you using an ethernet shield with a sd card reader? that uses pin 4 for CS…

caleksa:
Hello all!
I 've been struggling to make Arduino DUE communicate with MAX7219 via SPI. It should use MAX display driver to control 40 LEDs. I am a newbie to programming, so the first attempt was unsuccessful, so I decided to test the SPI on board, attached the oscilloscope, but did not detect sending any bits through MOSI line on SPI header. Both lines (CLK and MOSI) seams dead (no changes on the lines). Code is pretty simple, any help is appreciated. I’ve tested code on two DUEs, the same happens.

#include <SPI.h>

#define CS1 4

void setup() {
// put your setup code here, to run once:
digitalWrite(CS,1);
SPI.begin(CS1);
}

void loop() {
// put your main code here, to run repeatedly:
delay(1000);
SPI.transfer(CS1,0xA1,SPI_LAST);
delay(2000);
}

Yes, pdoriam, you are right, it is the part of old code, but unfortunately, SPI is not working.
No, I want to control 40 LEDs, so I use MAX7219 display driver, and communication between DUE and MAX is via SPI. The given code is used for testing SPI, as previously mentioned (not for communication with MAX), but there is no changes on the MOSI and SCLK lines (checked on oscilloscope).
Any idea what could it be?
Thanks in advance!

Caleksa,

Can you confirm your Arduino IDE version?

Try running this code and check the output on scope? Do you have a scope handy? Or a logic Analyzer?

// inslude the SPI library:
#include <SPI.h>

// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;

void setup() {

pinMode (slaveSelectPin, OUTPUT);
SPI.begin();
}

void loop() {

digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0xAA);
SPI.transfer(0xFF);
digitalWrite(slaveSelectPin, HIGH);

delay(1000);
}

Hello all,
Finally, tho problem is solved! The IDE version is 1.5.4. I attached the oscilloscope to the SPI pins and saw that there is no signal on the pins (SPI header on the board), although it should be. I guess that somehow the SPI communication is not enabled, so I consulted the SAM3X manual and write the approipriate values in the SAM3X registers and now it works. Below are given some parts of the code (two functions) which might help someone with the similar problem. Please note that the SPI is used for the communication with the MAX7219 display driver.

#include <SPI.h>

#define CS 10

// initialize the SPI
void initializeSPI() {
SPI.begin(CS);
// allow writing to the control registers
SPI0->SPI_WPMR = 0x53504900;
// set the connection params
SPI0->SPI_MR = 0xF4000011;
SPI0->SPI_CSR[0] = 0x000AF586;

// allow SPI communication
SPI0->SPI_CR = 0x00000001;
}

// Function for sending 2 bytes
void sendDataSPI(char addr, char data) {

// combine two arguments into one
// higher byte is address, lower is data
int temp = 0;
temp = temp|data; // lower byte
temp = temp|(addr << 8); // higher byte
// check status bit TDRE
while((SPI0->SPI_SR & SPI_SR_TDRE) == 0) {}
SPI0->SPI_TDR = temp;

}

Cheers!