# Can't trigger AVR ADC Interrupt

I am using an Atmel's XPlained mini board with Atmega328PB. I am trying to use ADC interrupt, however I can't get it to fire. Here's the code that I have a problem with:

#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>

PORTC ^= (1<<PINC2);
}

int main(void)
{
DDRC |= (1<<PINC2); //pin 2 as output
DDRC &= ~(1<<PINC3); //adc pin as input
sei();
ADMUX |= (1<<REFS0); //reference voltage VCC

while (1)
{
}
}


As far as I understand the code should enable a constant conversion in a free running mode with the frequency of 16MHz/128, and each time the conversion is completed the ADC_vect should fire toggling PIN2 of Port C. The problem is it doesn't. I am guessing I am not initializing the ADC correctly, however I can not pin point were the problem is.

EDIT: After pointing out that the adc was not being run in a free running mode I cleared the ADTS bits. However I still couldn't toggle the pin. I think something else is at fault here.

You've written

ADCSRA |= (1<ADEN); //adc enable


And I think it should be

ADCSRA |= (1<<ADEN); //adc enable


Which seems plausible since the error you've come across is that it never does anything.

• That's absolutely right, too bad that such a mistake doesn't trigger any kind of warning. – Anthropomorphous Dodecahedron Dec 20 '18 at 7:16
• @AnthropomorphousDodecahedron Well 1<ADEN is evaluated as if it was inside a if statement. I don't know what value ADEN actually is, but if it is, say equal to 2, then 1<ADEN becomes true which becomes 00000001 which is then OR:ed with ADCSRA. - So it shouldn't generate an error nor a warning, it is perfectly legal code. – Harry Svensson Dec 20 '18 at 8:12

See table 24-6 of the datasheet. You are setting bits ADTS1 and ADTS0 high (leaving ADTS2 as zero). It means that the trigger signal is "Timer/Counter0 Compare Match A". You are not doing anything to configure that timer (and the reset state of the timer is simply "stopped").