cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L471RGT6 ADC Calibration

rwils.1
Associate III

Hi ,

 Is there an automatic calibration routine for the STM32L471RGT6 ADC? If there isn't can someone point me to example code on how to read the internal reference (or some other approach) so I can write code to perform calibration. 

Thank you very much 

Richard

12 REPLIES 12
TDK
Guru

The reference manual RM0351 covers calibration procedure for the ADC.

TDK_0-1701442965214.png

 

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

This is the code I have tried so far to read the calibration values 

 

///////////try calibrating the ADC //////////////

HAL_ADC_Start(&hadc1);

while (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) != HAL_OK) {} calibration=HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED); // This function returns the calibration value

TDK
Guru

> This is the code I have tried so far to read the calibration values 

Why do you think that will read calibration values? Are we just throwing stuff at a wall and seeing if it works? This looks like something ChatGPT would write.

Follow the reference manual. If you can't or don't want to for whatever reason, follow an existing working CubeMX example that uses the ADC. Here is one of many:

https://github.com/STMicroelectronics/STM32CubeL4/blob/master/Projects/NUCLEO-L452RE/Examples/ADC/ADC_RegularConversion_Polling/Src/main.c

 

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

Yes I am throwing stuff at a wall after reading through the HAL manual sections! I appreciate the links you sent. 

Cheers

Richard

Fair enough, thanks for the honesty. Apologies for being a bit harsh.

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

hey - at least you were trying to help ! . 

rwils.1
Associate III

Thanks TDK,

 I followed the instructions in the manual to the letter, the ADC values changed somewhat, but they are still 10% out of calibration which seems way out. I have confirmed the VREF voltages and the input voltages to the ADC pins and added delays of 250ms between turning on the ADC and performing the calibration steps as some others have suggested . I have even hardcoded the registers - bad practice but I can't access register values directly as they don't appear in the header file. So for instance DEEPPWD is not a member of the CR register. 

I'm not too sure where to go from here.

Any advice would be appreciated if you have time. Thank you.

Richard

TDK
Guru

Perhaps share more about your setup. Is this a custom board or known good hardware? Can you share the schematic? Is VREF+ stable and decoupled appropriately? Is VDDA?

If you can share more of your code or specific situation, the answers you get will generally be better. Why do you think they are 10% off? What are you measuring and what do you expect? If you connect the input to GND, do you get 0? If you connect it to VREF+/VDDA do you get 4095? If not, how close?

> I can't access register values directly as they don't appear in the header file

CMSIS headers are generally comprehensive, especially on common peripherals like ADC. What registers don't exist there? DEEPPWD is defined on line 1438 here:

https://github.com/STMicroelectronics/cmsis_device_l4/blob/013bf0e41256ffbc2b539676f2007d08b297a86a/Include/stm32l471xx.h

Some general tips on ADC accuracy are here. In particular, sampling time has a large effect.

https://www.st.com/resource/en/application_note/an2834-how-to-optimize-the-adc-accuracy-in-the-stm32-mcus-stmicroelectronics.pdf

 

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

Thank you very much,

it is a custom board and the VREF is AVDD , it is well decoupled and stable (measured with an oscilloscope). My approach is to measure the actual voltage present on the ADC pin and calculate the expected number of counts by VIN/VDDA*4096. I confess i haven't done the obvious check of tying the pin to ground and VDDA directly , that will take some board hacking, but I will do it regardless. i think your point about ADC sampling speed is salient. I'll slow down the sampling rate and see if that changes the result at all.

i couldn't find DEEPPWD in the listing for the project CMSIS header file STM32L471x.h. After actually reading through the header file I realize it is ADC_CR_DEEPPWD. Now I understand the settings, thanks!

I appreciate your help