cancel
Showing results for 
Search instead for 
Did you mean: 

Fault reading of internal temperature sensor in stm32f042

RR.7
Associate II

The program to read the Internal temperature and V_battery voltage using ADC in STM#2F042 is as follows:

#include "main.h"

#define I2C_ADDR (0x40<<1);

#define V_Ref 3.3

#define RESOLUTION 255

#define TEMP30_CAL_ADDR ((uint16_t*)((uint32_t*)0x1FFFF7B8))

#define TEMP110_CAL_ADDR ((uint16_t*)((uint32_t*)0x1FFFF7C2))

ADC_HandleTypeDef hadc;

float internal_temp;

uint8_t index1=0;

uint32_t adc_value[2];

float V_bat;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_ADC_Init(void);

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)

{

if(__HAL_ADC_GET_FLAG(hadc,ADC_FLAG_EOC))

{

adc_value[index1]=HAL_ADC_GetValue(hadc);

index1++;

}

if(__HAL_ADC_GET_FLAG(hadc,ADC_FLAG_EOS))

{

internal_temp = adc_value[0];

internal_temp -= (int32_t) *TEMP30_CAL_ADDR;

internal_temp *= (int32_t)(110-30);

internal_temp /= (int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR);

internal_temp += 30;

V_bat=(float)adc_value[1]*V_Ref/RESOLUTION;

index1=0;

}

}

int main(void)

{

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_ADC_Init();

MX_TIM2_Init();

HAL_ADC_Start_IT(&hadc);

while (1)

{

}

}

static void MX_ADC_Init(void)

{

ADC_ChannelConfTypeDef sConfig = {0};

hadc.Instance = ADC1;

hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

hadc.Init.Resolution = ADC_RESOLUTION_8B;

hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;

hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

hadc.Init.LowPowerAutoWait = DISABLE;

hadc.Init.LowPowerAutoPowerOff = DISABLE;

hadc.Init.ContinuousConvMode = ENABLE;

hadc.Init.DiscontinuousConvMode = DISABLE;

hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc.Init.DMAContinuousRequests = DISABLE;

hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;

if (HAL_ADC_Init(&hadc) != HAL_OK)

{

Error_Handler();

}

/** Configure for the selected ADC regular channel to be converted.

*/

sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;

sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

sConfig.SamplingTime =ADC_SAMPLETIME_1CYCLE_5 ;

if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

{

Error_Handler();

}

/** Configure for the selected ADC regular channel to be converted.

*/

sConfig.Channel = ADC_CHANNEL_VBAT;

if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

{

Error_Handler();

}

}

This is the code I have written (generated from Cubemx.) It will not get the proper value for internal temperature and V_bat (battery voltage).The internal temperature shows 45°C.

Can someone please tell me the error/problem in the code, especially in the callback function?

2 REPLIES 2

Perhaps output the values you're actually reading, and double check the math on paper.

Start perhaps by casting the pointers properly, via a less convoluted cast

#define TEMP30_CAL_ADDR ((uint16_t*)0x1FFFF7B8)

#define TEMP110_CAL_ADDR ((uint16_t*)0x1FFFF7C2)

and making sure those look to be valid

printf("%u %u\n", *TEMP30_CAL_ADDR, *TEMP110_CAL_ADDR);

As I recall the slope is negative.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Piranha
Chief II

> The internal temperature shows 45°C.

That's a completely normal internal temperature for smaller packages.