PDA

View Full Version : interrupt driven adc with multiple channel selection help ..



superchiku
04-09-2008, 11:34 AM
i wanted to test the interrupt routine of the avr adc in which i wrote a simple program where i selected 2 channels of admux and stored the adc converted values of each channel in 1 variable but i got problem, the values of the adc were not consistent , i mean if i change the value of 1 channel , the value for other channel would also change.

IN NORMAL CONVERSION MODE U JUST WAIT UNTIL THE ADIF BIT IS SET BUT SINCE HERE IT IS CLEARED BY HARDWARE I DONT KNOW HOW TO KNOW IF A CONVERSION HAS BEEN MADE AND AN INTERRUPT HAS BEEN CALLED , PLZZ HELP ME SOLVE THIS PROBLEM

the code is like this



# include<stdio.h>
#include <avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>

unsigned int a,b,c;

int main(void)
{
ADCSRA|=(1<<ADEN)|(1<<ADATE);//enable adc & auto trigger
ADCSRA|=(1<<ADPS1)|(1<<ADPS0);//set prescaler to 8
ADCSRA|=(1<<ADIE);//enable interrupts
SFIOR|=_BV(ADTS2)|_BV(ADTS1)|_BV(ADTS0);/free runnin
sei();//enable global interrupts
ADCSRA|=(1<<ADSC);//start adc conversion
while(1)
{

c=1;


ADMUX=0x61;//select channel 1


c=2;

ADMUX=0x62;//select channel 2

}

return(0);

}

ISR(ADC_vect)
{

if(c==1)
{
a=ADCH;//write adc value of channel1 in a
}

if(c==2)
{
b=ADCH;//write adc value of channel2 in b

}
}




iam using an atmega16 with internal 1mhz clock