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