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 ?

asked Nov 16, 2015 at 7:10
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Perhaps puting R1 from Vcc to LDR is a better solution, LDR grounded directly. \$\endgroup\$ Commented Nov 16, 2015 at 8:27
  • \$\begingroup\$ Proteaus giving me this error. [AVR AD CONVERTER] Referance value = 0 \$\endgroup\$ Commented Nov 16, 2015 at 8:59

3 Answers 3

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*/
}
answered Jul 30, 2018 at 4:13
\$\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!

answered Nov 18, 2015 at 17:26
\$\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. (削除ここまで)

answered Nov 16, 2015 at 7:46
\$\endgroup\$
6
  • \$\begingroup\$ Thanks but I cant use Arduino. I have to do this using Atmega32 alone \$\endgroup\$ Commented Nov 16, 2015 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\$ Commented Nov 16, 2015 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\$ Commented Nov 16, 2015 at 8:48
  • \$\begingroup\$ Proteaus giving me this error. [AVR AD CONVERTER] Referance value = 0 \$\endgroup\$ Commented Nov 16, 2015 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\$ Commented Nov 16, 2015 at 9:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.