Scaling ADC Result to a Set Range


Closed Thread
Results 1 to 40 of 45

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    Hi,
    I don't know exactly how you expect it to work but if Iäm not mistaken your formula, when re-written looks like:
    Code:
    MotorDuty = ADCInVAL * 252 / 255 + 100
    When ADCInVal = 1 Motor Duty will be 100
    When ADCInVAL = 255 MotorDuty will be 352

    Is that what you're seeing?

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    (MaxDuty - MinDuty) would be 250(2)-100= 150

    this would make more sense if your pwm value is a byte
    it now becomes
    Code:
    MotorDuty = ADCInVAL */ 150(2) + 100

    not knowing whats in the sub ChngMotorHPWM

    snippets lead to speculation

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    I'm using 8-bit resolution for PWM so it should be a byte but I have the variable defined as WORD in case the math takes it over 255:

    Code:
    MotorDuty   VAR WORD        ; Actual duty cycle for motor
    
    ChngMotorHPWM:
    
        CCP3CON.4 = MotorDuty.0
        CCP3CON.5 = MotorDuty.1
        CCPR3L    = MotorDuty >> 2
    
        CCP4CON.4 = MotorDuty.0
        CCP4CON.5 = MotorDuty.1
        CCPR4L    = MotorDuty >> 2
        
        RETURN
    Do you think the issue would be resolved if I change the datatype of MotorDuty to BYTE? I was also thinking I might switch to 10-bit resolution for both ADC and PWM to get the finest grain control of the motor speed, but that may be overkill.

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


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    > Do you think the issue would be resolved....

    What issue? What exactly is it that doesn't work the way you expect? What do you expect and what does it do?

    75% dutycycle (8bit resolution) +/-25% from an 8bit ADC readin:
    Code:
    PWMDuty = 191 + (64 - ADCValue >> 1)
    191 is 75% of 255, your initial PWM duty cycle. When ADCvalue (BYTE) is 0 PWMDuty (BYTE) will be 255=100%. When ADCValue is 255 PWMDuty will be 127=50%.

    /Henrik.

    /Henrik.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    The goal is to scale the ADC values of 0-255 to a range of duty cycles appropriate for my project (100-252 with the chosen prescaler). If I manually calculate the results are correct:
    • ADCInVal =0 yields 100
    • ADCInVal=127 yields 175
    • ADCinVal=255 yields 252

    But perhaps I don't have the order right in the expression because I don't see those results with the above code.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    from excel chart, multiply adc val by .6 and add 100........ but can't mult decimal so multiply by 6 then divide by 10 then add 100
    Attached Images Attached Images  
    Last edited by amgen; - 29th October 2015 at 21:03.

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


    Did you find this post helpful? Yes | No

    Default Re: Scaling ADC Result to a Set Range

    Hi,
    > but can't mult decimal
    Sure you can, sort of, by using the */ or ** operators.
    Code:
    PWMDuty = 100 + (ADCInValue ** 39322)  ' Same as 100 + ADCInValue * 0.600006 but likely faster than 100+ADCInValue*6/10
    /Henrik.

Similar Threads

  1. Replies: 4
    Last Post: - 27th January 2015, 15:34
  2. Replies: 4
    Last Post: - 30th May 2012, 08:28
  3. Converting 10bit ADC result to 8 bit
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 5th March 2012, 20:38
  4. strange A2D 5V scaling result - 16F819
    By Max Power in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th April 2010, 02:28
  5. Scaling ADC values
    By purkolator in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 29th November 2007, 05: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