cancel
Showing results for 
Search instead for 
Did you mean: 

How to do ADC averaging in STM32H7 MCU using hardware over sampling

RShar.9
Senior

hello everyone i am using stm32h7 MCU, i want averaged ADC data using  hardware over sampling. how can get this.

1 ACCEPTED SOLUTION

Accepted Solutions
Imen.D
ST Employee

Hello @RShar.9​ ,

The example ADC_OverSampler available in the STM32CubeH7 package will help you to configure and use the ADC to convert an external

analog input combined with oversampling feature:

STM32Cube_FW_H7_V1.5.0\Projects\STM32H743I-EVAL\Examples\ADC\ADC_OverSampler

  • For Oversampling ratio x4, you can add this lines before calling HAL_ADC_Init
hadc1.Init.OversamplingMode         = ENABLE;      /* Oversampling enabled */
hadc1.Init.Oversampling.Ratio                 = 3;    /* Oversampling ratio */
hadc1.Init.Oversampling.RightBitShift         = ADC_RIGHTBITSHIFT_2;         /* Right shift of the oversampled summation */
hadc1.Init.Oversampling.TriggeredMode         = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;         /* Specifies whether or not a trigger is needed for each sample */
hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; /* Specifies whether or not the oversampling buffer is maintained during injection sequence */

  • For oversampling ratio x2, update these lines:
hadc1.Init.Oversampling.Ratio                = 1;   /* Oversampling ratio */
 hadc1.Init.Oversampling.RightBitShift        = ADC_RIGHTBITSHIFT_1;        /* Right shift of the oversampled summation */

 Hope this helps you.

Best Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

View solution in original post

7 REPLIES 7
Imen.D
ST Employee

Hello @RShar.9​ ,

The example ADC_OverSampler available in the STM32CubeH7 package will help you to configure and use the ADC to convert an external

analog input combined with oversampling feature:

STM32Cube_FW_H7_V1.5.0\Projects\STM32H743I-EVAL\Examples\ADC\ADC_OverSampler

  • For Oversampling ratio x4, you can add this lines before calling HAL_ADC_Init
hadc1.Init.OversamplingMode         = ENABLE;      /* Oversampling enabled */
hadc1.Init.Oversampling.Ratio                 = 3;    /* Oversampling ratio */
hadc1.Init.Oversampling.RightBitShift         = ADC_RIGHTBITSHIFT_2;         /* Right shift of the oversampled summation */
hadc1.Init.Oversampling.TriggeredMode         = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;         /* Specifies whether or not a trigger is needed for each sample */
hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; /* Specifies whether or not the oversampling buffer is maintained during injection sequence */

  • For oversampling ratio x2, update these lines:
hadc1.Init.Oversampling.Ratio                = 1;   /* Oversampling ratio */
 hadc1.Init.Oversampling.RightBitShift        = ADC_RIGHTBITSHIFT_1;        /* Right shift of the oversampled summation */

 Hope this helps you.

Best Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

hii

thanks for your reply.

i have questions.'

  1. ratio x4 means, adc will give averaged of 4 adc data?
  2. ratio x2 means, adc will give averaged of 2 adc data?

i am using dma in adc. will i get averaged data in dma buffer?

can you please tell me what are features of hardware oversampling in stm32h7, i never used it in any other MCU.

Actually my ADC signal is very noisy , how can filter it.

Hi @RShar.9​ ,

1. 2. Yes

The output of ADC Data register will contain the average of the data. And it will be transferred by DMA.

So yes, your DMA buffer data has already the average data.

This is done purely in hardware without having the need to have a software for the averaging (less power consumption + less CPU load).

You can add an RC low pass filter before your ADC input in order to filter noise + use oversampling.

To remove noise, you can use differential mode , but this require that your input signal is fully differential.

Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

Thanks..

hadc1.Init.Oversampling.Ratio = 1; /* Oversampling ratio */

what is the purpose of over sampling ratio to set 1 or 3 ?

when i saw in ADC_OverSampler example , i found that by that it will increase the resolution of adc data.

so that after get the 18 bit adc value i have to divide that by 4 to get actual 16 bit averaged adc data?

Trying to understand the above post values?!

The docs state that Ratio values are 0 - 1023, setting the number of additional oversamples, per sample. 2x(Ratio=1),4x(Ratio=3),8x(=7),16x(=15),32x(..),64,128,256,512,1024,2048 which matches to -> ADC_RIGHTBITSHIFT_1 - ADC_RIGHTBITSHIFT_11
RightBitShift is the shift (division) required for averaging, binary, so stepping in power2, 1=div2,2=div4,etc, 11 values allowed, so max. us 2^11 = 2048, in this case 1024 samples per sample!

So Ratio cannot be any value from 1-1023, but 1, 3, 7, 15, etc. Is that correct?

Is this so obvious that the ST docs skipped mentioning it?

 

hadc1.Init.Oversampling.Ratio                 = 3;    /* Oversampling 3x */
hadc1.Init.Oversampling.RightBitShift         = ADC_RIGHTBITSHIFT_2;   /* Divide result by 4 to average? */

Shouldn't it be /* Oversampling 4x , not 3x */ :-

 

 

 

 

 

hadc1.Init.Oversampling.Ratio = 3; /* Oversampling 4x */
hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_2;   /* Divide result by 4 to average */