cancel
Showing results for
Did you mean:

# How to get a ADC resolution?

Senior

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] */
const uint32_t pippo = hadc1.Init.Resolution; // Only for explanation
const float PIN_VOLTAGE = 5000.0F; // [mV]
const float ANALOG_IN_RESOLUTION =
float pin_voltage;
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?

1 ACCEPTED SOLUTION

Accepted Solutions
Guru

Look at the definition for hadc1.Init.Resolution to see the various values and do an if statement for each.

``````if (hadc1.Init.Resolution == ADC_RESOLUTION_6B) {
...
...
} ...``````

You can also look at how they're defined within the register to make a formula for this.

> const float PIN_VOLTAGE = 5000.0F; // [mV]

Not sure what you mean here. Why is this hard-coded to 5V anyway? Shouldn't it be 3.3V? CubeMX allows you to define a value for VREF+ (technically VDD) that you could use if you want.

``#define  VDD_VALUE                    (3300UL) /*!< Value of VDD in mv */``

6 REPLIES 6
Chief III

Use this button to properly post source code:

Ok! I wish to edit my post, but I cannot figure out how... :\
EDIT: Found!

Guru

Look at the definition for hadc1.Init.Resolution to see the various values and do an if statement for each.

``````if (hadc1.Init.Resolution == ADC_RESOLUTION_6B) {
...
...
} ...``````

You can also look at how they're defined within the register to make a formula for this.

> const float PIN_VOLTAGE = 5000.0F; // [mV]

Not sure what you mean here. Why is this hard-coded to 5V anyway? Shouldn't it be 3.3V? CubeMX allows you to define a value for VREF+ (technically VDD) that you could use if you want.

``#define  VDD_VALUE                    (3300UL) /*!< Value of VDD in mv */``