PWM/Temp setting help with math? or code.


Closed Thread
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: PWM/Temp setting help with math? or code.

    that looks pretty good henrik, no I didn't say "20 or 25%, you claim both...." I said it needs to be 25% and existing is 20%, but thats ok, yes it needs to be 25%
    I forgot what does "*/" do again?


    and arrati, that code is basically what I had started with but didnt work because the end result max would have been 100 and I needed it to be 255, see henrik's code, he did 25% of 255 which is 64, I wasnt thinking when i made my code 55 and then I ended up getting a 20% minimum. numbers are important a decimal in the wrong place, 1 didgit off can ruin code. but thanks, everyones Ideas help somewhere even if its not today.

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


    Did you find this post helpful? Yes | No

    Default Re: PWM/Temp setting help with math? or code.

    Hi,

    The */ operator multiplies the two values you give it and the discards the least significant byte of the result. It's the same thing as first multiplying then dividing by 256. The good thing is that intermeditate result can be large within limits and you won't overflow like you would if you did the same thing "manually".

    Put another way, the */ operator multiplies by units of 1/256. So:
    100 */ 64 = 25
    100 */ 128 = 50
    100 */ 256 = 100
    100 */ 384 = 150
    100 */ 512 = 200
    and so on.

    Lets say you have a value of 123 and want to multiply it by say 3.477 (the correct answer is 427.67). To find the number to use with the */ operator all you need to do is 3.477*256=890. 123*/890 will return 427.

    /Henrik.

  3. #3
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: PWM/Temp setting help with math? or code.

    Sorry if I'm hijacking here but wanted thank you Henrik for the cool tip!
    I've been experimenting with a 2000dps gyroscope that outputs a max of 32757 counts.
    Then with the resolution at 0.07dps, I had to use LONG's to make (32757*0.07=2292.99dps) work.

    Code:
    INTxg   VAR   WORD
    Xg      VAR   LONG
    
    INTxg = (Xg*7)/100   ' Xg max value =32757
    
    
       *** changed to ***
    
    
    INTxg   VAR   WORD
    Xg      VAR   WORD
    
    INTxg = Xg*/18   ' 0.07*256=17.92 for (32757*18)/256=2303, rounding up only 0.43% error at max
    I didn't know if it would work without LONGs but it did, and trimmed 1356 Words in my program!
    Louie

Similar Threads

  1. Replies: 1
    Last Post: - 4th March 2013, 21:57
  2. Using Walter's Servo PWM encode / decode code
    By NickMu in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 1st November 2012, 23:17
  3. Why? Temp conversion C to F code from Rentron (Bruce)
    By Heckler in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th May 2011, 18:34
  4. Setting code size boundaries
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th March 2007, 20:11
  5. RH/Temp Sensor Example Code
    By Bruce in forum Code Examples
    Replies: 7
    Last Post: - 15th June 2006, 15:55

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