cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32F4 internal temperature reading with stm32F4 peripheral library V1.4.4

tusharrabadiya3
Associate II
Posted on October 03, 2016 at 11:36

Hi,

I want to read the internal temperature of STM32F429II. I am using stm32F4 peripheral library V1.4.4. Below is my code,

// Init

static HAL_StatusTypeDef ADCx_Init(void)

{

  hAdc.Instance = ADC1;

if (HAL_ADC_DeInit(&hAdc) != HAL_OK)

{

return HAL_ERROR;

}

  

        if (HAL_ADC_GetState(&hAdc) == HAL_ADC_STATE_RESET)

{

hAdc.Init.ClockPrescaler = ADC_Prescaler_Div2;

hAdc.Init.Resolution = ADC_RESOLUTION_12B;

hAdc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hAdc.Init.ScanConvMode = DISABLE;

hAdc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

hAdc.Init.ContinuousConvMode = ENABLE;

hAdc.Init.NbrOfConversion = 1;

hAdc.Init.DiscontinuousConvMode = DISABLE;

hAdc.Init.NbrOfDiscConversion = 1;

hAdc.Init.ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;

hAdc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hAdc.Init.DMAContinuousRequests = DISABLE;

if (HAL_ADC_Init(&hAdc) != HAL_OK)

{

return HAL_ERROR;

}

hAdc_AdcChannelConfig.Channel = ADC_CHANNEL_TEMPSENSOR;

hAdc_AdcChannelConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;

hAdc_AdcChannelConfig.Rank = 1;

hAdc_AdcChannelConfig.Offset = 0;

if (HAL_ADC_ConfigChannel(&hAdc, &hAdc_AdcChannelConfig) != HAL_OK)

{

return HAL_ERROR;

}

}

return HAL_OK;

}

//Clock

// in stm32f4xx_hal_msp.c

void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)

{

  __HAL_RCC_ADC_CLK_ENABLE();

 

}

// Call this function at some interval from main to read temperature

uint16_t sampleInput(void)

{

        float TemperatureValue = 0;

uint16_t value = 0;

if (HAL_ADC_Start(&hAdc) != HAL_OK)

{

            printf(''HAL_ADC_Start fail\r\n'');

  return HAL_ERROR;

}

if (HAL_ADC_PollForConversion(&hAdc, HAL_MAX_DELAY) != HAL_OK)

{

                printf(''HAL_ADC_PollForConversion fail\r\n'');

return HAL_ERROR;

}

if((HAL_ADC_GetState(&hAdc) & HAL_ADC_STATE_REG_EOC) !=  HAL_ADC_STATE_REG_EOC)

{

                printf(''HAL_ADC_GetState fail\r\n'');

return HAL_ERROR;

}

        

value = HAL_ADC_GetValue(&hAdc);

        printf(''HAL_ADC_GetValue [%x] [%d]\r\n'', value, value);   

TemperatureValue =  value&0x0fff;// 12 bit result

        TemperatureValue *= 3300;

        TemperatureValue /= 0xfff; //Reading in mV

        TemperatureValue /= 1000.0; //Reading in Volts

        TemperatureValue -= 0.760; // Subtract the reference voltage at 25�C

        TemperatureValue /= .0025; // Divide by slope 2.5mV

        TemperatureValue += 25.0; // Add the 25�C

        

        printf(''Temp = [%f]\r\n'', TemperatureValue);

}

I am stuck here in ''HAL_ADC_PollForConversion'' function. Please help me out of this.

#adc #!stm32 #internal-temperature-sensor
3 REPLIES 3
slimen
Senior
Posted on October 03, 2016 at 12:03

Hello,

I recommend to you using latest version of

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef4.html

 v1.13.0 firmware 

There is always enhancement and bug fixes in the new release.

Regards

tusharrabadiya3
Associate II
Posted on October 03, 2016 at 12:34

Got the solution. it was about ADC clock enable issue. It is__HAL_RCC_ADC1_CLK_ENABLE instead of 

__HAL_RCC_ADC_CLK_ENABLE.

Now, This code is working fine. Thanks!!!!!!!

AP_040
Senior

Hello,

I am working on STM32L462CE MCU and my requirement is to read internal temperature and supply voltage. I have tried with example but it is not working. Can you please help me.