Shop OBEX P1 Docs P2 Docs Learn Events
Having problems getting the LTC1298 to read more accurately. — Parallax Forums

Having problems getting the LTC1298 to read more accurately.

pimp200277pimp200277 Posts: 22
edited 2007-06-05 16:09 in BASIC Stamp
Hi,
My problems is I can't seem to get my ADC - LTC1298 (12-bit analog to digital converter) to read my pH sensor accurately enough.

My pH sensor output for what I am measuring·is suppose to be ·about 2.25volts.
so after converting the analog reading to voltage·using this formula··Vref *·analog reading / 4096
I am only getting for an output the number·" 2 ".

CODE:

' {$STAMP BS2}
' {$PBASIC 2.5}
' Pin Assignments
CS····· CON·· 9······· ' Chip Select
DIO_10· CON·· 10······ ' Data I/O pin 10
CLK···· CON·· 11······ ' Clock
' LTC1298 configuration

AD····· VAR·· Word···· ' Variable to hold 12-bit AD result.
config· VAR·· Nib
v······ VAR·· Byte

HIGH CS··············· ' Deactivate ADC to begin.
HIGH DIO_10··········· ' Set data pin for first start bit.
again:
· GOSUB convert···················· ' Get data from ADC.
· GOSUB calc_volt
· DEBUG· "A/D Reading: ",DEC AD,CR
· DEBUG· "volt Reading: ",DEC v, CR

· PAUSE 500······························· ' Wait a half second.
GOTO again································ ' Endless loop.
Calc_Volt:
v = 5 * AD / 4096
convert:
config = config | %1011
· LOW CS·································· ' Activate the ADC.
· SHIFTOUT DIO_10,CLK,LSBFIRST,[noparse][[/noparse]config\4]· ' Send config bits.
· SHIFTIN DIO_10,CLK,MSBPOST,[noparse][[/noparse]AD\12]······ ' Get data bits.
· HIGH CS································· ' Deactivate the ADC.
RETURN···································· ' Return to program.

Results:

A/D Reading: 2129
volt Reading: 2
A/D Reading: 2128
volt Reading: 2
A/D Reading: 2126
volt Reading: 2
A/D Reading: 2130
volt Reading: 2
A/D Reading: 2126
volt Reading: 2
I tired putting "DEC3" instead of "DEC" on the Debug volt reading line and I got this:

A/D Reading: 2127
volt Reading: 002
A/D Reading: 2129
volt Reading: 002
A/D Reading: 2127
volt Reading: 002
A/D Reading: 2128
volt Reading: 002
A/D Reading: 2129
volt Reading: 002

Any Suggestions on what I should try?

Comments

  • ghost13ghost13 Posts: 133
    edited 2007-06-05 04:05
    Isn't the Basic Stamp only capable of whole numbers... not decimals?

    Try multiplying your equation by 100, and then the output should be 225.
    Then, you could print that, or if you really wanted to, parse it and add a "."
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-06-05 04:20
    There should be a RETURN at the end of the calcvolt routine. Otherwise it will go right on into the convert routine before returning.

    That is not the problem though. In convert use this equation instead of 5*AD/4000:

    v = 8000 ** AD

    Then the answer when AD=2129 will be 259, which you can display with a decimal point...
    DEBUG "volt Reading: ",DEC v/100,".",DEC2 v, CR


    Why 8000 ** AD? That is the same as 8000/65536, where the division by 65536 is implied in the ** operator. And the fraction you want is 500/4096 = 8000/65536.

    Need more accuracy for roundoff? Use

    v = 40000 ** AD * 2
    DEBUG "volt Reading: ",DEC v/1000,".",DEC3 v, CR

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • pimp200277pimp200277 Posts: 22
    edited 2007-06-05 04:23
    I thought the stamp could do decimals since I was looking at the Basic Analog to Digital Book on parallax : http://www.parallax.com/dl/docs/books/edu/baad.pdf in Chapter 4 Pages 72 and 73 and in the results on page 73 it showed the results in a decimal.

    I will give your suggestion a try.
  • pimp200277pimp200277 Posts: 22
    edited 2007-06-05 04:37
    @Tracy I tried your method and the AD reading comes out right but the Decimal reading values are way off.
    The results were:

    A/D Reading: 2128
    volt Reading: 0.03
    A/D Reading: 2129
    volt Reading: 0.03
    A/D Reading: 2129
    volt Reading: 0.03
    A/D Reading: 2126
    volt Reading: 0.03
    A/D Reading: 2127
    volt Reading: 0.03
    A/D Reading: 2125
    volt Reading: 0.03
    A/D Reading: 2126
    volt Reading: 0.03
    A/D Reading: 2126
    volt Reading: 0.03
    A/D Reading: 2127
    volt Reading: 0.03
    A/D Reading: 2127
    volt Reading: 0.03
    A/D Reading: 2129
    volt Reading: 0.03
    A/D Reading: 2124
    volt Reading: 0.03
    A/D Reading: 2129
    volt Reading: 0.04
  • pimp200277pimp200277 Posts: 22
    edited 2007-06-05 04:48
    Ok, I took part of what you said Ghost " multiplying my equation by 100" Then I took part of what you said Tracy " Displaying the answer with a decimal point" and I was able to get a better result:

    A/D Reading: 1882
    volt Reading: 2.02
    A/D Reading: 1874
    volt Reading: 2.02
    A/D Reading: 1869
    volt Reading: 2.02
    A/D Reading: 1852
    volt Reading: 2.02
    A/D Reading: 1835
    volt Reading: 2.02
    A/D Reading: 1826
    volt Reading: 2.02
    A/D Reading: 1806
    volt Reading: 2.02
    A/D Reading: 1789
    volt Reading: 2.02
    A/D Reading: 1781
    volt Reading: 2.02
    A/D Reading: 1773
    volt Reading: 2.02
    A/D Reading: 1764
    volt Reading: 2.02
    A/D Reading: 1759
    volt Reading: 2.02
    A/D Reading: 1754
    volt Reading: 2.02
    A/D Reading: 1742
    volt Reading: 2.02
    A/D Reading: 1721
    volt Reading: 2.02
    A/D Reading: 1703
    volt Reading: 2.02
    A/D Reading: 1698
    volt Reading: 2.02
    A/D Reading: 1687
    volt Reading: 2.02
    A/D Reading: 1677
    volt Reading: 2.02
    A/D Reading: 1673
    volt Reading: 2.02
    A/D Reading: 1672
    volt Reading: 2.02
    A/D Reading: 1672
    volt Reading: 2.02
    A/D Reading: 1666
    volt Reading: 2.02
    A/D Reading: 1664
    volt Reading: 2.02
    A/D Reading: 1657
    volt Reading: 2.02
    A/D Reading: 1660
    volt Reading: 2.02
    A/D Reading: 1655
    volt Reading: 2.02
    A/D Reading: 1648
    volt Reading: 2.02

    So now I only have 1 problem how do I get the Volt Reading to Change along with the A/D Reading at the same time it Changes?
    As you can see from the results above the A/D readings are changing but the volt Reading remains the same.
  • pimp200277pimp200277 Posts: 22
    edited 2007-06-05 05:01
    Ok, I think I figured it out and am all set now. I got this
    CODE:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    ' Pin Assignments
    CS CON 9 ' Chip Select
    DIO_10 CON 10 ' Data I/O pin 10
    CLK CON 11 ' Clock

    ' LTC1298 configuration


    AD VAR Word ' Variable to hold 12-bit AD result.
    pH VAR Word ' pH value of the water
    config VAR Nib
    v VAR Byte
    v2 VAR Byte


    HIGH CS ' Deactivate ADC to begin.
    HIGH DIO_10 ' Set data pin for first start bit.

    again:
    GOSUB convert ' Get data from ADC.
    GOSUB calc_volt
    DEBUG "A/D Reading: ",DEC AD,CR
    DEBUG "volt Reading: ",DEC v/100,".",DEC2 v, CR


    PAUSE 500 ' Wait a half second.
    GOTO again ' Endless loop.

    Calc_Volt:

    v = 8000 ** AD
    v2 = v * 100
    RETURN

    convert:
    config = config | %1011
    LOW CS ' Activate the ADC.
    SHIFTOUT DIO_10,CLK,LSBFIRST,[noparse][[/noparse]config\4] ' Send config bits.
    SHIFTIN DIO_10,CLK,MSBPOST,[noparse][[/noparse]AD\12] ' Get data bits.
    HIGH CS ' Deactivate the ADC.
    RETURN ' Return to program.

    And these Results:

    A/D Reading: 1547
    volt Reading: 1.88
    A/D Reading: 1544
    volt Reading: 1.88
    A/D Reading: 1545
    volt Reading: 1.88
    A/D Reading: 1548
    volt Reading: 1.88
    A/D Reading: 1549
    volt Reading: 1.89
    A/D Reading: 1550
    volt Reading: 1.89
    A/D Reading: 1551
    volt Reading: 1.89
    A/D Reading: 1546
    volt Reading: 1.88
    A/D Reading: 1552
    volt Reading: 1.89
    A/D Reading: 1556
    volt Reading: 1.89
    A/D Reading: 1555
    volt Reading: 1.89
    A/D Reading: 1556
    volt Reading: 1.89
    A/D Reading: 1556
    volt Reading: 1.89
    A/D Reading: 1561
    volt Reading: 1.90
    A/D Reading: 1558
    volt Reading: 1.90
    A/D Reading: 1561
    volt Reading: 1.90
    A/D Reading: 1778
    volt Reading: 2.17
    A/D Reading: 1781
    volt Reading: 2.17
    A/D Reading: 1778
    volt Reading: 2.17
    A/D Reading: 1763
    volt Reading: 2.15
    A/D Reading: 1766
    volt Reading: 2.15
    A/D Reading: 1761
    volt Reading: 2.14
    A/D Reading: 1761
    volt Reading: 2.14
    A/D Reading: 1775
    volt Reading: 2.16
    A/D Reading: 1761
    volt Reading: 2.14
    A/D Reading: 1757
    volt Reading: 2.14
    A/D Reading: 1754
    volt Reading: 2.14
    A/D Reading: 1757
    volt Reading: 2.14
  • pimp200277pimp200277 Posts: 22
    edited 2007-06-05 05:42
    Another Question is there a way to just get the program to just list 2 results and have the numbers change
    without having to make a new line everytime there's a new value. I just want 2 lines of results:
    1 - A/D reading
    1 - volt reading

    That keep changing values without having the results keep listing like in my post above
  • RDL2004RDL2004 Posts: 2,554
    edited 2007-06-05 15:44
    There's a command to have DEBUG place the cursor in a specific spot before printing. I think the command is:

    DEBUG CRSRXY, #, #

    Where #,# is the x, y location (column, row I think - and they start at 0) You don't have to reprint the whole line either, just the part that changes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Rick
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-06-05 16:09
    Use the HOME and CLREOL control characters.

    DEBUG HOME, "A/D Reading: ",DEC AD,CLREOL,CR
    DEBUG "volt Reading: ",DEC v/100,".",DEC2 v,CLREOL,CR

    You can read about the control characters in the manual along with the DEBUG command.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.