I can't implement ADC interrupt

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have already read some of the great tutorials on ADC-interrupt. So I was very excited to implement it. But I can't compile my code. It shows several errors. So I have to revert back to my old working code. I don't place here my entire code but two important functions. I guess I don't properly understand interrupt yet.

Working code:

void adc_init(void)
{
	ADMUX |=(1<<REFS0);
	ADCSRA |=(0<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADFR)|(1<<ADEN);
}

uint16_t adc_read(void)
{
	
	ADCSRA |=(1<<ADSC);
	while(ADCSRA & (1<<ADSC))
	{
		return(ADC);
	}
}

Not working Interrupt implemented code:

uint16_t adc_read(void)
{
	ADCSRA |=(1<<ADIE);
        sei();
	ADCSRA |=(1<<ADSC);
	ISR(ADC_vect)
	{
	  return(ADC);
	}
}

uC=ATmega8L, uC freq=1MHz, Prescalar=8, AVR Studio 4.19

Plz tell me what I have done wrong when implementing interrupt?

When you do ask questions, you may look stupid.
When you do NOT ask questions, you will STAY stupid.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should not put an ISR INSIDE a function, keep it separate.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And if you try to "return" something from the ISR, then very likely you haven't fully understood the concept of interrupts in general.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your reply @both of you

When you do ask questions, you may look stupid.
When you do NOT ask questions, you will STAY stupid.