I'm trying to use Arduino Due to communicate with a MAX11040K ADC converter. Everything is working properly. I'm using Serial communication with Matlab, and SPI communication with the converter. But the rate of data acquisition is far below what I expected. I do a collection test for 1 second, and the maximum number of bytes I can collect is approximately 25,000. Is there any way to optimize my code or even communication? Can anyone tell me if this is the maximum bytes rate I can get?
Here is the code:
#include <DueTimer.h> // interrupt Timer
#include <SPI.h> // SPI communication
int CS = 24; // Chip Selected connected to digital pin 24
int comandoUsb=0; // receive command via USB
const byte PinLed = 50; // Led
bool state = false;
int byte1=0; // bytes receiveds from SPI
int byte2=0;
int byte3=0;
void flash(){
state = !state;
digitalWrite(PinLed, state); // Led off...
Timer3.stop(); // stop timer interrupt and stop transmition of data
}
void setup() {
pinMode(CS, OUTPUT); // sets the digital pin as output
pinMode(PinLed, OUTPUT); // sets the digital pin as output
Timer3.attachInterrupt(flash); // attach interrupt
Serial.begin(250000); // initialize the serial communication:
SPI.begin(); // start SPI communication
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE2)); // sets parameters SPI communication
}
void loop() {
digitalWrite(CS, HIGH); // CS HIGH
if (Serial.available()) { // if something in the buffer, read
comandoUsb = Serial.read();
}
if(comandoUsb == 'a'){ // if receive 'a', set up converter register config
digitalWrite(CS, LOW);
SPI.transfer(96);
SPI.transfer(48);
digitalWrite(CS, HIGH);
comandoUsb= 0;
}
if(comandoUsb == 'b'){ // if receive 'b', read converter register config and send by Serial
digitalWrite(CS, LOW);
SPI.transfer(224);
byte1 = SPI.transfer(0);
digitalWrite(CS, HIGH);
Serial.write(byte1) ;
comandoUsb= 0;
}
if(comandoUsb=='c'){ // if receive 'c', read data register while interrupt is not call
state = !state;
digitalWrite(PinLed, state);
comandoUsb= 0;
Timer3.start(1000000); // Calls every 1000ms
while(state){
digitalWrite(CS, LOW);
SPI.transfer(240);
byte1 = SPI.transfer(0);
byte2 = SPI.transfer(0);
byte3 = SPI.transfer(0);
digitalWrite(CS, HIGH);
Serial.write(byte1);
Serial.write(byte2);
Serial.write(byte3);
}
}
}
I expected it to get more bytes because I did some testing with an 8bit PIC18F4550 and 48Mhz clock and I got 53,000 bytes in a second. And this arduino works with 84MHz and 32bit, so you should get more
I appreciate any help.