Fixed temperature measurement

This commit is contained in:
Thomas Kolb 2021-06-12 16:47:59 +02:00
parent df3211333e
commit add925bba0
1 changed files with 12 additions and 9 deletions

View File

@ -17,20 +17,23 @@ static fxp_t calibration_factors[ADC_NUM_CHANNELS-1]; // all except temperature;
/* Temperature sensor calibration value address */ /* Temperature sensor calibration value address */
#define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2)) #define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2))
#define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8)) #define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8))
#define VDD_CALIB ((uint16_t) (330)) /* calibration voltage = 3,30V - DO NOT CHANGE */ #define VDD_CALIB ((int32_t) (330)) /* calibration voltage = 3,30V - DO NOT CHANGE */
#define VDD_APPLI ((uint16_t) (330)) /* actual supply voltage */ #define VDD_APPLI ((int32_t) (330)) /* actual supply voltage */
/* function for temperature conversion */ /* function for temperature conversion */
static fxp_t calc_temperature(uint16_t adc_val) static fxp_t calc_temperature(uint16_t adc_val)
{ {
fxp_t temperature = FXP_FROM_INT( int32_t temperature_raw = ((int32_t)adc_val * VDD_APPLI / VDD_CALIB)
((int32_t)adc_val * VDD_APPLI / VDD_CALIB) - - (int32_t)(*TEMP30_CAL_ADDR);
(int32_t)*TEMP30_CAL_ADDR);
temperature = fxp_mult(temperature, FXP_FROM_INT(110 - 30)); fxp_t temperature = FXP_FROM_INT(temperature_raw);
temperature = fxp_div(temperature, FXP_FROM_INT(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR));
return fxp_add(temperature, FXP_FROM_INT(30)); fxp_t scale_dividend = FXP_FROM_INT(110 - 30);
fxp_t scale_divisor = FXP_FROM_INT((int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR));
fxp_t scale = fxp_div(scale_dividend, scale_divisor);
return fxp_add(fxp_mult(temperature, scale), FXP_FROM_INT(30));
} }
@ -71,7 +74,7 @@ void measurement_init(void)
ANALOG_INPUT_U_SW, // U_SW ANALOG_INPUT_U_SW, // U_SW
ANALOG_INPUT_I_SOLAR, // I_Solar ANALOG_INPUT_I_SOLAR, // I_Solar
ANALOG_INPUT_I_LOAD, // I_Load ANALOG_INPUT_I_LOAD, // I_Load
16 // Temperature sensor ADC_CHANNEL_TEMP // Temperature sensor
}; };
// Convert calibration factors to fixed-point numbers for direct use // Convert calibration factors to fixed-point numbers for direct use