ADCIN > HPWM but with min/max limits - i failed maths


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2005
    Posts
    44

    Default ADCIN > HPWM but with min/max limits - i failed maths

    Im just working on a small project, but my maths skills are not 100% so I'll ask here and see what you guys reckon:

    I have an ADC input, that receives an 0-5v level based on a potentiometer, but in this application, the ADC input is never 0-255 (lets assume 8 bits)
    For example the min ADC reading may be 12, and the max 221.
    The min and max readings will be stored on the eeprom.

    So assuming the 12 and 221 as min and max, gives a range of 209 steps

    Now, I need to output a PWM (HPWM) signal, whose duty cycle varies inversely proportional to the ADC, but there is also a min and max PWM % that needs to be set.
    Lets use 12% and 88% as the limits here, so min duty is 38 and max is 224 with a range of 186 steps. These max and mins are also in EEPROM

    Therefore, what i want to achieve -
    ADC = 12, PWM = 224
    .
    .
    .
    ADC = 221, PWM = 38

    (and everything in between)

    Whats the best way of performing this calculation?

    My initial thoughts are to divide the no of ADC steps by the no of PWM steps and multiply this result, by the ADC reading then inverting the bits to give me the PWM duty cycle, does this make sense??

    I told you i failed my maths classes

  2. #2
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Hi Jamie_s,

    With HPWM you cannot do this if I understood right. Somebody correct if I'm wrong.
    Did you mean that with ADC = 12 you will get one pulse high/low in every 224 and ADC = 221 one pulse high/low in every 38?

    If yes, then try something like this.

    Code:
    <code><font color="#000000">adval <b>VAR BYTE
    
    </b>Loop:
        <b>GOSUB </b>Read_ADC
        <b>HIGH </b>PWM_Out        <font color="#000080"><i>'Change to match your pin name for PWM output!
        </i></font><b>PAUSE </b>1
        <b>LOW </b>PWM_Out
        <b>PAUSE </b>adval
        <b>GOTO </b>Loop
    
    Read_ADC:
        <b>ADCIN </b>0, adval			
        adval = 235 - ((89 * adval)/100)  
        <b>RETURN</b></code>
    Funtion that is used here is y = 235 - 0.89*x.
    x = 221 -> y = 38.21
    x = 12 -> y =224.32
    It should give you sufficient accuracy, I hope so.

    BR,
    -Gusse-

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    ADCMin is the Minimum ADC Value we can have. ADCMax is the Maximum ADC Value we can have. Every routine must know it's limitations so first lets make sure we stay in range...
    Code:
    If ADC < ADCMin then ADC=ADCMin
    If ADC > ADCMax then ADC=ADCMax
    Now let's convert our ADC value... which varies between ADCMin (0%) and ADCMax (100%) but to an INVERSE Percentage
    Code:
    ADC=100-(((ADC-ADCMin)*100)/(ADCMax-ADCMin))
    Our PWM varies between PWMMin (0%) and PWMMax(100%), so here let's calculate the PWMOut Value we need by Applying our previously calculated Percentage and not forgetting to add-in our minimum offset
    Code:
    PWMOut=(((PWMMax-PWMMin)*ADC)/100)+PWMMin
    Four lines... and unless I've dropped a clanger - that should do the job.

    Put some figures into the variables, pencil, paper and calculator, and see if it works for you...

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Melanie,
    I have a question about your post...
    What does
    b*****k
    mean???
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    hmmm... consider it an an English engineering technical term... which can be used in the plural... forgot we have an international audience...

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    hmmm... consider it an an English engineering technical term... which can be used in the plural... forgot we have an international audience...
    Well I am just an old hillbilly that never did so good in English
    I even looked in the manual. Seeing that you changed the previous post I guess I was looking in the wrong manual

    Keep having fun!!!
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Aug 2005
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Melanie,

    Seems your code works spot on

    Thankyou

Similar Threads

  1. 16F684 adcin and hpwm
    By astouffer in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 21st November 2008, 17:29
  2. adcin and HPWM
    By lerameur in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 20th September 2008, 16:49
  3. sound command & music
    By trying in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th May 2006, 14:14
  4. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43

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