I had a look at some more examples, but I still cannot figure out, why some people are using ADC in free running mode and setting timers as well. What is the purpose of that?
Here is a sample code. IT IS NOT MY CODE. I just found such one.
//timer library
#include <ARMtimer.h>
#undef HID_ENABLED
int result = 0;
int sampleCount = 0;
int sampleFrequency = 100000;
// unsigned long sampleTime = 1000; //in millis
const int numSamples = 2048;
int resultArray[numSamples];
unsigned long programtime = 0;
bool once = false;
//setup adc function
void prepareADC(){
pmc_enable_periph_clk(ID_ADC);
adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP_FAST);
adc_enable_channel(ADC, ADC_CHANNEL_7);
ADC->ADC_MR |= 0x80; // free running
adc_enable_interrupt(ADC, ADC_IER_DRDY);
NVIC_EnableIRQ(ADC_IRQn);
adc_start(ADC);
}
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);
}
}
volatile void receivedata()
{
if (sampleCount < numSamples){
resultArray[sampleCount] = result;
}
sampleCount = sampleCount + 1;
if (sampleCount == numSamples){
sampleCount = 0;
}
}
void sendData(){
for (int j=0; j<= numSamples; j++){
Serial.print(j);
Serial.print(": ");
Serial.println(resultArray[j]);
}
}
void setup() {
Serial.begin(115200);
prepareADC();
programtime = millis();
}
void loop() {
delay(2000);
int now = micros();
startTimer(TC1, 0, TC3_IRQn, sampleFrequency, receivedata);
sendData();
stopTimer(TC3_IRQn);
int dur = micros() - now;
Serial.print("dur: ");
Serial.println((float)(dur/numSamples));
sampleCount = 0;
}