I am working on my first project using an STM32F103 microcontroller. I am trying to read the temperature and to print it out every second. The code seems to do its job sometimes but on a few occasions the function returns 0 when it clearly should not return 0.
I have defined the last_values
to be fixed values so that the calculation should allways be a positive number.
The function where things seem to mess up:
float TempSensor::getTemp() {
uint32_t average = 0;
for (int index = 0; index < AVERAGE_SIZE; index++)
average += last_values[index];
float average_val = average / AVERAGE_SIZE;
printf("Value %f, Address %p\n\r", average_val, (void*) &average_val);
printf("Average: %lu/%d=%f\n\r", average, AVERAGE_SIZE, average_val);
float result = 1 / (1 / (T0 + 273.15) + (log(((average_val * R1) / (ADC_RES - average_val)) / R0)) / B) - 273.15;
return result < 0 ? -25.0 : result;
}
The tempp_sensor.h file:
#ifndef INC_JEMINA_TEMP_SENSOR_H_
#define INC_JEMINA_TEMP_SENSOR_H_
#include "Jemina/Jemina.h"
#include "math.h"
const uint8_t AVERAGE_SIZE = 5;
//extern ADC_HandleTypeDef hadc1;
class TempSensor {
private:
uint8_t channel;
uint32_t last_values[AVERAGE_SIZE] = { 1111 , 2222, 3333, 4444, 5555};
public:
static uint32_t value[4];
TempSensor(uint8_t channel);
float getTemp();
void readTemp();
static void init();
};
#endif /* INC_JEMINA_TEMP_SENSOR_H_ */
The function call using an interrupt on timer 6
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim == &htim6) { //Gets called once every second
printf("H0 temp: %f\n\n\r", H0_TEMP.getTemp());
}
}
Output:
Value 3333.000000, Address 0x2000bf68
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 3333.000000, Address 0x2000bf88
Average: 16665/5=3333.000000
H0 temp: 65.428040
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
Value 0.000000, Address 0x40aa0a00
Average: 16665/5=0.000000
H0 temp: 0.000000
As you can see the average_val
should allways be 16665/5=3333.0 but sometimes it remains 0.0. One thing I noticed is that the address is also different when the calculation seems to go wrong.
Does anyone know why this happens and how I can fix it?