Weird ADC / Math Results with 9-Bit PWM


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    That explains it, thanks very much. When I do the math by hand the 255's cancel each other out and then it's just 412+100.

    If I put brackets around the dividing part will that fix it?

  2. #2
    Join Date
    Aug 2011
    Posts
    460


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    If I put brackets around the dividing part will that fix it?
    That'll truncate the division, leaving you with 412/255 = 1

    You can use PBPL mode so that it uses LONGs, which should work but it'll be larger and slower if that matters. Otherwise your best bet might be to turn the equation into a lookup table.
    To be honest, I don't use PBP much, so maybe others can suggest something else.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Thanks again tumbleweed. I can't use PBPL without moving up to a 18F chip, and the minimum # of pins is then 18 (I only need two outputs so an 8-pin chip is ideal. Also, the PCB is pretty cramped now as it is).

    But a lookup table could be tricky given the 0-255 range of the 8-bit ADC input but a PWM duty cycle range of 512. Maybe I should switch to 10-bit ADC and just divide by 2?

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


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Why not look at using the DIV32 command?
    Dave Purola,
    N8NTA
    EN82fn

  5. #5
    Join Date
    Aug 2011
    Posts
    460


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    That's an interesting operation. Splitting the equation up into two parts for DIV32 seems to work...
    Code:
    LEDBrVal = ((compVal - 0) * (MaxDuty - MinDuty))
    LEDBrVal = DIV32 (MaxADCVal - 0) + MinDuty
    Is it safe to do something like that or would you suggest making the DIV32 statement simpler so that you know it immediately follows the multiplication (I don't know what the OP's intent for the " - 0" part of the above is for)?

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


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Yes, The way you have it is OK. Just remenber that the result of the first multiplication before the DIV32 is to a dumby variable and it won't be usefull after the DIV32 function. It should be sized as a word.
    Dave Purola,
    N8NTA
    EN82fn

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Quote Originally Posted by Dave View Post
    Yes, The way you have it is OK. Just remenber that the result of the first multiplication before the DIV32 is to a dumby variable and it won't be usefull after the DIV32 function. It should be sized as a word.
    Thanks Dave and tumbleweed! I've never had to use DIV32 before so I appreciate the explicit examples.

  8. #8


    Did you find this post helpful? Yes | No

    Default Re: Weird ADC / Math Results with 9-Bit PWM

    Quote Originally Posted by tumbleweed View Post
    That's an interesting operation. Splitting the equation up into two parts for DIV32 seems to work...
    Code:
    LEDBrVal = ((compVal - 0) * (MaxDuty - MinDuty))
    LEDBrVal = DIV32 (MaxADCVal - 0) + MinDuty
    Is it safe to do something like that or would you suggest making the DIV32 statement simpler so that you know it immediately follows the multiplication (I don't know what the OP's intent for the " - 0" part of the above is for)?
    I grabbed this scaling function from an Arduino library and was emulating that:

    Code:
    '   Arduino Map function to emulate:
    '   ===============================
    '   map(value, fromLow, fromHigh, toLow, toHigh)
    
    '   long map(long x, long in_min, long in_max, long out_min, long out_max)
    '   {
    '     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
    '   }
    In general, the minimum brightness level is 0. But in this case, I only want to adjust the LED brightness between 50-100% duty cycle, so maybe I need to tweak that part as well.

Similar Threads

  1. Weird PWM Behaviour on 16F1825
    By RossWaddell in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th October 2012, 22:59
  2. Averaging 16 bit values without using 32 bit math
    By sirvo in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th October 2007, 23:18
  3. Strangw results when using PWM command
    By malc-c in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 10th July 2006, 13:14
  4. Strange Results in Math
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 31st August 2005, 08:09
  5. PBP 16-bit ADC result math
    By sonic in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2005, 15:21

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