Percent math help
TC
Posts: 1,019
I am not that great with math, and I have ran into a problem that I need some help with.
I am working on a voltage/current meter for a PC power supply. I have the current part figured out and working, but I cant figure out how to do the voltage. What I have is a Microchip PAC1710, it has a 11-bit ADC that has a input range from 0 to 39.98V. I have it figured out to convert the ADC value to a voltage. But now I need to figure out how to change it into a percentage from a set value.
For example:
If I have a set voltage of 3.3V ($A9), and what is read from the ADC is 3.457V ($B1). I need it to output 47 (4.7%). I have this part figured out.
But also, if I had the same set voltage, and the value from the ADC is 3.124V ($A0). I need it to output -56 (-5.6%)
I can change the way my program takes the value if it would be to hard to do a positive and negative value. What I would like to be able to do is just set the set voltage, and the prop output what percentage is the ADC value from the set voltage, with the set voltage being 0%
There will only be 3 set voltages, 3.3V, 5V, and 12V
Thanks
TC
I am working on a voltage/current meter for a PC power supply. I have the current part figured out and working, but I cant figure out how to do the voltage. What I have is a Microchip PAC1710, it has a 11-bit ADC that has a input range from 0 to 39.98V. I have it figured out to convert the ADC value to a voltage. But now I need to figure out how to change it into a percentage from a set value.
For example:
If I have a set voltage of 3.3V ($A9), and what is read from the ADC is 3.457V ($B1). I need it to output 47 (4.7%). I have this part figured out.
But also, if I had the same set voltage, and the value from the ADC is 3.124V ($A0). I need it to output -56 (-5.6%)
I can change the way my program takes the value if it would be to hard to do a positive and negative value. What I would like to be able to do is just set the set voltage, and the prop output what percentage is the ADC value from the set voltage, with the set voltage being 0%
There will only be 3 set voltages, 3.3V, 5V, and 12V
Thanks
TC
Comments
pme := 1000 * (value - target) / target
Note that this does not have to be in volts...you can use native ADC counts, if you wish. Also, you can pre-round to the nearest integer value like this:
pme := (1000 * (value - target) + (target / 2)) / target
You shouldn't have to worry about overflow.
Jonathan
Edit: I think 3.124V is 5.3% below the target value of 3.3 (not 5.6%), assuming you want a percentage of the target value, not just the lower value.
Wonderful! That works, Thank you.
And the way I got the 5.6% is from (( 3.3 / 3.124 ) -1 ) *100 = 5.6%
It is the only way I know how to do percentages.
Jonathan
((3.3 - 3.124) / 3.3) * 100% = 5.333%
or
(1 - (3.124 / 3.3)) * 100% = 5.333%
Edit: I hadn't seen Jonathan's last post
Ah, now I get it. Thanks
I never learned much about percetages, But now I can say "I know 2 ways to do it. And I understand what is going on"
Thank You