ADCIN & 16F870 - how to avoid non linear conversion?


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,124

    Default ADCIN & 16F870 - how to avoid non linear conversion?

    Hello.

    I've built a simple circuit, with 16F870 and MAX6126 as reference voltage supply.

    The problem is that I can't write a code that will properly convert read data into voltage.

    By using external multimeter, I've determined that with 678 read by ADC, voltage is 3 volts. Based on this, I've created a conversion formula, but it does not appears to work correctly, for example, when input is 4.1v, it reads as 4.35 volts. And when input is 1.4V, it reads as 1V. The code is as follows (based on :

    Code:
    Include "modedefs.bas"  ' Include serial modes
       TRISA = %11111111       ' Set PORTA to all input
       ADCON1 = %10000001      ' Set PORTA analog and right justify result
    
    DEFINE LCD_DREG PORTB
    DEFINE LCD_DBIT 4
    DEFINE LCD_RSREG PORTB
    DEFINE LCD_RSBIT 0
    DEFINE LCD_EREG PORTB
    DEFINE LCD_EBIT 1
    DEFINE LCD_BITS 4 
    DEFINE LCD_LINES 2
    DEFINE LCD_COMMANDUS 1500
    DEFINE LCD_DATAUS 44
    DEFINE OSC 4
    
    Define  ADC_BITS     10    ' Set number of bits in result
    Define  ADC_CLOCK    3     ' Set clock source (3=rc)
    Define  ADC_SAMPLEUS 50    ' Set sampling time in uS
    
    adval Var word             ' Create adval to store result 
    droebiti var word 'temporal value
    
    mainloox:
       ADCIN 0, adval          ' Read channel 0 to adval
       droebiti=adval*44 'convert to volts
       
       Lcdout $fe,$c0, "Value: ", DEC adval, "    "  ' Display the decimal value 
       Lcdout $fe,$80, "Voltage: ",  DEC (droebiti / 10000), ".", DEC4 droebiti, " V " 
       Pause 100               ' Wait .1 second
    Goto mainloox           ' Do it forever
    What I'm doing wrong?

    I'm using 10k pot with middle pin tied to RA0, one pin tied to Vss and another to voltage reference output. This might be the issue?

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


    Did you find this post helpful? Yes | No

    Default Re: ADCIN & 16F870 - how to avoid non linear conversion?

    Hi,
    The MAX6126 comes in various versions but I suspect you're using the 5V one, since you're feeding in voltages above 4.096V I'm guessing you're using the 5V version of the MAX6126 - is that correct?

    Your numbers don't quite add up.... With a VRef of 5V an input of 3V should give you a reading of (1024/5) * 3 = 614, you're getting 678. Have you followed the datasheet for the voltage reference regarding supply and output bypassing and have you verified that the VRef voltage actually IS what you expect it to be?

    /Henrik.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: ADCIN & 16F870 - how to avoid non linear conversion?

    You have ADCON1 set to use porta.3 as an input for a positive reference. Without a reference connected its floating

    Change ADCON1 to this
    Code:
     
    ADCON1 = %10000000
    then your references will be +Vref = vdd and -Vref = vss. Or you can connect porta.3 to +5v without changing adcon1

  4. #4
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: ADCIN & 16F870 - how to avoid non linear conversion?

    I'm using MAX6126 with output voltage of 4.098v and it outputs that voltage - verified with precision multimeter.
    The +Vref is tied to MAX6126 output and +Vref is tied to GND. So this is why I have ADCON1=%10000001
    When I tie RA0 directly to MAX6126 output, the raw output reads 1023, when I connect it to GND, it reads 0.

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


    Did you find this post helpful? Yes | No

    Default Re: ADCIN & 16F870 - how to avoid non linear conversion?

    Hi,
    OK, that's a great value because then each bit in the ADC result equals exactly 4mV but your numbers still doesn't add up....

    If you input exactly 3V you should get a value of 750 +/- a couple of bits - not 678 as you're apparently getting.

    Have you checked the VRef voltage with a scope? Perhaps it oscillates or have a lot of noise?

    What if you replace the 10k pot with a 1k pot, and put a 10nF capacitor between the ADC input and GND. Or, what if you increas the sampling time? (I'll admit, I don't really have this sampling time and Tad stuff clear in my head at the moment, need to do a little reading)

    Once you get a correct ADC result for the voltage you're feeding it you can increase the resolution by oversampling. If you take 40 samples, add them together and divide the result by 10 you have a value reading in mV (well, almost - there's 4mV "missing" at the top).
    Code:
    adval = 0
    For i = 0 to 39
      ADCIN 0, adval
      Voltage = Voltage + adval
      PAUSEUS 100
    NEXT
    
    LCDOUT $FE, 1, DEC Voltage/10000, ".", DEC3 Voltage // 10000
    /Henrik.

  6. #6
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: ADCIN & 16F870 - how to avoid non linear conversion?

    Ok I've double checked wiring and so on. When getting 1023, I was connecting the upper end of pot to +5V, not to MAX output. Now, when I connected it to MAX output, it reads 987, instead of 1023, so I assume, there might be some more register settings required?

Similar Threads

  1. How can I avoid stack corruption
    By BrianT in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th August 2013, 02:45
  2. linear data memory & large data arrays
    By ronsimpson in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 27th June 2011, 23:21
  3. How to avoid DIV32 command?
    By pxidr84 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 16th May 2011, 06:11
  4. Help needed with 16F870 ADC
    By srob in forum mel PIC BASIC
    Replies: 1
    Last Post: - 10th May 2008, 13:14
  5. Linear to S Curve conversion
    By luminas in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 22nd May 2007, 14:15

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