Code for ADC interfacing of AVR



#include <avr/io.h>

int main(void)
{
int adcValue;                                               //Variable to hold ADC value;

        DDRD = 0xff;                                        //Make all PIN of PORT D output;
        PORTD=0x00;                                       //Pull all pin low;

        ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0);
        //ADC feature enabled with prescaler of 32 but conversion is not started here.
       
        
        while(1)
        {
ADMUX = 0x05;                           //ADC5 (PIN 28) is used for conversion
ADCSRA |= (1<<ADSC);              //Start ADC conversion

                while(ADCSRA&(1<<ADSC));     //wait till ADSC bit is high (till conversion completes)
                adcValue=ADCW;                         //converted value is stored in variable "adcValue"
               
                if(adcValue>768)                          //switch pins according adc value
PORTD  =  0x01;               //PORTD_0 High
else if(adcValue>511)
PORTD  =  0x02;               //PORTD_1 High
else if(adcValue>255)
PORTD  =   0x04;              //PORTD_2 High
else if(adcValue>1)
PORTD  =   0x08;              //PORTD_0 High
}
return 0;
}

No comments:

Post a Comment