MrNando
MrNando

Reputation: 11

Wrong value using the ADC with Attiny84

I am using an Attiny84 with an LM335z temperature sensor to create a temperature sensor. Using the ADC, I obtain the voltage value and perform the necessary conversions to obtain the temperature value. Then, I display this temperature on a 7-segment display. Here is a code snippet where I configure the ADC and the function I use to read the temperature.

void iniPuertos(void) {
    DDRB = 0b00000011; 
    PORTB = 0b00000100; 
    
    DDRA = 0b11111110; 
    
    /
    ADMUX |= (1 << REFS0); 
    ADMUX |= (1 << MUX2) | (1 << MUX0); 
    ADCSRA |= (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2); 
    ADCSRA |= (1 << ADEN); 

    
    GIMSK |= (1 << INT0);   
    MCUCR |= (1 << 2);  
                
    sei();  

}

int leerTemperatura(void) {
    uint16_t lecturaSensor;
    float voltageSensor = 0.0;
    float temperatura = 0.0;
    int temperaturaSuavizada = 0;
    
    PORTB |= (1 << 0); //Sensor
    _delay_ms(250);

    for (int i = 0; i < 50; i++) {
        ADCSRA |= (1 << ADSC); 
        while (ADCSRA & (1 << ADSC));
            
        lecturaSensor = ADC;
        voltageSensor = lecturaSensor * (5.0 / 1023.0); 
        temperatura = ( voltageSensor / 0.01) - 273.15; 
        temperaturaSuavizada += temperatura;
    }
    temperaturaSuavizada = round(temperaturaSuavizada / 50);
    PORTB &= ~(1 << 0); 
    return temperaturaSuavizada;
}

The issue lies in the variable 'lecturaSensor,' which is responsible for obtaining the ADC reading. This variable seems to erroneously obtain the reading, as when displaying the value on the display, the number shown is incorrect and does not correspond to the temperature. However, if I assign a value like lecturaSensor = 565, the temperature value is displayed correctly. By 'displaying incorrectly,' I mean that not all the LEDs light up as they should. It's worth mentioning that I'm using a BCD decoder. I attempted several changes and modifications to the configuration without success, and I'm unable to identify the error. Additionally, since the Attiny doesn't have UART, I cannot view the value of the variable. I'm seeking assistance in case there's an issue that I'm overlooking.

I'm adding some images: https://imgur.com/a/kzu5jr2

Upvotes: 1

Views: 212

Answers (1)

GandhiGandhi
GandhiGandhi

Reputation: 1350

these chips without UARTS are hard to debug all at once. They do have a "DebugWire" port for debugging; if you have access to that and a debugger, you could greatly simplify debugging with the ability to set breakpoitns and read the value of lecturaSensor directly.

If not, here are some steps that I'd go through to debug the system by isolating each part:

  • For the temperature sensor, you could make a program that sets PORTB0 high forever, then measure the analog input from the LM335z to ATtiny with a multimeter. That could help verify the sensor is outputting expected voltages.
  • Looks like ADMUX is configured to use PA0 as the analog reference voltage; what's connected to that pin?
  • Also looks like the ADC clock prescaler is set to divide the CPU clock by 1/128; the datasheet recommends "an input clock frequency between 50 kHz and 200 kHz". Does that value make sense given your CPU frequency?
  • Try sampling the ADC once then returning the lecturaSensor value from this function directly. There could be a bug in the averaging function.
  • "I mean that not all the LEDs light up as they should." - from that, I wonder if the 7-segment display is working as you intend. You could write a program that writes known values to the display to make sure that it and your driver for it are working.

Those are all the ideas I have, good luck debugging!

Upvotes: 1

Related Questions