int ad_convert(void);
void setup() {
volatile char *dira,*ADMX;
volatile int value;
dira=0x30; //port f ddr
*dira=0x00; //set as input
ADMX=0x7C; //address of ADMUX reg of ADC
*ADMX=0x00;//AREF,data is right adjusted,ADC0 is selected
Serial.begin(9600);
while(1)
{
value=ad_convert(); //ADC function is called and the result is stored in variable value
Serial.print(value); //result from ADC is printed
Serial.print("\t");
delay(2500);
}
}
int ad_convert(void)
{
volatile unsigned char ainlow,*adsl,*adsh;
volatile char *adcsa;
volatile unsigned int ain,ainhigh;
adcsa=0x7a; //ADC controller and status reg address
*adcsa=0xc7; //ADC is enabled, conversion started, trigger mode off,prescaler=128
adsl=0x78; //8 bit result at lower reg
adsh=0x79; //2 bit result at higher reg
while((*adcsa & (1<<4))==0); //when conversion is done ADIF interrupt flag is set.
ainlow=*adsl; //lower 8 bit is copied to variable
ainhigh=*adsh;// upper 2 bit is copied to variable
ainhigh=ainhigh<<8; //2 bits are left shifted to 9th and 10 position
ain = ainhigh + ainlow; // upper and lower bits are added
return(ain); //result is returned
}
void loop() {
}