#include <mega32.h>
#include <delay.h>
#include <alcd.h>
#include <stdio.h>
#define ADC_VREF_TYPE 0x00
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
void LM35_read(void);
void Voltage_read(void);
int a,m;
unsigned char buffer[16],i,j,k;
flash char code[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
void main(void)
{
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0x81;
    lcd_init(16);
    DDRA=0X0;
    DDRB=0XFF;
    DDRC=0XFF;
    DDRD=0XFF;

    while (1)
        {
            LM35_read();
            Voltage_read();

        }
}
void LM35_read(void)
{
    a=read_adc(0);
    a=a/2.04; 
    lcd_clear();
    sprintf(buffer,"temprature:%u ",a);
    lcd_puts(buffer);
    i=a/100;
    j=a%100;
    k=j%10;
    j/=10;  
    for(m=0;m<500;m++)
    {   
        if(i!=0)
        {
            PORTC=0X01;
            PORTD=code[i];
            delay_ms(2);
        }
        PORTC=0X02;
        PORTD=code[j];
        delay_ms(2);
        PORTC=0X04;
        PORTD=code[k];
        delay_ms(2); 
    }     
}

void Voltage_read(void)
{
    float temp;
    a=read_adc(1);
    temp=a/30.909; 
    lcd_clear();
    sprintf(buffer,"Voltage:%f ",temp);
    lcd_puts(buffer);
    temp*=10;    
    i=temp/100;
    j=(int)temp%100;
    k=j%10;
    j/=10; 
    for(m=0;m<500;m++)
    {    
        if(i!=0)
        {
            PORTC=0X01;
            PORTD=code[i];
            delay_ms(2);
        }
        PORTC=0X02;
        PORTD=(0x7f&code[j]);
        delay_ms(2);
        PORTC=0X04;
        PORTD=code[k];
        delay_ms(2); 
    }        
}