Electrical – Simulation of sensor in proteus using Atmega16

atmegaavrproteus

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.

Best Answer

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.

Related Topic