I am struggling for multiple hours now and cant explain my problem.
I try to digitize 7 analog signals with a STM32G071GB.
I started with the standard AnalogRead() implementation which worked flawlessly. However i experience some noise and wanted to accumulate longer samples ad do some oversampling.
I set up the ADC with the HAL using the build_opt.h (-DHAL_ADC_MODULE_ONLY)
and copied the STM32IDE generated initializing code into Arduino.
The init looks like this:
Code: Select all
void MX_ADC_Init()
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 7;
hadc1.Init.DiscontinuousConvMode = ENABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.OversamplingMode = ENABLE;
hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_4;
hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_2;
hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_LOW;
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = ADC_REGULAR_RANK_3;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = ADC_REGULAR_RANK_4;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = ADC_REGULAR_RANK_5;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/** Configure Regular Channel
*/
..............
Code: Select all
switch(adc){
case 1:
HAL_ADC_PollForConversion(&hadc1, 1); // wait until done
ADCbatt1 = HAL_ADC_GetValue(&hadc1); // get value
HAL_ADC_Start(&hadc1); // start conversion
adc++; // set next value
break;
case 2:
HAL_ADC_PollForConversion(&hadc1, 1); // wait until done
ADCbatt2 = HAL_ADC_GetValue(&hadc1); // get value
HAL_ADC_Start(&hadc1); // start conversion
adc++; // set next value
break;
case 3:
HAL_ADC_PollForConversion(&hadc1, 1); // wait until done
ADCpower_444 = HAL_ADC_GetValue(&hadc1); // get value
HAL_ADC_Start(&hadc1); // start conversion
adc++; // set next value
break;
case 4:.....
However the code works flawlessly if i do:
hadc1.Init.OversamplingMode = DISABLE;
So no oversampling produces the expected behavior e.g. different values for all 7 ADC channels and they reflect the voltage.
Activating oversampling produces no meaningful output.
I am not sure if my code is wrong or if thats some kind of bug?