NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » ACD sample rate

March 04, 2010
by norby31
norby31's Avatar

I'm running some audio into a pin assigned as ADC input. After I crunch some numbers and do a little conversion to binary bits I'm getting pretty low resolution output timewise. 1) Is the sample rate the same as the ADC clock rate? 2) How do I set sample rate? 3) My code isn't using any huge loops so I wonder what would cause the slowness? 4) Does writing to the LCD cause problems with sample rate? Enough Qs for now

March 05, 2010
by pbfy0
pbfy0's Avatar

writing to the LCD is quite slow, I think.

March 05, 2010
by bretm
bretm's Avatar

My understanding of the relationship between ADC clock rate and ADC sample rate: The ADC first charges a small capacitor to the voltage you're measuring. The rate at which this capacitor charges depends on the input impedance. Once it's charged, the ADC clock is used to coordinate several comparisons with the capacitor voltage, e.g. 2.5V? Lower. 1.25V? Higher. 1.875V? Higher. 2.1875V? Lower. Etc., one comparison per clock cycle, ten comparisons total.

The datasheet says a normal conversion takes 13 ADC clock cycles. The sample-and-hold takes place during the first 3, and 10 are for comparisons.

If you take a measurement, write to the display, and then take another measurement, you're going to be as slow as the display. Instead you could use an interrupt to grab a measurement result, store it somewhere, and then immediately start another measurement. Then you can update the display in your main loop, and that's where you can do any averaging, min/max, or any other filtering you need.

Declare the intermediate storage variables as "volatile" so that the compiler doesn't optimize them out of your main loop, which may not have any code that would actually sets or changes those variables.

So it looks like if you want a sampling frequency of F_SAMP, the ADC clock has to be 13 * F_SAMP, so the ADC clock prescaler has to be F_CPU/(13 * F_SAMP). E.g. for 44100 samples per second and 14.7456MHz CPU clock, the prescaler division should be about 26. The closest you can choose is 16 (and you'd have extra time to kill) or 32 (which would be slower than 44100hz).

March 05, 2010
by norby31
norby31's Avatar

I took some extraneous code out (writing to LCD) and I can actually hear a tone coming out of it.

Post a Reply

Please log in to post a reply.

Did you know that you can make a spooky Halloween Jack-O-Lantern with a microcontroller? Learn more...