Interfacing LDR with atmega32

avrcldrmicrocontrollerproteus

I have to make LDR based light switch that turn on lights in per-defined light condition. To do this I have to use atmega32 Micro controller. I follow some tutorials and write some codes. the code compile in Atmel studio without any issues but when I run the code using Proteaus simulator code doesn't work

This is the code

#include <avr/io.h>
#include <util/delay.h>

#define LTHRES 500

// initialize adc
void adc_init()
{
    // AREF = AVcc
    ADMUX = (1<<REFS0);
    
    // ADC Enable and prescaler of 128
    // 16000000/128 = 125000
    ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}

// read adc value
uint16_t adc_read(uint8_t ch)
{
    // select the corresponding channel 0~7
    // ANDing with '7' will always keep the value
    // of 'ch' between 0 and 7
    ch &= 0b00000111;  // AND operation with 7
    ADMUX = (ADMUX & 0xF8)|ch;     // clears the bottom 3 bits before ORing
    
    // start single conversion
    // write '1' to ADSC
    ADCSRA |= (1<<ADSC);
    
    // wait for conversion to complete
    // ADSC becomes '0' again
    // till then, run loop continuously
    while(ADCSRA & (1<<ADSC));
    
    return (ADC);
}

int main()
{
    uint16_t adc_result0, adc_result1;
    char int_buffer[10];
    DDRC = 0x01;           // to connect led to PC0
    
    // initialize adc and lcd
    adc_init();
    

    
    _delay_ms(50);
    
    while(1)
    {
        adc_result0 = adc_read(0);      // read adc value at PA0

        
        // condition for led to glow
        if (adc_result0 < LTHRES)
        PORTC = 0x01;
        else
        PORTC = 0x00;
        
        
    }
}

And this is a screenshot of proteaus simulation.

enter image description here

I got this error

[AVR AD CONVERTER] Referance value = 0

I only have basic knowledge of programming micro-controllers and simulation using proteaus. can someone please help me how should I configure this ?

Best Answer

Make sure u connect AVcc with 5v

and my code for ADC Config

void ADC_Init()
{
    DDRA=0x0;               /* Make ADC port as input */
    ADCSRA = 0x87;          /* Enable ADC, fr/128  */
    ADMUX = 0x40;           /* Vref: Avcc, ADC channel: 0 */

}

int ADC_Read(char channel)
{

     int Ain,AinLow;

     ADMUX=ADMUX|(channel & 0x0f);  /* Set input channel to read */

     ADCSRA |= (1<<ADSC);           /* Start conversion */
     while((ADCSRA&(1<<ADIF))==0);  /* Monitor end of conversion interrupt */

     _delay_us(10);
     AinLow = (int)ADCL;                /* Read lower byte*/
     Ain = (int)ADCH*256;           /* Read higher 2 bits and 
                                    Multiply with weight */
    Ain = Ain + AinLow;             
    return(Ain);                    /* Return digital value*/
}