12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct value


Closed Thread
Results 1 to 18 of 18

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Hi,
    Need to figure out how to get from 3272 to 5.06!
    Couple of ideas:
    Code:
    ADValue VAR WORD
    Temp VAR WORD
    Result VAR WORD
    
    ADValue = 3272
    Temp = ADValue ** 35809   ' Multiply by ~0.5464
    Result = ADValue + Temp     ' Result now 5059
    
    ' Or, same as above but without using the Temp variable:
    Result = ADValue + (ADValue ** 35809)
    
    ' Or, perhaps:
    ADValue = 3272
    Result = ADValue */ 396     ' Multiply by ~1.5469, Result now 5061
    
    'Display the value
    LCDOUT Result / 1000, ".", DEC3 Result // 1000
    /Henrik.

  2. #2
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    172


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Hi,

    If you change your voltage divider to a 50%-50% configuration (say 4k7 and 4k7) and then set the reference voltage for the ADC to internal 4.096V, each step of the ADC will equate 0.5mV
    Multiply your ADC reading by two and then manipulate the result to read "X.XXX Volts" per Henrik's LCDOUT routine.
    Yes, I understand there may be some slight inaccuracies but I have found this approach to be quite effecting in recent projects.

    Cheers
    Barry
    VK2XBP

  3. #3
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Henrik-
    That looks like a cool way to do this, I have never used these operators, but I will try them all, just to see more about them.
    It looks much more efficient than what I had done - multiple dividing......

    Thanks! I will let you know how it turned out.
    -Steve
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  4. #4
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Barry-
    I understand what you are saying, and yes, I am using the internal 4.1 reference - 1st time in fact to rely on an internal!. I was trying to use more of the available resolution on the ADC. If I do a 50% voltage divider, I have 2.5 volts against a 4.096 voltage reference. The divider I am using, uses more of the resolution of the 4.096 reference, 3.23 volts against 4.096 which gives me a little headroom above 5 volts.

    I have done that in the past, but felt that I left some resolution on the table. This way does work, perhaps not straightforward, but I think it does give me more bits of resolution that can be used.

    That being said, if I hadn't found a way (though it was more clunky than Henrik's) - I was sure thinking of doing just that!

    Thanks and Regards to All,
    Steve
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  5. #5
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Henrik-
    Sorry for the late reply, been out with a respiratory illness - geeesh.
    In your example :
    ADValue = 3272
    Temp = ADValue ** 35809 ' Multiply by ~0.5464
    Result = ADValue + Temp ' Result now 5059

    Why are you using 'Top 16 bits' for multiplying? and how did you arrive at '35809' as the multiplier?

    I really want to learn from this, Thanks.
    -Steve
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

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


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Hi,
    We want a "raw" display value of 5060 when the ADC returns 3272. That's a factor of 1.5464, right?
    The ** operator does a 16*16bit multiplication and then returns the 16 top bits of the intermediate 32bit result which is the same as dividing that intermediate result by 65536.

    So, on a calculator, 3272 * 35809 / 65536 = 1787 (truncated). Add in the "original" 3272 and we get 5059 which is pretty close to 5060.

    Just think of the ** operators as multiplying by units of 1/65536.
    65536*0.5464 = 35809...
    35809 * (1/65536) = 0.5464...
    And so on.

    /Henrik.

  7. #7
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Henrik-
    So, I kinda get the top 16 bits thing, but I still don't know where you got the number 35809?
    65536 * 0.5464 ----> shouldn't this be 65535 * 1.5464?
    -Steve
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

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


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Hi,
    65536 * 0.5464 ----> shouldn't this be 65535 * 1.5464?
    Idealy, yes. But that would result in a value larger than 65535 which won't work without resorting to LONGs. So I treat what's on the right hand side of the decimal point separately from what's on the left hand side which is why I first multiply ADValue by 0.5464 and then ADD the ADValue to that result to get the 1.5464. See if this makes more sense:
    Code:
    Result = ADValue + (ADValue ** 35809)   ' ~Same as ADValue * 1.5464
    Since you're using an 18F part you atleast have the option to use LONGs so if you ARE then by all means go ahead and try Result = ADValue ** 101345, it should work.

    Hope I don't make it more confusing....

    /Henrik.

  9. #9
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Is a bit more confusing........ BUT I will try what you had said. I just can't quite wrap my head about using sections of numbers....... I'll draw it out on paper and see if that helps....
    I'll get it though, may take me a bit.
    This forum is an amazing central clearing for knowledge and the fact that others are not only willing but patient is refreshing...
    Thanks,
    Steve
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

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


    Did you find this post helpful? Yes | No

    Default Re: 12 bit A2D on 18F46K80, need a bit of help for figuring TAD and getting correct v

    Hi Steve,

    On paper, Result = X * 3 is the same as Result = X+X+X, right?
    If the above is correct then Result = X * 1.5 is the same as Result = X + (X/2), right?
    And if we agree that the above is correct then I think we'll agree that Result = X + (X*0.5) is the same as Result = X * 1.5 too?

    Same thing with the formula in the previous message but the number is 1.5464 instead of 1.5.

    Again, ideally, we would want to do Result = ADValue ** 101345 (where 101345 is 65536*1.5464) but that will result in a compile time error if LONGs aren't enabled (because 101345 is larger than 65536 so there's a risk of overflowing the 32bit intermediate result thus returning the wrong value) so we resort to first calculating what 0.5464 is and then add that to the original value, exactly as we did in the example above.

    Don't worry, I'm sure you'll get it!

    /Henrik.
    Last edited by HenrikOlsson; - 22nd May 2014 at 19:47.

Similar Threads

  1. How do I use 10 bit A/D on 8 bit Pic? 12F675
    By polymer52 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st April 2020, 20:10
  2. Replies: 1
    Last Post: - 12th March 2012, 23:34
  3. Averaging 16 bit values without using 32 bit math
    By sirvo in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th October 2007, 22:18
  4. LCDOUT 4-bit data on 8-bit setting
    By breesy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 26th June 2006, 18:39
  5. Help 14-bit and 16-bit Core
    By jetpr in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th September 2005, 03:29

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