I have a device on ATMega16 microcontroller that is supposed to constantly send ADC measurement results over USART. The controller is operating at 16MHz with an external crystal and ADC prescaler is set to 128. I have tried two methods of performing ADC conversion and sending the results.
- The first method is based on interrupts
int main(void) {
...
while (true) {}
}
ISR(ADC_vect) {
USARTSendByte(ADCL);
USARTSendByte(ADCH);
ADCSRA |= 1 << ADSC;
}
- The second method is based on manually starting the conversion
int main(void) {
...
// main loop
while (true) {
if (adcEnabled) {
ADMUX |= channel;
ADCSRA |= (1 << ADSC);
while (!(ADCSRA & (1 << ADIF))) {
// Do nothing
}
ADCSRA |= (1 << ADIF); // Clear ADIF
USARTSendByte(ADCL);
USARTSendByte(ADCH);
}
}
}
I've performed a number of tests which consisted of sending 32 blocks of 512 bytes (16384 bytes in total) over USART and measuring transmission time. In the first case (interrupts) the average time was 1623.13ms
. The smallest result was 1535ms and the largest - 1712ms. In the second case the result was 1600.38ms
which is 22.75ms less than in previous case, with a smallest result of 1530ms and the largest result of 1679ms.
So the question is: will interrupts actually decrease ADC performance and why is that happening or the results of my tests were inconclusive?