STM32F411RE ADC: wrong results with 5V supply to LM35 temperature sensor.
I am trying to get LM35 temperature sensor value using STM32F411RE. When supply voltage fed to LM35 is 3.3 V, the correct digital data is produced. But when 5V is fed as supply to LM35, the digital values produced are the same as those when 3.3V is supplied. Do I have to configure the reference as 5V anywhere in the code? or Is there any way to get proper digital values for 5V supply as well? Please help me out.
Code:
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADCres,ADCres_len);
if(conv_handler==1){
conv_handler=0;
//HAL_ADC_Stop_DMA(&hadc1);
//break;
}
//HAL_ADC_Stop_DMA(&hadc1);
HAL_Delay(100);
digital=ADCres;
temp=(digital*330)/4096;
}
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
conv_handler=1;
}
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}