1
votes

I would like to get peak value from STM32 adc samples. I have written the below code and I've managed to get peak value however most of the time this value includes the biggest noise. In order to eliminate noise effects, I have decided to apply averaging method. I would like to get 5 measurements' averages. Then I'd like to compare these averages and use the biggest one(biggest average). Can anybody suggest a code?

Regards,

Umut

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
    {
    ADC_raw = HAL_ADC_GetValue(hadc);
    Vdd = 3.3 * (ADC_raw)/4095;
    if (Vdd > Vmax)
      {
        Vmax = Vdd;
        }
1

1 Answers

1
votes

At first, I would remove as much code as possible from the Callback function because it is still inside the interrupt context which should be as short as possible. This is mentioned in a lot of answears here so I will not go into details on how to handle this.

For averaging the measurement, there are multiple ways you can go.

Automatic avarage

Use the ADCs oversampling function. The controller will sample the signal multiple times (configure using OVFS register) and calculate an average value before triggering the interrupt.

Manual average

Using the HAL_ADC_ConvCpltCallback function Store the numer of desired value into an array and calculate the average in the main loop.

Manual average using DMA

Let the DMA store the number of samples you want to use in an array using the function HAL_ADC_Start_DMA. When all samples have been collected you will be notified. This will reduce the processor load because you don't have to shift the data into the array yourself.


You can also combine the oversampling (most of the time a good idea) and one of the other methods depending on your Use-Case.