How to get a ADC resolution?
I have the following code that I call from a freertos task scheduled with a period of 100 ms.
It reads the voltage applied to a pin and return the voltage value as a float.
float pinin_pv(void) {
/* Return the pin voltage in the interval [0, PIN_VOLTAGE] */
extern ADC_HandleTypeDef hadc1;
const uint32_t ADC_RESOLUTION_BITS = 10;
const uint32_t pippo = hadc1.Init.Resolution; // Only for explanation
const float PIN_VOLTAGE = 5000.0F; // [mV]
const float ANALOG_IN_RESOLUTION =
PIN_VOLTAGE / (float)(1 << ADC_RESOLUTION_BITS); // [mV]HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
size_t analog_read = HAL_ADC_GetValue(&hadc1);
float pin_voltage;
pin_voltage = ANALOG_IN_RESOLUTION * (float)analog_read;
return pin_voltage;
}
As you see, I have hard-coded const uint32_t ADC_RESOLUTION_BITS = 10; but I would like to pull this information from hadc1.Init.Resolution; (as example, I defined a variable named pippo in the code above). However, the value of pippo is like 1000 times larger that what is supposed to be. How to fix it? Is it possible to avoid hard-coding the variable PIN_VOLTAGE as well?
