cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to read VREFINT and other adc 1 channels (nucleo l432KC)

HDIA.1
Associate III

Hello ,

I will power a stm32 pcb with battery so the vdd voltage will drop

So i need to read this VDD to make a conversion of external temp sensor from 0->4096 to a voltage

Voltage temp=(VDD*adc_value/4096)

But my chip only have one ADC and i'm unable to have correct value when i try to read VREFINT and a other channel

------------------------------------------------------

TERMINAL VALUE:

VREFINT=3.8V (chip will die )

PA6=1.16V (connected to GND)

-------------------------------------------------------

CODE FOR DMA :

 HAL_ADC_Start_DMA(&hadc1,value,2);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

 char MSG[100];

 float Vdd;

 Vdd = 3300 * (*VREFINT_CAL_ADDR)/ value[1];

 Vdd=Vdd/1000;

 sprintf(MSG,"La tension est de : %f V\r\n",Vdd);

 HAL_UART_Transmit(&huart2,MSG,sizeof(MSG),1000);

 char MSG1[100];

 float PA7;

 PA7=((Vdd*value[0])/4095.0f);

 sprintf(MSG1,"La tension de PA6 est de : %f V\r\n",PA7);

 HAL_UART_Transmit(&huart2,MSG1,sizeof(MSG1),1000);

  /* USER CODE BEGIN 3 */

 }

---------------------------------------------------------------------------------------

CODE FOR CHANGING THE CHANNEL WHEN WE WANT TO READ IT

void ADC_Select_CH12 (void)

{

ADC_ChannelConfTypeDef sConfig = {0};

 /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

 */

 sConfig.Channel = ADC_CHANNEL_12;

 sConfig.Rank = 1;

 sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;

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

 {

  Error_Handler();

 }

}

void ADC_Select_VREF (void)

{

ADC_ChannelConfTypeDef sConfig = {0};

 /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

 */

 sConfig.Channel = ADC_CHANNEL_VREFINT;

 sConfig.Rank = 1;

 sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;

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

 {

  Error_Handler();

 }

}

 while (1)

 {

  /* USER CODE END WHILE */

 uint16_t ADC_VAL[1000];

 ADC_Select_VREF();

 HAL_ADC_Start(&hadc1);

 HAL_ADC_PollForConversion(&hadc1, 1000);

 ADC_VAL[0] = HAL_ADC_GetValue(&hadc1);

 HAL_ADC_Stop(&hadc1);

 char MSG[100];

 float Vdd;

 Vdd = 3300 * (*VREFINT_CAL_ADDR)/ ADC_VAL[0];

 Vdd=Vdd/1000;

 sprintf(MSG,"La tension est de : %f V\r\n",Vdd);

 HAL_UART_Transmit(&huart2,MSG,sizeof(MSG),1000);

   HAL_Delay(100);

 ADC_Select_CH12();

 HAL_ADC_Start(&hadc1);

 HAL_ADC_PollForConversion(&hadc1, 1000);

 char MSG1[100];

 ADC_VAL[1] = HAL_ADC_GetValue(&hadc1);

 HAL_ADC_Stop(&hadc1);

 float PA6;

 PA7=((Vdd*ADC_VAL[1])/4095.0f);

 sprintf(MSG1,"La tension de PA6 est de : %f V\r\n",PA7);

 HAL_UART_Transmit(&huart2,MSG1,sizeof(MSG1),1000);

  /* USER CODE BEGIN 3 */

 }

------------------------------------------------------------------

So where i'm wrong and do you have some ideas to correct this

Thanks

0 REPLIES 0