Hey folks,
I had some time today to wade through the SAM3X Datasheet and figured out how to get the ADC up and running. I thought I would share, in case it's useful to others. Please don't make fun of my limited skills.
uint32_t result = 0;
time_t elapsedtime = 0;
time_t totaltimeelapsed = 0;
int counter = 0;
void ADC_Handler(void) {
//Check the ADC conversion status
if ((adc_get_status(ADC) & ADC_ISR_DRDY) == ADC_ISR_DRDY)
{
//Get latest digital data value from ADC and can be used by application
result = adc_get_latest_value(ADC);
// Serial.println("test");
}
}
void setup(){
Serial.begin(115200);
adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP_FAST);
adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1);
//Port 0 = ADC_Channel_7
adc_enable_channel(ADC, ADC_CHANNEL_7);
NVIC_EnableIRQ(ADC_IRQn);
adc_set_comparison_channel(ADC, ADC_CHANNEL_7);
adc_set_comparison_mode(ADC, ADC_EMR_CMPMODE_IN);
adc_set_comparison_window(ADC, 0, 3300);
adc_enable_interrupt(ADC, ADC_IER_DRDY);
adc_configure_trigger(ADC, ADC_TRIG_SW, 0);
}
void loop(){
while (totaltimeelapsed <= 1000){
// counter = counter + 1;
time_t capturetimestart = millis();
// time_t timestart = micros();
adc_start(ADC);
// time_t timeend = micros();
// elapsedtime = timeend - timestart;
// Serial.print(counter);
// Serial.print(" , ");
Serial.println(result);
// Serial.print(" , ");
// Serial.print(elapsedtime);
// Serial.print(" , ");
time_t capturetimeend = millis();
time_t capturetimeelapsed = capturetimeend-capturetimestart;
totaltimeelapsed = totaltimeelapsed + capturetimeelapsed;
// Serial.println(totaltimeelapsed);
}
}
I pulled 1000 millis of data and plotted in Matlab (see attached). It looks like it's pulling about 2.5k/s.
One question: Does this pull at a consistent rate (as it would with a timer interrupt)? Or is this dependent on the code in the loop?
Cheers,
Mike