Shop OBEX P1 Docs P2 Docs Learn Events
Problem with 12-bit AD converter — Parallax Forums

Problem with 12-bit AD converter

TrangTrang Posts: 9
edited 2009-06-22 16:21 in BASIC Stamp
Hello everyone!

I'm a newbie and I'm trying to interface a BS2e with the 12-bit AD converter LTC1298 CN8. I'm stuck at a problem that I haven't been able to figure out for 2 days now [noparse]:([/noparse] Could anyone please help me?

I got the sample code from: LTC1298 App Kit
and I modified it a little bit to just read the result from channel 0. Here is my code:

' {$STAMP BS2e}
' {$PBASIC 2.5}

CS        PIN   4
CLK       PIN   7
DIO       PIN   6
AD        VAR   Word


HIGH CS
HIGH DIO

config=%1001

again:
 GOSUB convert
 DEBUG "Channel 0: ", DEC AD, CR
 PAUSE 200
GOTO again

convert:

 LOW CS
 SHIFTOUT DIO, CLK, MSBFIRST, [noparse][[/noparse]config\4]
 SHIFTIN DIO, CLK, MSBPOST, [noparse][[/noparse]AD\12]
 HIGH CS
RETURN




I wanted to be consistent so I made every data transfer to be MSB first. The sample code used LSBFIRST instead. But either way, I couldn't any meaningful data. It kept showing 0 as the result.

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2009-06-18 16:40
    I don't have the data sheet in front of me but I've used the sample code for years without problems. The MSB/LSB choice may not be yours to make as it is defined in how the LTC1298 works.

    I'd back up and get the chip working with the un-modified code. Usually, all you want to change is which channel is being converted.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • TrangTrang Posts: 9
    edited 2009-06-18 17:31
    Hi stamptrol,

    Thanks for your reply and suggestions. Testing the AD converter with the unmodified code was the first thing I did actually. But it didn't work either. I kept getting 0. I also checked all my wiring many, many times...

    Is there anything tricky about this besides all that was mentioned in the datasheet? (eg: adding resistors, capacitors, or maybe even using external voltage supply.... ? -- anything of that sort that was not said in the datasheet?)

    Also. I was wondering if it is the chip itself that's defective... Do they tend to break down easily?
  • stamptrolstamptrol Posts: 1,731
    edited 2009-06-18 18:36
    I've never had a dead chip among the LTC1298, but its always possible. You haven't inadvertently put high voltage on the inputs ( > 5 volts)?

    I assume you are using the correct pins on the chip and have the corresponding Stamp pins connected as per the diagram. You have the chip powered up. If using separate supplies for the chip and the Stamp. tie the 0 volt (gnd) together.

    Normally, the only extra resistor is the one connected to the Data in and Data out pins at the chip. With no voltages on the input channels, they will float a bit and show some spurious value. A reading of exactly zero sounds a bit suspicious. Using a pull-down of 20K - 100K pulls them down close to 0.

    Heres my conversion code from a project a couple of weeks ago. The main program just sets the variable "oddsign" then calls this code. It always returns two values, one for each channel. This is not the actual code, but should work as a test if you declare variables, etc.

    CS PIN 5 ' a/d Chip select; 0 = active
    DIO_n PIN 7 ' a/d Chip Data I/O pin _number_.
    Clk PIN 8 ' a/d Chip Clock to ADC; out on rising, in on falling edge.

    main:
    FOR oddSign = 0 TO 1 ' Toggle between input channels.
    GOSUB convert ' Get data from ADC.
    PAUSE 500 ' Wait a half second.
    NEXT ' Change channels.
    debug dec4 chan1val, " ", dec4 chan2val
    goto main


    convert:
    config = config | %1011 ' Set all bits except oddSign.
    LOW CS ' Activate the ADC.
    SHIFTOUT DIO_n,CLK,LSBFIRST,[noparse][[/noparse]config\4] ' Send config bits.
    SHIFTIN DIO_n,CLK,MSBPOST,[noparse][[/noparse]AD\12] ' Get data bits.
    IF oddSign=0 THEN chan1
    ret1:
    IF oddSign=1 THEN chan2
    ret2:
    HIGH CS ' Deactivate the ADC.
    RETURN ' Return to program.

    chan1:
    chan1val=AD
    GOTO ret1
    chan2:
    chan2val=AD
    GOTO ret2


    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • denodeno Posts: 242
    edited 2009-06-18 22:16
    I been using the LTC1298 for sometime now, and here is my code that is used to produce two channels.· One to read battery voltage and the other channel to read amps.· Hope this helps...I think part of your problem is how you are configuring the LTC1298.· I should note that I am using an Allegro Hall Effect Current device to measure plus or minus 130 amps.· This device interfaces very easily with the LTC1298, as the output is 0 to 5 volts, corresponding to -130a @ 0volt, 0a @ 2.5volt, and +130a @ 5volt.

    I take 3 readings in a row, for averaging purposes.

    DenO

    '===CONFIG LTC1298 =======
    ···· config_volt = config_volt | %1111· 'Set all configure bits for the LTC1298 to measure voltage on channel 1
    ···· config_amps = config_amps | %1011·'Set all configure bits for the LTC1298 to measure amps on channel 2

    '====MAIN PROGRAM========
    ······· 'Important number is the fact that their are 273.25 adcIndex's per volt.
    doItAgainSam:
    · GOSUB display_the_volts
    · GOSUB display_the_amps
    · GOTO doItAgainSam
    '====SUB ROUTINE TO COMPUTE AMPS AND DISPLAY IT====
    display_the_amps:
    · OUT0 = 0································ 'Activate the ADC.
    · SHIFTOUT 2, 1,LSBFIRST,[noparse][[/noparse]config_amps\4]·· 'Send config bits.
    · SHIFTIN· 2, 1,MSBPOST,[noparse][[/noparse]ADCindex1\12]···· 'Get data bits.
    · OUT0 = 1

    · OUT0 = 0································ 'Activate the ADC.
    · SHIFTOUT 2, 1,LSBFIRST,[noparse][[/noparse]config_amps\4]·· 'Send config bits.
    · SHIFTIN· 2, 1,MSBPOST,[noparse][[/noparse]ADCindex2\12]···· 'GEt DATa bits.
    · OUT0 = 1

    ··OUT0 = 0································· 'Activate the ADC.
    · SHIFTOUT 2, 1,LSBFIRST,[noparse][[/noparse]config_amps\4]·· 'Send config bits.
    · SHIFTIN· 2, 1,MSBPOST,[noparse][[/noparse]ADCindex3\12]···· 'GEt DATa bits.
    · OUT0 = 1
    · adcIndex_amps_average =((ADCindex1 + ADCindex2 + ADCindex3) / 3)
    · DEBUG DEC ? adcIndex_amps_average, CR
    · RETURN
    '====SUB ROUTINE TO COMPUTE VOLTAGE AND DISPLAY IT=========================
    display_the_volts:
    · OUT0 = 0································ 'Activate the ADC.
    · SHIFTOUT 2, 1,LSBFIRST,[noparse][[/noparse]config_volt\4]·· 'Send config bits.
    · SHIFTIN· 2, 1,MSBPOST,[noparse][[/noparse]ADCindex1\12]···· 'Get data bits.
    · OUT0 = 1
    · PAUSE 50······························· 'wait for another reading for a better average
    · OUT0 = 0································ 'Activate the ADC.
    · SHIFTOUT 2, 1,LSBFIRST,[noparse][[/noparse]config_volt\4]·· 'Send config bits.
    · SHIFTIN· 2, 1,MSBPOST,[noparse][[/noparse]ADCindex2\12]···· 'GEt DATa bits.
    · OUT0 = 1
    · PAUSE 50
    · OUT0 = 0································ 'Activate the ADC.
    · SHIFTOUT 2, 1,LSBFIRST,[noparse][[/noparse]config_volt\4]·· 'Send config bits.
    · SHIFTIN· 2, 1,MSBPOST,[noparse][[/noparse]ADCindex3\12]···· 'GEt DATa bits.
    · OUT0 = 1
    · adcIndex_average = (ADCindex1 + ADCindex2 + ADCindex3) / 3· 'average adc for smoothing effect...
    · DEBUG DEC ? adcIndex_average, CR
    · RETURN
  • TrangTrang Posts: 9
    edited 2009-06-19 04:08
    Thank you stamptrol and deno for your replies. This is really helpful!
    I'll post some update when I figure out my problem...
  • TrangTrang Posts: 9
    edited 2009-06-22 15:21
    Hey guys,

    Thank you very much for all your help. It seems that now I know where the problem lies but I don't know how to fix it. The problem is with the chip select part.

    Today I did this test: I kept chip select low all the time: this didn't work and I still kept getting 0.
    However, when I made CS low, then right after that pull the wire that grounds it out, it read meaningful data.

    First I suspected that it was because the set CS low-high happened too fast for AD conversion, but even when I added a pause between LOW-ing CS and shiftout, shiftin, it didn't work either...

    I'm really curious why I'm getting this problem... Any ideas? I really appreciate your help!
  • TrangTrang Posts: 9
    edited 2009-06-22 16:21
    OH yay! I changed the CS to a different pin. And it works now! So excited!!!!
Sign In or Register to comment.