setting values and percentages


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    So using my variables B_max = (4095/100)*maxbright would that be

    Code:
    B_max = maxbright */10483
    Nope - that don't work
    Last edited by Scampy; - 29th June 2014 at 19:21.

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


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    If B_Max is the value you send to the PWM controller (0-4095) and maxbright is the percentage value (0-100) as entered by the user then yes.
    Actually, at 100% it'll give you 4094 so make that B_max = maxbright */10484 instead.

    /Henrik.

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by HenrikOlsson View Post
    If B_Max is the value you send to the PWM controller (0-4095) and maxbright is the percentage value (0-100) as entered by the user then yes.
    Actually, at 100% it'll give you 4094 so make that B_max = maxbright */10484 instead.

    /Henrik.
    Yes - B_max and W_max are the values that are sent to the PCA chip (0 - 4095). And maxbright has a value between 0 and 100 as you assumed. But using that line of code the values for B_max and W_max do not change from the pre-programmed default of 4095 after the menu option is used to change the max brightness level

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


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Hi,
    Strange..... I tested the following code
    Code:
    HSEROUT["Program start",13]
    For i = 0 to 5
        Lookup2 i,[1, 10, 25, 50, 75, 100], maxbright
        B_max = maxbright */ 10484
        HSEROUT ["Maxbright: ", DEC3 maxbright, "     B_Max: ", DEC4 B_max, 13]
    NEXT
    And got the following results:
    Code:
    Program start
    Maxbright: 001     B_Max: 0040
    Maxbright: 010     B_Max: 0409
    Maxbright: 025     B_Max: 1023
    Maxbright: 050     B_Max: 2047
    Maxbright: 075     B_Max: 3071
    Maxbright: 100     B_Max: 4095
    Seems to work fine here....

    /Henrik.

  5. #5
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by Scampy View Post
    the values for B_max and W_max do not change from the pre-programmed default of 4095 after the menu option is used to change the max brightness level
    Is the default of 4095 inside the main loop and resetting B_max and W_max after the menu option returns to the main loop.

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Don't know what happened to my post last night, but I found out why it was doing that - It was one of those oversights caused through hours of programming without a break. The menu option saved all the changes when exiting from the menu to the main program, so unless this action was performed all the changes were lost if you remained in the menu section as the diagnostics option reads the values stored in memory and thus read the defaults and over wrote them. This was changed so each section of the menu now writes the new values to memory when exiting the option and reverting back to the menu screen, so switching between menu options and then the diagnostics means that the changes made in any of the other menu sections are read back in.

    One thing I would like to know is what the */ function means

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


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    It's been covered multiple times on the forum and it's covered in the manual.
    In short, think of it as multiplying by units of 1/256. 10484/256=40.95.

    /Henrik.

  8. #8
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    One thing I would like to know is what the */ function means
    This is what I understand or misunderstand possibly.

    Internally the maths can have a 32 bit result because the PIC has a 32 bit register. But if your maths calculation will fit in a word then using word=byte*byte will work and you do not need ** or */.

    If you have wordH=word**word and wordL=word*word then you have all 32 bits stored in two words.

    */ is less obvious but gives the middle word of the 32 bits. Which is the same as word=word*word/256 losing the top and bottom 8 bits.

    If as in your case and as you know 4095/100 gives a result of 40 therefore B_max = (4095/100)*maxbright will give 2000 when maxbright is 50.

    Whereas 4095*256/100 gives result of 10483 and 50*/10483 gives 2047 because by multiplying by 256 and using the */ to remove the 8 lower bits (or divide by 256 which is the same) increased accuracy is achieved. So where is the precision lost? The division /100 is the source of lost precision so as a rule multiply first and divide last which is exactly what */ does in that your program multiplies by 256 and */ divides by 256 last. As an aside working this way reduces the calculations within the program, there is only a division

    A question then. What is the result of 50*4095/100 2000 or 2047? and (4095/100)*50?

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    result 50*4095/100 is 2047
    result (4095/100)*50 is 2000

    the calc follows typical bodmas rules (brackets, of , divide .....) see precedence in manual

    the only 'suprising' bit is that the indermediate result (50*4095) is 204750 way too big for a word var. the pbp compiler code does indeed use 32 bit intermediate results.
    the */ and ** operators allow you access these 32 bit results (providing you follow the rules of course)

Similar Threads

  1. POT part values
    By Michael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th January 2009, 10:41
  2. Port Values
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd January 2007, 20:44
  3. t in POT, c's values
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th August 2006, 23:30
  4. Numeric Values
    By Armando Herjim in forum General
    Replies: 8
    Last Post: - 28th June 2006, 01:54
  5. Wrong values with COSINE!
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 8th May 2006, 16:59

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