Displaying a percentage


Closed Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default Displaying a percentage

    Hi,

    I have a word variable that can hold a number from 0 to 4096. I want to display the value of that variable as a percentage on an LCD. EG when it's 4096 it's 100%, when 2048 it's 50% etc. but can't think how to do it. I know that simply dividing 4096 by 100 will give me the increment value for 1%, but can't seem to format it to work correctly. From a previous bit of code I came across this which is used in a menu option to set the value of CH1_Max between 0 and 4096 by using a word variable which has a value between 0 and 100

    Code:
    CH1_Max = maxbright */ 10484
    Any pointers ?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    Percent = Value ** 1600 is one option but you're pissing away a lot of resolution.

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    Thanks Henrik,

    Is there a way to get better resolution ?

  4. #4
    Join Date
    Feb 2005
    Location
    brookfield, ct, usa
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    X = 4096 * 100
    Percent = div32 value
    Lcdout (dec2 percent)

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    Is there a way to get better resolution ?
    To get .1% is easy, same aproach:
    Code:
    Percent = Value ** 16000   ' Percent is now 0-1000
    LCDOUT $FE, 1, Percent / 10, ".", Percent // 10
    Since you've got slightly over 4000 counts the smallest unit is around 0.025% but I don't know if you need, or even want, the display to show that?

  6. #6
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    Or you could do it this way as there is no need to do another math operation on the variable:

    Percent = Value ** 16000 ' Percent is now 0-1000
    LCDOUT $FE, 1, Percent / 10, ".", Percent DIG 0

    Just format the variable....
    Dave Purola,
    N8NTA
    EN82fn

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    many thanks guys, I'll give it a go when I'm home from work

  8. #8
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    Quote Originally Posted by HenrikOlsson View Post
    Percent = Value ** 1600 is one option but you're pissing away a lot of resolution.
    Code:
    pc1 = CH1_PWM ** 1600                                             
    lcdout $FE,$80,"CH1"," ",dec PC1
    This works nicely, apart from one small issue, in that it won't display 100. It reached 99 and that's it !

    By resolution, I didn't realise that you were referring to the decimal resolution displayed, but was referring to the maths (I assume that whatever divisions were happening the increments for each 1% would be rounded down to an integer as PBP doesn't support FP maths).

    Is there a way where I can display 100 for 4095 steps ?

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    Well, to be fair you said your value ranged from 0 to 4096, that's 4097 "steps" ;-)

    pc1 = CH1_PWM ** 1601

    There WILL be rounding (or truncating rather) issues, yes.

    pc1 = CH1_PWM ** 16004 is better and will give you the result in steps of 0.1% as show before.

  10. #10
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    Quote Originally Posted by HenrikOlsson View Post
    Well, to be fair you said your value ranged from 0 to 4096, that's 4097 "steps" ;-)
    Yeah, sorry... the PCA chip has 4096 steps, so that's 0 to 4095. - brain fade - Must be my age

    Changing the value to 1601 works a treat - Can you explain how that works ?

  11. #11
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    I feel I've written about the */ and ** so many times I've lost count, it's also described in the manual, but it's apparently one of those things...so here goes:

    The ** multiplies the two values you give it. This results in an intermediate 32bit result. PBP then returns the top 16bits of those 32bits. This means it does an inherent divide by 65536 so you COULD think of the ** operator as a "multiply by units of 1/65536 command".

    So x = value ** 1601 is PBP lingo for x = value * 0.02443 and what's 4095*0.02443? Correct, it's 100.

    How did I find 1601? Well you want an "output value" of 100 for an "input value" of 4095 so, (100/4095)*65536=1600.39 but since you really wanted it to display 100 and not 99 we had to round that up to 1601. Same thing for the 0.1% resoultion, then we need to round it up to 16004.

    I hope that makes sense.
    /Henrik.

    EDIT: Forgot, the */ and ** works slightly different depending on if you have LONGs enabled or not. The above describes how it works when DON'T have LONGs enabled.

  12. #12
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Displaying a percentage

    Many thanks, and sorry for making you have to repeat yourself ;-)

Similar Threads

  1. Displaying strings from codespace.
    By Byte_Butcher in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 28th March 2010, 20:32
  2. Displaying Weather Statistics
    By Bill Legge in forum Off Topic
    Replies: 12
    Last Post: - 12th October 2009, 09:09
  3. Displaying temperature Setpoints
    By Kalind in forum mel PIC BASIC Pro
    Replies: 58
    Last Post: - 27th October 2008, 14:52
  4. Displaying temperature using a -40 to 185F??
    By jblackann in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 15th January 2008, 18:45
  5. Displaying numbers
    By Christopher4187 in forum General
    Replies: 8
    Last Post: - 20th March 2006, 23:14

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts