0
\$\begingroup\$

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 ?

\$\endgroup\$
  • 1
    \$\begingroup\$ Perhaps puting R1 from Vcc to LDR is a better solution, LDR grounded directly. \$\endgroup\$ – Marko Buršič Nov 16 '15 at 8:27
  • \$\begingroup\$ Proteaus giving me this error. [AVR AD CONVERTER] Referance value = 0 \$\endgroup\$ – charith Nov 16 '15 at 8:59
1
\$\begingroup\$

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*/
}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

try adding the header

#define F_CPU 1000000UL // (or 8000000UL or 16000000UL depending upon what
                        //frequency you want your Atmega32 to run at)

before or after you include util/delay.h Proteus requires your Atmega's frequency to be determined beforehand.

Comment if it still doesn't work out!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

edit: not an arduino board. AVCC and AVREF still need to be hooked up, however.

An arduino board has more electronics on it than just a single ATMEGA and an LED (although not too much more).

Make sure that your AVCC and AVREF pins are hooked up to the correct voltages, and also make sure your XTAL pins are connected to a crystal and some capacitors.

See if you can find a schematic for your particular Arduino - that should show up exactly how to set up the chip's voltage, ground, and crystal pins.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks but I cant use Arduino. I have to do this using Atmega32 alone \$\endgroup\$ – charith Nov 16 '15 at 8:34
  • \$\begingroup\$ Have you done a 'hello-world' in your simulator? Maybe loaded a simple program that just flashes the LEDs a few times and then terminates? \$\endgroup\$ – Nicholas Clark Nov 16 '15 at 8:40
  • \$\begingroup\$ Yes I did and part of my simulation working perfectly. (relay part) but I want to measure LDR value and turn OFF/ON relay. to do this I have to do ADC. program compiles successfully but when I run it in Proteaus I got an error. I have to make this before tomorrow night. Could you please help me sir :( \$\endgroup\$ – charith Nov 16 '15 at 8:48
  • \$\begingroup\$ Proteaus giving me this error. [AVR AD CONVERTER] Referance value = 0 \$\endgroup\$ – charith Nov 16 '15 at 8:56
  • \$\begingroup\$ Try connecting AREF and AVCC to the right values. I'm guessing you don't care what the values are, so try connecting both of them to 5V and see if your circuit runs then. \$\endgroup\$ – Nicholas Clark Nov 16 '15 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.