Speed ADC

Maple Mini, Maple Rev3, Maple Rev 5 and Maple Ret 6, iTead Maple etc
Post Reply
alex---1967
Posts: 2
Joined: Sun Jan 10, 2016 2:04 pm
Location: Russia
Contact:

Speed ADC

Post by alex---1967 » Sun Jan 10, 2016 4:34 pm

Hello! Can anyone suggest - how can I increase the speed of ADC ? When I use standard IDE then the conversion rate is about 7 microseconds.

User avatar
mrburnette
Posts: 1803
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Speed ADC

Post by mrburnette » Sun Jan 10, 2016 4:50 pm

alex---1967 wrote:Hello! Can anyone suggest - how can I increase the speed of ADC ? When I use standard IDE then the conversion rate is about 7 microseconds.
Much effort went into the Pig'O'Scope to enhance ADC,
This may be helpful:
http://www.stm32duino.com/viewtopic.php?t=107&start=40

From the old Maple forum: http://forums.leaflabs.com/topic.php?id=154
The primary reason I am interested in switching from the Arduino to the Maple is the improved ADC resolution of the Maple (the Arduino has 10-bit ADC while the Maple has 12-bit ADC). The faster ADC sample rate of the STM32 chip is a nice bonus.

In http://forums.leaflabs.com/topic.php?id=149#post-942 gbulmer said:

To be fair, 850K samples/second, on each of two or three ADC's, is so much better than an Arduino's <10k samples/second

This statement is correct, because in section 11.2 of the STM32 reference manual (file 13902.pdf) it says the ADC conversion time is "1.17 microseconds at 72 MHz".

Good luck,

Ray

User avatar
mrmonteith
Posts: 112
Joined: Wed Aug 26, 2015 3:11 pm
Location: Greenville, North Carolina
Contact:

Re: Speed ADC

Post by mrmonteith » Sun Jan 10, 2016 9:02 pm

There's been several write-ups about Arduino ADC and increasing the sample rate. This is one:
http://www.microsmart.co.za/technical/2 ... duino-adc/

The Arduino libraries do have their limits and several have written some libraries to get beyond those limitations. You can opt for buying more speed or learning how to squeeze the most out of what you have. Nothing wrong with either way.

Another thing is with the interrupts on the Arduino the conversions aren't exactly timed well. So if you're wanting precise timing that another issue that could cause problems depending on your use. Just so you're aware of it.

It's all fun to learn though. Enjoy

Michael

robca
Posts: 54
Joined: Sat Nov 21, 2015 11:55 pm

Re: Speed ADC

Post by robca » Sun Jan 10, 2016 11:35 pm

If you use DMA and ADC directly, you can easily sample at 1MHz on two parallel channels (and have up to 16 channels sequenced in parallel)

If you can provide more details on what type of conversion you are trying to achieve (speed, how many channels, etc), I could very likely provide sample code (I spent a lot of time figuring out how to make simultaneous dual conversion with DMA work properly)

alex---1967
Posts: 2
Joined: Sun Jan 10, 2016 2:04 pm
Location: Russia
Contact:

Re: Speed ADC

Post by alex---1967 » Mon Jan 11, 2016 7:02 pm

Thank you all.

I read "STM32-RM0041_Reference_manual" , application note "How to get the best ADC accuracy...",
Language Reference of Maple IDE, namely "adc.h (low-level ADC support)" , but I do not understand, because there are no examples(samples) of the code.

Now, after reading the examples(samples) I understood how to do it.


Instead:

void setup()
{
......
pinMode(3, INPUT_ANALOG);
......
}
void loop()
{
..........
for (m=0; m < 100; m++)
{
s_array[m] = analogRead(3);
}
................
}







I write:


#include "adc.h"
........
void setup()
{
......
pinMode(3, INPUT_ANALOG);
......
}
void loop()
{
........
adc_set_sample_rate(ADC2, ADC_SMPR_1_5);

for (m=0; m < 100; m++)
{
s_array[m] = adc_read(ADC2, 8);
}
................
}


After that, sampling time of 2.4 microseconds already.

User avatar
ahull
Posts: 1597
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Speed ADC

Post by ahull » Mon Jan 11, 2016 10:24 pm

The Pig-o-Scope sketch also shows how to use DMA and 2 channels for the maximum speed available. We managed to get 1.08 uS per sample or 925,925.925 samples per second, i.e. just under 1MHz which is as fast as the little pigs legs will carry it. :D
You can find it on github here.
- Andy Hull -

robca
Posts: 54
Joined: Sat Nov 21, 2015 11:55 pm

Re: Speed ADC

Post by robca » Wed Jan 13, 2016 5:17 pm

alex---1967 wrote:Thank you all.

I read "STM32-RM0041_Reference_manual" , application note "How to get the best ADC accuracy...",
Language Reference of Maple IDE, namely "adc.h (low-level ADC support)" , but I do not understand, because there are no examples(samples) of the code.

Now, after reading the examples(samples) I understood how to do it.


Instead:

void setup()
{
......
pinMode(3, INPUT_ANALOG);
......
}
void loop()
{
..........
for (m=0; m < 100; m++)
{
s_array[m] = analogRead(3);
}
................
}







I write:


#include "adc.h"
........
void setup()
{
......
pinMode(3, INPUT_ANALOG);
......
}
void loop()
{
........
adc_set_sample_rate(ADC2, ADC_SMPR_1_5);

for (m=0; m < 100; m++)
{
s_array[m] = adc_read(ADC2, 8);
}
................
}


After that, sampling time of 2.4 microseconds already.
You can't get decent sampling speed using adc_read() in a for loop. The Arduino libraries overhead slows you down (the conversion itself is done at the speed you select, but the loop and calling of adc_read() is where it spends all the time. You need to directly set the registers for DMA and ACD. The PigScope is a good starting point, albeit it samples a single channel using two ADC (to speed up conversion), and there's a lot of extra code to look at before understanding the specifics of the conversion.

My code is for dual conversion (two channels in parallel), so wouldn't help as much as the PigScope for your specific needs

Post Reply