cancel
Showing results for 
Search instead for 
Did you mean: 

STM32h747 ADC sampling problem

yuri CH
Senior

Hi!

I am currently working with the stm32h747, trying to sample current and voltage using the internal ADC with its analog pins. My samples are wrong, the values sampled are coming out with a wierd offset (for example: 0 voltages is samples as ~1.87v). i can see that aplying votage on the pins or shorting them to GND gives some effect but the values are still wrong.

the init code is standard and generated by cubeMX, My cube defenitions look like this:

0693W00000Hr0AKQAZ.pngHere is an example of how i sample:

the following function is called and i configure the specific pin i want to sample.

float CAdc::GetAnalogIn(void)

{

 float val, Result;

  

 ADC_ChannelConfTypeDef sConfig = {0};

  

 sConfig.Channel = ADC_CHANNEL_18;     //set MNTR_APD_Pin channel

 sConfig.Rank = ADC_REGULAR_RANK_1; //Work with one channel

 sConfig.SamplingTime     = ADC_SAMPLETIME_16CYCLES_5; //number of cycles

  

 HAL_ADC_ConfigChannel(&hadc1,&sConfig);

  

 val =  ADC_Get_Channel_Val(&hadc1,10);

 Result = (((val * 3.3) / 4096) * 1);

  

 return Result;

}

This function makes the actual sampling operation and called after proper config.

float CAdc::ADC_Get_Channel_Val(ADC_HandleTypeDef* hadc,uint8_t Avg_Count)

{

 float value = 0;

 HAL_StatusTypeDef retCode;

  

 retCode = HAL_ADC_Start(hadc);

 if(retCode != HAL_OK)

  return -1;

  

 retCode = HAL_ADC_PollForConversion(hadc, Avg_Count);

 if(retCode != HAL_OK)

  return -1;

  

 uint32_t raw = HAL_ADC_GetValue(hadc);

 value = raw;

  

 retCode = HAL_ADC_Stop(hadc);

 if(retCode != HAL_OK)

  return -1;

  return value;

}

Thanks in advance to those who can help resolve this issue :)

1 ACCEPTED SOLUTION

Accepted Solutions
yuri CH
Senior

Thanks for the responses everyone.

The issue was resolved by changing configuration in the cube and now we are able to read the data properly.

The only thing that is still alarms me that the old configuration which was basic worked on the F7 and did not work on the H7.

View solution in original post

9 REPLIES 9
MM..1
Chief III

Why you use float ??

uint32_t raw = HAL_ADC_GetValue(hadc);
 value = raw;

and how you check return -1

 val =  ADC_Get_Channel_Val(&hadc1,10);
 Result = (((val * 3.3) / 4096) * 1);

yuri CH
Senior

the result in calculations will be a float value so i return what the ADC sampled already converted to float variable. but this isnt the problem, the value sampled is already too high to begin with.

TDK
Guru

What board? What pin?

What value do you get when you short it to GND? What value do you get when you short it to 3V3?

General tips for ADC accuracy are here:

https://www.st.com/resource/en/application_note/cd00211314-how-to-get-the-best-adc-accuracy-in-stm32-microcontrollers-stmicroelectronics.pdf

If you feel a post has answered your question, please click "Accept as Solution".

You still dont check return -1 in your code and maybe you use ADC3 and this have IN18 internaly connected to temp sensor. This seems as your values...

 sConfig.Channel = ADC_CHANNEL_18;

i am using ADC1, also i have tried this with 10 different adc inputs (0,1,2,3,4,5,6, 10,15,19)

the mcu is stm32h757, embedded in a custom board.

the issue persists on both STM32H747 disco and nucleo-H745.

While working on the Nucleo-H745 i tried several different analog pins (PC0, PA4, PF11)

The results were:

pin not connected to anything - 1.5-1.6 V

connected to 3.3V - 2.9-3V

connected to GND: 1.2 V

The results variy between pins by 100-200 mV.

yuri CH
Senior

I have also tried running the same code and same cube defenitions on nucleo-f767zi, everything worked fine and i was able to sample the voltages from GND to 3v3 perfectly.

does anyone have an idea whats the problem with this on the H7?

Check schematics, pin connection and SMPS usw.

yuri CH
Senior

Thanks for the responses everyone.

The issue was resolved by changing configuration in the cube and now we are able to read the data properly.

The only thing that is still alarms me that the old configuration which was basic worked on the F7 and did not work on the H7.