Login Join Maker Pro

# Voltage calculations in Fixed Format

#### electronicsLearner77

Jul 2, 2015
256
I want to perform the voltage divider calculations in fixed format, please advise if my calculations are correct.

Vout voltage is given to the microcontroller, and i want to calculate V1.
Vout = V1*R2/(R1+R2)
V1 = (R1+R2)/R2Vout
V1 = 108/8*Vout = 13.5*Vout -> eq1
13.5 constant is in Q4.12 format converting it into Q1.15 format

= 13.5*2^12 = 55296
Q1.15 format = 55296*2^3 = 442368 = 0x6 C000

#define factor (0x6C000)
Let the value of Vout received in Q1.11 format = 657 (657/1024*3.3V = 2.11V)

As per equation1
V1 = 13.5*Vout

int main(void)
{
uint32 Voltage=0;
voltage = factor * adc
voltage = 442368 * 657 = 0x1152 C000 (Q1.31 format);
}

There are some gaps i am not able to proceed after this. Also the constant i highlighted crossed the 16 bit. Can you please advise how to resolve?

#### Bluejets

Oct 5, 2014
6,199
Don't know about all your equations but a quick work out in my head shows if 5v on the Vout, Vin would be 67.5v.

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
13,098
voltage = 442368 * 657 = 0x1152 C000 (Q1.31 format);
Imho you can't do math with Q-formatted numbers this way unless you use a library that does the necessary conversions for you.

1. what is the resolution of the ADC? Presumably 12 bit, but you don't state that explicitly. I assume this from your using the factor 212.
2. what is the reference voltage for the ADC, i.e. what is the correspondence between ADC value and voltage?
3. are you using signed or unsigned values, i.e. can teh voltage be only of one poöarity or can it be of both polarities?
4. there seem to be different versions of the Q-notation. Which one do you use?
5. why do you use the Q-notation at all? Seems overkill to me.

Assuming you have
- 12 bit ADC (0...4095)
- unipolar input voltage
- 5 V reference voltage, i.e. ADC 4095 == 5 V
Then
Vin = 5 V / 4095 × 13.5 × ADC_value= 0.0165 × ADC_Value

Simply do integer math by scaling the constant factor up and re-scaling down after the multiplication:
Vin = 0.0165 × ADC_Value = (10000 × 0.0165 × ADC_Value) / 10000 = (165 × ADC_Value) / 10000
(the exact value of the factor is more like 0.016479...)
This requires 32 bit integer math, but this is going to be faster than any kind of floating point emulation.

With some loss of accuracy and not fully using the range of the ADC you could get away with 16 bit math:
Vin = 0.0165 × ADC_Value = (1000 × 0.0165 × ADC_Value) / 1000 = (16 × ADC_Value) / 1000

If, on the other hand, you had a floating point unit (which I assume you don't have), there would be no need for any other number format than the native flaoting point format of this unit.

Replies
5
Views
2K
Replies
5
Views
7K
Replies
4
Views
103
Replies
5
Views
159
Replies
12
Views
253