I am running a test program to test out the SPI programming to test my ADC circuit, however I could not get the SPI clock to run on my desired speed. The code is as follows, I am using the suggested way to perform a SPI transfer. The same method to call SPI is working perfectly on UNO R3, but on DUE the clock speed still staying at the default 4MHz. Is there any specific way to control the clock speed on DUE?
#include <SPI.h>
const int speedmax = 48000000; //set clock speed at 48MHz
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
SPI.begin(10);
}
void loop() {
// put your main code here, to run repeatedly:
for(byte wiper_value = 0; wiper_value <= 65535; wiper_value++) {
Serial.println(wiper_value);
SPI.beginTransaction(SPISettings(speedmax, MSBFIRST, SPI_MODE3));
SPI.transfer16(10, wiper_value); // send a new wiper value
SPI.endTransaction();
Serial.println(wiper_value); // read the value from analog pin A0 and send it to serial
delay(500);
}
}
PS: I tried using the old, phase out method and it works, but I would like to use the suggested method to build my program.
Unless things have changed, the Serial.print() function is blocking once the serial tx buffer is full.
On SPI, try a speed lower than 48MHz but higher than 4MHz. Maybe 24?
Edit: ...and the code will stay in the Serial.print() function until there is enough room in the tx buffer for the entire string.
I had tried SPI.setClockDivider() and it works at 48MHz without removing the Serial functions, but there is a problem, between each 8bits transferred there's a relatively huge gap despite I am using SPI.transfer16. Do you have any suggestions to reduce the gap between each byte?
This was already removed the serial communication parts.
My new code is as below, the SPI waveform is still similar to the photo above, there's a 1us gap between each bytes in a single transaction.
#include <SPI.h>
const int speedmax = 48000000; //set clock speed at 48MHz
void setup() {
// put your setup code here, to run once:
SPI.begin(10);
SPI.setClockDivider(10,2);
}
void loop() {
// put your main code here, to run repeatedly:
for(byte wiper_value = 0; wiper_value <= 65535; wiper_value++) {
SPI.beginTransaction(SPISettings(speedmax, MSBFIRST, SPI_MODE3));
SPI.transfer16(10, wiper_value); // send a new wiper value
SPI.endTransaction();
}
}
Theres no change on the output, same that there's a 1us gap between the first and second 8 bits, same for changing the code to both int and replace the for loop with a while(1) loop, there is still no sign of the 16bits are transferred without a gap.
Take a look here. Since you are using the 16 transfer, pay attention to lines 225 and 226, 229 and 230, 234 and 235, and again lines 238 and 239. Those are delays.