#include<avr/io.h>
#include<avr/interrupt.h>
#include"lcd.h"
#define ADC_CHNL0 0
#define ADC_CHNL1 1
#define ADC_CHNL2 2
#define ADC_CHNL3 3
//volatile unsigned int ADC_Channel = ADC_CHNL0,ADC_Ready = 0; //Global
//volatile unsigned int ADC_Data = 0;
int main(void)
{
initsyst();//initialise ADC & LCD
while(1)
{
ADCData(ADC_CHNL0);
ADCData(ADC_CHNL1);
ADCData(ADC_CHNL2);
_delay_ms(500);
lcd_cmd(0x01);
}
}
void initsyst()
{
lcd_init();
initADC();
}
void initADC()
{
// sei();//Global interrupt enable
//Vref=2.56 internal, ADC Left Adjust,channel 0(LDR on board)
ADMUX |= ((1<<REFS1) | (1<<REFS0)) ;
//Enable ADC,free running,start convertion,ADC inrtpt enable,128 prescalar,
ADCSRA |= ((1<<ADEN) | (1<<5) | (1<<ADIE) |(1<<ADPS2) |(1<<ADPS1) |(1<<ADPS0));
}
void ADCData(unsigned char chnl)
{
ADMUX = (ADMUX & 0xE0) | chnl;
ADCSRA |= (1<<ADSC);//start the conversion
while(!(ADCSRA & (1<<ADIF))); //wait for Conversion complete flag to raise
switch(chnl)
{
case ADC_CHNL0://data received from channel ADC0 (LDR)
lcd_showvalue1(ADC,8);
break;
case ADC_CHNL1://X
lcd_showvalue1(ADC,1);
break;
case ADC_CHNL2://Y
lcd_showvalue2(ADC,1);
break;
default:
break;
}//switch...case ends
}