## Setting exactly 1000 samples for multi channel ADC

16 posts / 0 new
Author
Message

Dear Experts,

Hi again,

In a project ( suppose an ECG project) that is based on an arduino leonardo board, I am in need of capturing exactly 1000 samples per second for more than 3 ADC channels.

But as every ADC process needs exactly 13 ADC clocks I have to change the oscillator of board to get 1000 samplws and it is not a good idea.

I thinks there is other way based on timers. In that I need to set a timer to produce interrupts 1000 times per second and in interrupt routine I start an ADC process and in interrupt of ADC I get samples.

I need exact sample rate because DSP algorithms need it as an input argument.

Now my questions:

1. Am I in the right way? or other standard routine shall be used?

2. Is there any source code that I could get hints from it?

BR

Last Edited: Tue. Mar 31, 2015 - 04:52 AM

Yes, you are on exactly the right track with the timer and interrupts.

Tom Pappano
Tulsa, Oklahoma

Last Edited: Tue. Mar 31, 2015 - 06:03 AM

1000 samples per second on 3 channels is 3000.   i.e. 333us is available per sample.

If the ADC is clocked at 200kHz,  it is 5us per 'ADC clock'.    You actually need 25 'ADC clocks' for a fresh conversion.  25 x 5us = 125us

Since you need a little pause when you change the multiplexer channel,   everything looks possible within your time frame.

David.

If we use interrupt based multi channel ADC then 13 clocks is necessary for each Digital to Analog Conversion or 25 clocks?

Does it matter?

You have sufficient time either way.  i.e. 333us

The data sheet explains how the ADC and the multiplexer works.

When you change channel,   the sample-and-hold capacitor has to charge to the new input voltage.   i.e. it is wise to allow 5us,  before you start the conversion on a new channel.

David.

I just picked an AVR datasheet at random. It said:

A normal conversion takes 13 ADC clock cycles. The first conversion after the ADC is switched
on (ADEN in ADCSRA is set) takes 25 ADC clock cycles in order to initialize the analog circuitry.

Don't know about you but the way I read that I would only be concerned about 25 clocks if I was to keep switching ADEN on and off. I'm not sure why anyone would choose to do this so I think it's pretty safe to assume it is 13 clocks per "normal" conversion (from ADSC being set to 1 before it returns to 0). Just turn ADEN on, if it really concerns you make one conversion and ditch the result to get the first 25 clocks out of the way then expect to get a value reading every 13 clocks from then on. As David says at 200kHz you are looking at about 65us per conversion so a sample rate of 15.3kHz.

Amazing. Me & Mr Lawson also agree on this point.

Imagecraft compiler user

Don't know about you but the way I read that I would only be concerned about 25 clocks if I was to keep switching ADEN on and off.

You are correct, except that I believe that it also applies to changing the reference source.

Regards,
Steve A.

The Board helps those that help themselves.

Alright--let's start over...

I am in need of capturing exactly 1000 samples per second for more than 3 ADC channels.

How many is "more than three"?  4? 14? 44?

In a project ( suppose an ECG project)

Do we know that these signals have good "drive"?  IME (and that of others) is that relatively high impedance signals (e.g. thermistor) might well need double-convert on each channel to get good results.

While the 200kHz and x13 and 15ksps is often tossed about, in the real world it isn't always that easy.  The system clock doesn't always divide down to exactly 200kHz ADC clock.  The mentioned Leonardo runs at 16MHz.  We have a choice of powers of two for the prescaler.  /64 gives 250kHz ADC clock; /128 gives 125kHz ADC clock.  In my apps with magic crystals I usually end up at 57.6kHz or 115.2kHz.  125kHz x13 is ~100us.  Plus in a real app there might be latency and ISR time and ...  I usually figure about 200us sustained rate.  That would be about 5 channels comfortably to complete in 1ms, or about 5ksps sustained rate.

Assuming a non-trivial app, let's look at e.g. 5 channels, 1000 conversions per second each, so 5000 sample results to be entered into the analysis engine.

Two bytes each; so 10000 bytes of SRAM needed for one second of storage.  (I'll wager the "DSP algorithms" use some memory, too...)  Leonardo is based on '32U4:

"> – 1.25/2.5K Bytes Internal SRAM (ATmega16U4/ATmega32U4)

Now, how are you going to put 10 kilograms of stuff into a 2.5 kilogram box?

Even three channels doesn't fit.  "If it doesn't fit, you must acquit."

Assuming one can fit in the DSP processing along with the data acquisition, perhaps the loop can be closed e.g. every tenth of a second?

Given the above, should we even go further?  But if I wanted several channels at a more-or-less constant sample time of 1ms, I'd use a timer and auto-trigger to kick off a round of conversions.  ADC-complete ISR stores the result and kicks off the next channel.  At the end of a round, it clears the auto-trigger flag and sets up for the next millisecond.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

I come to the conclusion to use 13 clock conversion without turning ADC circuit on and off repeatedly.

Dear @theusch

ATMEGA32U4 has a USB interface to send captured data to a HOST side program thus DSP algorithms could be run on the HOST.

I selected ATMEGA32U4 because it has a nice usb interface and also a very nice library for USB transaction ( I mean LUFA).

ECG devices have 3 to 12 leads.

The simplest one has 3 leads and I started to implement it.

I count on the following sentence from atmega32u4 datasheet:

"Alternatively, setting the ADHSM bit in ADCSRB allows an increased ADC clock frequency at the expense of higher power consumption."

I do not how much it increases the ADC sample rate but I hope it increases a lot that I could get 12 leads work properly.

BR

I Ctrl-F'd the datasheet for ADHSM. The amount of info seems limited but I would doubt that if you want to maintain 10 bit resolution that it is really possible to run the ADC above 200kHz (you can run it faster at the expense of bit resolution of course). At 200kHz and using 13 clocks a conversion take 65us. That is 15,300 samples per second. If you really have 12 leads and want 1,000 samples per second that is just 12,000 so, in theory there's capacity enough to achieve this but you are going to have to be quite clever about using the 65us between each result to process the previous ones.

Personally I wouldn't take the risk with something like this. Running things close to the design limit means that if there's anything you have over-looked in your design you may actually be bumping up against the top limit.

Xmega's have far higher spec ADC's would you not be better off exploring those? Is it cost or package size or availability of USB or need for 5V Vcc or something else that is driving your need to use 32U4? Apart from the 5V Vcc it looks on the surface as if Xmega may be better on most scores.

Dear Clawson

The reason that I have not used Xmega series  yet is just lack of information.

If you happen to know any arduino board based on Xmega series which has at least 8 ADC channels and a USB interface please let me know.

BR

Why does it need to be Arduino? Atmel produce a number of "Xplained" demo/eval boards you can use and the recent ones (probably called "Xplained Pro") even have an Arduino compatible I/O pin out.

Or were you suggesting you wanto to do the final development in Arduino code?

Arduino is OK for knocking up a quick prototype but it's not normally a solution for a final commercial solution.

clawson wrote:
...  "Xplained Pro" even have an Arduino compatible I/O pin out

I don't think so?

But you can get an adaptor:

http://uk.rs-online.com/web/p/pr...

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

I don't think so?

You are right, it was a vague memory. It's the Xplained Mini (D10, 328P, 168P) and Xplained Ultra (A5D3, A5D4, V71) that have Arduino footprints...

http://www.atmel.com/products/mi...