[Reposted from General forum - this is a technical support question]
Greetings All!
I'm using the Teensy 4.0 with an external ADC (the ADS8861, which is a 16 bit, SPI output ADC). I'm in the testing phase, and I'm getting erroneous outputs on the SPI data and clock lines.
The yellow trace is the "conversion start" pin. I'm triggering ADC conversions at 10kHz. The blue trace is the SPI clock line. My complete code is here:
When I debug my ADC result, I get an erroneous value of ADCresult = 65535. Here's the question: In my interrupt, I call on 2 SPI.transfer() transactions (8 bits + 8 bits = 16 bits in total, for the ADC result). In the oscilloscope capture, there are 3 blocks of SPI clocks (the blue trace). This tells me that 3 SPI transactions are happening, even though I only called for 2. How can this happen? I'm not sure why my SPI is "triggering" too many times.
Greetings All!
I'm using the Teensy 4.0 with an external ADC (the ADS8861, which is a 16 bit, SPI output ADC). I'm in the testing phase, and I'm getting erroneous outputs on the SPI data and clock lines.
The yellow trace is the "conversion start" pin. I'm triggering ADC conversions at 10kHz. The blue trace is the SPI clock line. My complete code is here:
Code:
/* RGB Analog Example, Teensyduino Tutorial #2
http://www.pjrc.com/teensy/tutorial2.html
This example code is in the public domain.
*/
#include <SPI.h>
#define HWSERIAL Serial1
const int adcTriggerPin = 2; //PWM to the ADC ("start conversion" pin)
const int adcReadyPin = 3; //Interrupt from the ADC data (rising edge is trigger that ADC is done)
//On the teensy, we also have:
//MISO: pin 12
//CLK: pin 13
volatile unsigned int interrupt_counter;
volatile uint32_t ADCbyte1, ADCbyte2;
volatile int32_t ADCresult;
volatile uint8_t print_flag;
int loop_counter;
/************************************
SPI pins:
pin 12 = MISO
pin 13 = SCLK
************************************/
// set up the speed, mode and endianness of SPI devices:
SPISettings settingsADC(10000000, MSBFIRST, SPI_MODE2); //SPI_MODE2?
void setup() {
Serial.begin(9600);
pinMode(adcTriggerPin,OUTPUT);
analogWriteFrequency( adcTriggerPin, 10000);
pinMode(adcReadyPin, INPUT);
delay(1000);
// initialize SPI:
SPI.begin();
/* Interrupt attacments: (pin, callback func, edge or change) */
attachInterrupt(adcReadyPin, ADC_ready, RISING);
analogWrite(adcTriggerPin, 150); //Start the ADC converstion trigger
}
void loop()
{
loop_counter++;
Serial.print("Loops: ");
Serial.println(loop_counter);
Serial.print("ADCbyte1: ");
Serial.println(ADCbyte1);
Serial.print("ADCbyte2: ");
Serial.println(ADCbyte2);
Serial.print("ADC result: ");
Serial.println(ADCresult);
Serial.print("Interrupt counter: ");
Serial.println(interrupt_counter);
delay(1000);
}
void ADC_ready(void)
{
noInterrupts(); //Disable interrupts
//ALL variables in here must be volatile
interrupt_counter++;
for (volatile int time_waster = 0; time_waster < 100; time_waster++)
{
volatile int j = time_waster + 1;
}
SPI.beginTransaction(settingsADC);
ADCbyte1 = SPI.transfer(0);
ADCbyte2 = SPI.transfer(0);
ADCresult = (int32_t)((ADCbyte1 << 8) + ADCbyte2); // * 5.0f / 32768.0f;
SPI.endTransaction();
interrupts(); //Enable interrupts
}
When I debug my ADC result, I get an erroneous value of ADCresult = 65535. Here's the question: In my interrupt, I call on 2 SPI.transfer() transactions (8 bits + 8 bits = 16 bits in total, for the ADC result). In the oscilloscope capture, there are 3 blocks of SPI clocks (the blue trace). This tells me that 3 SPI transactions are happening, even though I only called for 2. How can this happen? I'm not sure why my SPI is "triggering" too many times.