AnsweredAssumed Answered

Stm32F4 internal temperature reading with stm32F4 peripheral library V1.4.4

Question asked by rabadiya.tushar on Oct 3, 2016
Latest reply on Oct 3, 2016 by rabadiya.tushar
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.

Outcomes