ED201
May 2, 2018, 7:28pm
1
I have this loop
for (int i=0; i<2000; i++)
{ myArray = analogRead(A0);
}
I would assume that with 84 Mhz clock you could go very fast
I would like to take reading every 1 us which gives 1 Mhz clock.
it takes each time to execute myArray = analogRead(A0) like 20 us.
I know that it takes bunch of loops each time.
Is there anyway to speed it up
I saw somewhere code that you can actually read faster on analog pin but writing the value into array slows it down.
I would like to take reading every 1 us
Forget it. It takes about 100 times that long for the ADC to get a reading.
The CPU's clock speed has NOTHING to do with it.
I saw somewhere code that you can actually read faster on analog pin but writing the value into array slows it down.
Crap!
Mark
The maximum ADC sampling rate is 1 MHz, and of course you won't reach this frequency with an analogRead().
Here is an example sketch to sample in Free Running Mode (1 MHz):
volatile uint32_t LastConversion;
void setup()
{
adc_setup();
}
void loop()
{
}
/************* Configure adc_setup function *******************/
void adc_setup() {
PMC->PMC_PCER1 |= PMC_PCER1_PID37; // ADC power ON
ADC->ADC_CR = ADC_CR_SWRST; // Reset ADC
ADC->ADC_MR = ADC_MR_FREERUN // Free running mode
| ADC_MR_PRESCAL(0);
ADC->ADC_ACR = ADC_ACR_IBCTL(0b01); // For frequencies > 500 KHz
ADC->ADC_IER = ADC_IER_EOC7; // End Of Conversion interrupt enable for channel 7
NVIC_EnableIRQ((IRQn_Type)ADC_IRQn); // Enable ADC interrupt
ADC->ADC_CHER = ADC_CHER_CH7; // Enable Channels 7 = A0
}
void ADC_Handler () {
LastConversion = ADC->ADC_CDR[7]; // Reading ADC->ADC_CDR[i] clears EOCi bit
}
If you store several samples at a time, it's better using a PDC DMA to reach 1 MHz.