#ifndef _MEGA32A_INCLUDED_
#include <mega32a.h>
#endif

#ifndef _MATH_INCLUDED_
#include <math.h>
#endif

#ifndef _DELAY_INCLUDED_
#include <delay.h>
#endif

#ifndef _MEGA_LIGHT_INCLUDED_
#define _MEGA_LIGHT_INCLUDED_
#endif

#pragma used+



#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 0
unsigned int adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
// Voltage Reference: AREF pin
#define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (0<<ADLAR))

// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
static unsigned char input_index=0;
// Read the AD conversion result
adc_data[input_index]=ADCW;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
   input_index=0;
ADMUX=(FIRST_ADC_INPUT | ADC_VREF_TYPE)+input_index;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
}

int LDR_init()
{
// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: Free Running
ADMUX=FIRST_ADC_INPUT | ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (1<<ADSC) | (1<<ADATE) | (0<<ADIF) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (0<<ADPS0);
SFIOR=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);

// Global enable interrupts
#asm("sei")

return 0;
}

float LDR_res()
{
float adcvolt,res,light;
adcvolt=(adc_data[0]*5.0)/1023.0;
res=(10*(5-adcvolt))/adcvolt;       
light=pow(10,(2.12-log10(res))/0.78);  
return light;
}

#pragma used-
