0
\$\begingroup\$

I have trouble reading data from sensors using ATmega16 in Proteus. We use either light sensors for which I use LDR in proteus, or LM35 temperature sensor for temperature reading. The problem is, doesn't matter which sensor I use, I get an "[AVR AD CONVERTER] Reference value = 0 " error when I run the Proteus simulation and a big reading on the display, I think it's the max value.

The problem is somewhere in reading the value from the ADC channel, namely CH0 or PA0 in my case. At least I think it is.

Here is the code for the temperature sensor:

#define F_CPU 7372800UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include "lcd.h"

void writeLCD(uint16_t adc) {
    lcd_clrscr();

    char adcStr[16];
    itoa(adc, adcStr, 10);
    lcd_puts(adcStr);
}

ISR(ADC_vect) {
    uint16_t temp = ((ADC * 5.0/1024) - 0.5) * 1000/10;

    writeLCD(temp);
}

int main(void) {
    DDRD = 0xff;

    TCCR1A = _BV(COM1B1) | _BV(WGM10);
    TCCR1B = _BV(WGM12) | _BV(CS11);
    OCR1B = 64;

    lcd_init(LCD_DISP_ON);

    ADMUX = _BV(REFS0);
    ADCSRA = _BV(ADEN) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1);

    sei();

    while (1) {
        ADCSRA |= _BV(ADSC);

        _delay_ms(1000);
    }
}

... and the proteus scheme: enter image description here

Light sensor code:

#define F_CPU 7372800UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include "lcd.h"

void writeLCD(uint16_t adc) {
    lcd_clrscr();

    char adcStr[16];
    itoa(adc, adcStr, 10);
    lcd_puts(adcStr);
}

int main(void)
{
    DDRD = 0xff;

    TCCR1A = _BV(COM1B1) | _BV(WGM10);
    TCCR1B = _BV(WGM12) | _BV(CS11);
    OCR1B = 64;

    lcd_init(LCD_DISP_ON);

    ADMUX = _BV(REFS0);
    ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1);

    while (1) {
        ADCSRA |= _BV(ADSC);

        while (!(ADCSRA & _BV(ADIF)));

        writeLCD(ADC);

        _delay_ms(100);
    }
}

Scheme:

enter image description here

Am not sure if I should change the ADMUX register or maybe add an external capacitor ? Because when we use the sensor irl we add a capacitor between the AREF and ground pins. Any suggestions are appreciated, thank you in advance.

P.S. The codes are a bit different cause we used different triggering mechanisms for data conversion but that is irrelevant here. Same error for both codes.

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

It looks like you haven't connected AVcc to Vcc. Technically, in real life, it always needs to be connected to Vcc, but the simulation might be forgiving. In this case, you've set AVcc as the reference, so it definitely needs to be connected and the simulator is picking up on that.

On a side note, you cannot connect AVcc to a voltage that isn't Vcc, if you want to use a different voltage reference for ADC, you need to connect it to Aref.

\$\endgroup\$
1
  • \$\begingroup\$ Okay, I actually googled up a scheme and found exactly what you said. So I tried connecting the AREF and AVCC pins to a power terminal and after that the error dissapeared but now the display doesn't display anything and it seems like it's not even connected to ATmega - picture imgur.com/a/s1ZG8 ( notice the gray squares next to the pins which should be blue if they are on ). \$\endgroup\$
    – Machi
    Jan 20, 2018 at 21:28
0
\$\begingroup\$

Ok, nevermind, something crazy was happening, I'm really not sure what because I reverted everything to the start and the display still wouldn't work, even after disconnecting everything and just leaving the display and trying to write out Hello World lol. So I just deleted everything and started from scratch with a new code and new proteus scheme and after connecting AREF and AVCC to power it actually WORKS! So thank you a lot BeB00, you saved my day and maybe my semester cause now I can practice properly on my own :D

The problem is that I don't own all that equipment I need so I needed a way to practice coding stuff with these sensor without the equipment. Thank god for Proteus and BeB00, cheers!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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