при измерении ацп накапливает некоторое кол-во измерений и усредняет их
This commit is contained in:
parent
8f0a5b0a94
commit
b94459ebae
@ -11,6 +11,8 @@ extern "C" {
|
|||||||
extern void ErrorMsgHandler(const char * msg);
|
extern void ErrorMsgHandler(const char * msg);
|
||||||
|
|
||||||
// -------------------------- Analog read -------------------------- //
|
// -------------------------- Analog read -------------------------- //
|
||||||
|
#define ADC_SAMPLES_QTY 10 // samples quantity for averaging adc results
|
||||||
|
|
||||||
// structure for ADC channel initialization. Only the channel number
|
// structure for ADC channel initialization. Only the channel number
|
||||||
// changes, everything else is the same
|
// changes, everything else is the same
|
||||||
static ADC_HandleTypeDef hadc =
|
static ADC_HandleTypeDef hadc =
|
||||||
@ -44,11 +46,19 @@ uint32_t analogRead(uint32_t PinNumber)
|
|||||||
hadc.Init.Sel = adcChannel;
|
hadc.Init.Sel = adcChannel;
|
||||||
HAL_ADC_Init(&hadc);
|
HAL_ADC_Init(&hadc);
|
||||||
|
|
||||||
// start the conversion twice in case another channel was polled before
|
// start the dummy conversion in case another channel was polled before
|
||||||
HAL_ADC_SINGLE_AND_SET_CH(hadc.Instance, adcChannel);
|
HAL_ADC_SINGLE_AND_SET_CH(hadc.Instance, adcChannel);
|
||||||
value = HAL_ADC_WaitAndGetValue(&hadc);
|
HAL_ADC_WaitAndGetValue(&hadc);
|
||||||
HAL_ADC_Single(&hadc);
|
|
||||||
value = HAL_ADC_WaitAndGetValue(&hadc);
|
// accumulate results
|
||||||
|
uint32_t acc = 0;
|
||||||
|
for (uint8_t i = 0; i<ADC_SAMPLES_QTY; i++)
|
||||||
|
{
|
||||||
|
HAL_ADC_Single(&hadc);
|
||||||
|
acc += HAL_ADC_WaitAndGetValue(&hadc);
|
||||||
|
}
|
||||||
|
// get value by averaging
|
||||||
|
value = acc/ADC_SAMPLES_QTY;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ErrorMsgHandler("analogRead(): invalid analog pin number");
|
ErrorMsgHandler("analogRead(): invalid analog pin number");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user