So I have a project where I will be using an Arduino Leonardo and an external ADC (AD7980) to run a 16-bit (18bit) conversion and transmit it to the Arduino and then out on USB as fast as possible.
The AD7980 can run up to 1 MSPS, and I know I cannot reach that with the ATmega32U4 - but I want to know how to optimize my code for running it as fast as possible and how to measure it.
Right now my code is:
#include <SPI.h>
int CNV= 11; //Convert on pin 11
int Slaveselect=8;
int adcValue;
byte highByte;
byte lowByte;
void setup() {
Serial.begin(115200);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV2 ); // 8 MHz rate
SPI.setBitOrder(MSBFIRST);
pinMode(Slaveselect, OUTPUT);
pinMode(CNV, OUTPUT);
}
void loop() {
digitalWrite(Slaveselect,HIGH);
// SS always high to use 3 Wire mode without busy indicator
digitalWrite(CNV,LOW);
digitalWrite(13,LOW);
digitalWrite(CNV,HIGH);//starts conversion
digitalWrite(13, HIGH);
digitalWrite(CNV,LOW); //starts to transmit MSB on data line
digitalWrite(13,LOW);
highByte = SPI.transfer(0x00);
lowByte = SPI.transfer(0x00);
adcValue = highByte << 8;
adcValue = adcValue | lowByte;
Serial.print("analog value =");
Serial.println(adcValue);
Serial.print('\n');
}
The datasheet for the ADC can be found here: Datasheet