0
\$\begingroup\$

I am using an ATTINY44 in my current project. As I need to do some time sensitive switching I would like to avoid any unnecessary delay. To control another device (not time sensitive) I need to read an analog value though. I read several examples (including I checked below code from the Arduino cores (for analogRead()) and most are setting the ADSC bit and then waiting with a while loop for the conversion to finish. I also read about the free running mode in the datasheet and noise reduction mode (conversion during idle time).

So now my questions:

  • Is this while loop just bad coding or are they doing it for noise reduction (because in free running mode, also other code would be executed during conversion)?

  • If I put the check for ADSC to be cleared in my main loop, what would be the advantaged / disadvantages?

  • If this method is for for noise reduction, how much added inaccuracy (maybe a ballpark figure) can I expect - I can live with +-2 or 3LSB?

Code from wiring_analog.c:

// start the conversion
sbi(ADCSRA, ADSC);

// ADSC is cleared when the conversion finishes
while (bit_is_set(ADCSRA, ADSC));
\$\endgroup\$
2
\$\begingroup\$

-Is this while loop just bad coding or are they doing it for noise reduction (because in free running mode, also other code would be executed during conversion)?

The while loop is for waiting until the ADC has finished. Most ADC conversions depend on ADC configurations which are set prior to the start of conversion. Such as number of bits, speed of conversions, power mode and ADC clock speed. So it is convenient to wait until the hardware says it is done.

I don't see a problem if you wanted to execute code prior to checking for completion of the ADC conversion.

ADC noise is a very broad subject. If you have a specific question, ask. Otherwise I would suggest implementing your design based on the particular processor's application notes to see if the results are acceptable.

-If I put the check for ADSC to be cleared in my main loop, what would be the advantaged / disadvantages?

I don't see any problem with that.

-If this method is for for noise reduction, how much added inaccuracy (maybe a ballpark figure) can I expect - I can live with +-2 or 3LSB?

More often what I see as noise in embedded processor application comes from outside the processor. For instance, if the processor is activating high current bright LEDs during and ADC conversion.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.