0
\$\begingroup\$

I am using an ATmega8A for ADC conversion. I start ADC conversion by setting ADSC bit and wait till it clears by hardware to detect the end of conversion. I saw some people checking the ADIF bit to detect the end of conversion.

ADCSRA |= (1 << ADSC); // start ADC conversion
while (ADCSRA & (1 << ADSC)) ; // wait for the end of conversion

or

ADCSRA |= (1 << ADSC); // start ADC conversion
while (!(ADCSRA & (1 << ADIF))) ; // wait for the end of conversion
ADCSRA |= (1 << ADIF); // clear the flag

Of course, both of them work fine; I want to know which one occurs first, ADSC=0 or ADIF=1? And which one is better to use? The datasheet says "ADSC will read as one as long as a conversion is in progress. When the conversion is complete, it returns to zero." about ADSC and "This bit is set when an ADC conversion completes and the Data Registers are updated." about ADIF. Does that mean the value of data registers may not be updated right after ADSC is cleared?
I ask all of these because if I use checking ADIF to detect the end of conversion I had to clear it and this means one extra word in the flash memory and one extra clock cycle in program execution in comparison with just checking ADSC.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

ADIF is the interrupt flag. Unless the associated interrupt is enabled, you need to specifically clear that flag as you’ve mentioned. ADSC is the conversion complete flag. Which is set first? Probably both as one would think there would be only one mechanism internally to flag completion. I’ve always used ADSC. ADIF is used by those that don’t read the datasheet.

\$\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.