Displaying a percentage


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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.

  2. #2
    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 ?

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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.

  4. #4
    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 : 0

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