Shop OBEX P1 Docs P2 Docs Learn Events
Percent math help — Parallax Forums

Percent math help

TCTC Posts: 1,019
edited 2014-01-14 16:49 in Propeller 1
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

Comments

  • lonesocklonesock Posts: 917
    edited 2014-01-14 15:53
    Looks like you want "per-mil" error [8^)

    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.
  • TCTC Posts: 1,019
    edited 2014-01-14 16:09
    lonesock wrote: »
    Looks like you want "per-mil" error [8^)

    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.
  • lonesocklonesock Posts: 917
    edited 2014-01-14 16:22
    Ah, got it! Your equation works, but you want to know 3.124 as a percentage of the reference, meaning 3.3 is on the bottom of the fraction. So, same equation: (3.124 / 3.3) - 1 = -5.333.

    Jonathan
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-01-14 16:25
    I'd do the percentage math (when not worrying about integers) one of these two ways.

    ((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
  • TCTC Posts: 1,019
    edited 2014-01-14 16:48
    lonesock wrote: »
    Ah, got it! Your equation works, but you want to know 3.124 as a percentage of the reference, meaning 3.3 is on the bottom of the fraction. So, same equation: (3.124 / 3.3) - 1 = -5.333.

    Jonathan

    Ah, now I get it. Thanks
  • TCTC Posts: 1,019
    edited 2014-01-14 16:49
    Duane Degn wrote: »
    I'd do the percentage math (when not worrying about integers) one of these two ways.

    ((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

    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
Sign In or Register to comment.