Shop OBEX P1 Docs P2 Docs Learn Events
TLC2543, reading 12 bits of data?? — Parallax Forums

TLC2543, reading 12 bits of data??

fiveslofiveslo Posts: 40
edited 2007-04-10 04:33 in General Discussion
I've been trying this for awhile now and just can't seem to get the 12 bits of data from the TLC2543 A/D.. the code I'm using, posted below, is a hack/conversion of what I've found on Tracy Allen's website, emesystems.com, this combined with the code for the Parallax compass module, since I had the example of reading 11 bits? of data in the sample code for the SX, anyways here is what I've found, with the code presented, I can only get from I observe as 8 bit data, meaning that in the result watch window, my result variable only gives back a 0-255 value. I think I'm reading in the result as a byte array? but cannot get the display to ever show anything beyond 255, then when I tried to hack the line:

result=14464**result+result

from Tracy's program I got errors from the compiler, after researching I found that you can't supposedly multiply a variable by itself or something to that effect, which from what I gather I'm trying to do. Can someone, anyone please point me in the right direction and/or tell me what's wrong with my SX/B program, on how to modify the BS2 program to work on the SX, using SX/B. I'm a complete newbie with this SX/B stuff and completely lost, I have gotten the results from the BS2 program to work flawlessly, and now am trying to construct this program and circuit to accept a serial string from my BS2p to get a voltage reading from any of the channels of the TLC2543 on demand, which the SX/28 will then serially transmit back to the BS2p.. but in any case my example is only dealing with channel 0, and I still can't seem to get it to work... Please help..... Rob


' =========================================================================
'
' File...... TLC2543.SXB
' Purpose... Read inputs from TLC2543 A/D and transmit results serially.
' Author.... RKacer
' E-mail....
' Started... 30 MARCH 2007
' Updated... 08 APRIL 2007
'
' =========================================================================

'
' Program Description
'
'
' Connections:
'
' RC.7 ----> clock out from SX/28 to AD2543
' RC.6 ----> data out from SX/28 to AD2543 sdi
' RC.5 <---- data in from AD2543 sdo to SX/28
' RC.4 ----> AD2543 chip select, active low
' RC.3 ----> End Of Converion pin from AD2543
' RA.0 ----> Serial Output of result
'
' Components:
'
' Texas Instruments TLC2543, 12bit 11 channel A/D converter.
'
'
' Device Settings
'

DEVICE SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ 4_000_000


'
' IO Pins
'

sclk VAR RC.7 ' clock out from SX/28 to AD2543
sdo VAR RC.6 ' data out from SX/28 to AD2543 sdi
sdi VAR RC.5 ' data in from AD2543 sdo to SX/28
ADcs VAR RC.4 ' AD2543 chip select, active low

'
' Constants
'


'
' Variables
'

ADch VAR Byte ' selects AD external channel 0-10
result VAR Byte (2) ' result, 12 bit A/D conversion

'
' Watch Directives
'

WATCH result,12,SDEC ' display 12-bit result variable
WATCH result.0,12,SDEC
WATCH result.1,12,SDEC

' =========================================================================
PROGRAM Start
' =========================================================================

'
' Subroutine Declarations
'



'
' Program Code
'

Start:
HIGH ADcs ' make CS output
LOW sclk ' make clock 0-1-0

Main:
DO
LOW ADcs ' select chip
GOSUB ADwake
ADch=0 ' acquire TLC2543 channel 0
GOSUB ADread
BREAK() ' update result variable in watch window

LOOP
END
'
' Subroutine Declarations
'

ADread:
LOW ADcs
SHIFTOUT sdo,sclk,MSBFIRST,ADch<<8\12
SHIFTIN sdi,sclk,MSBPRE,result(1)\4 ' get result, 12 bits <--- the next two shiftin lines are cut and modified? from the Parallax compass app for SX/B
SHIFTIN sdi,sclk,MSBPRE,result(0) <--- I changed the "/3" in the compass app to "/4" since the compass app looks for? 11 bits????
HIGH ADcs ' deselect chip

'result=14464**result+result <
commented out because it fails miserably and doesn't seem to agree with the compiler...

RETURN

ADwake:
ADch=$b
GOTO ADread

Comments

  • JonnyMacJonnyMac Posts: 8,957
    edited 2007-04-09 14:42
    Unlike PBASIC, SX/B does not allow inline expressions as you have in the SHIFTOUT line. Evaluate that expression (adCh << 8) on the line before and then give it a go. Also, you can only have one operator per line, so your final result calculation will take two lines (and another variable; PBASIC uses internal vars that you have to do manually).

    frac = result ** 14463
    result = result + frac
    



    BTW, you can declare result as a word and then get to its bytes with result_LSB and result_MSB (these are created by the compiler). After looking again, you should define your adc channel variable, result, and a temporary variable as words, then try this:

    SUB AD_READ:
      LOW ADcs
      adChan = adChan << 8
      SHIFTOUT SDO, SClk, MSBFIRST, adChan\12
      SHIFTIN  SDI, SClk, MSBPRE, result\12 
      HIGH ADcs 
    
      tmpW1 = result ** 14464
      result = result + tmpW1
      ENDSUB
    



    You really should code this as a function pass parameters back and forth -- but get this working first and then make that adjustment.

    Post Edited (JonnyMac) : 4/9/2007 3:02:02 PM GMT
  • fiveslofiveslo Posts: 40
    edited 2007-04-10 03:03
    JonnyMac:
    After I substituted the code that you suggested:

    SUB AD_READ:
      LOW ADcs
      adChan = adChan << 8
      SHIFTOUT SDO, SClk, MSBFIRST, adChan\12
      SHIFTIN  SDI, SClk, MSBPRE, result\12 
      HIGH ADcs 
    
      tmpW1 = result ** 14464
      result = result + tmpW1
      ENDSUB
    

    ·I get a "Error 11, BYTE VARIABLE EXPECTED "result"· on the line,
    SHIFTIN··SDI,·SClk,·MSBPRE,·result\12·. I scanned thru the help file of SX/B and found something which caught my eye, that being the \COUNT parameter of SHIFTIN and SHIFTOUT, ranges from 1-8, is this true or was this an update when WORD VARs·were added to SX/B?· In any case I substituted as I think you suggested:

    SHIFTIN sdi,sclk,MSBPRE,result_MSB\4          ' get result, 12 bits
    SHIFTIN sdi,sclk,MSBPRE,result_LSB
    

    What I see in the watch window is a 12bit result, yet kind of weird... in the case that the range goes from
    0 to +2048, and when +2048 rolls over it changes to -2048 and goes back down to 0 counting the whole way negatively.. does that make sense??· I'm seeing the 0-4095 range however, not in a 0-4095 way, rather in a 0-2048 then -2048-0... is my WATCH statement responsible for this...
    WATCH   result,12,SDEC
    


    I'm guessing yes, if so how do I fix this???· As for the code:

    tmpW1 = result ** 14464
    result = result + tmpW1
     
    
    

    will this work with the result_LSB and result_MSB mods to result?· Reason I ask is this part after my _LSB and _MSB mods didn't make the compiler happy... I've posted my refined but still not working code below...· Thanks again for your input.. Rob

    Start:
      HIGH ADcs                                     ' make CS output
      LOW sclk                                      ' make clock 0-1-0
    

    Main:
      DO 
        LOW ADcs                                ' select chip
        ADch=$b                                 ' make dummy conversion to intialize converter
        GOSUB ADread
        ADch=0                                  ' acquire TLC2543 channel 0
        GOSUB ADread
        BREAK()                                 ' update result variable in watch window
        LOOP
        END
    

    ADread:
      LOW ADcs
      ADch=ADch<<8
    

    SHIFTOUT sdo,sclk,MSBFIRST,ADch\12        ' Can the \COUNT be greater than 8, instructions for SX/B say no!
      'SHIFTIN sdi,sclk,MSBPRE,result\12        ' <--- Error 11, BYTE VARIABLE EXPECTED "result"  COMPILER GENERATED ERROR!
    

      SHIFTIN sdi,sclk,MSBPRE,result_MSB\4          ' get result, 12 bits
      SHIFTIN sdi,sclk,MSBPRE,result_LSB
      HIGH ADcs                                    ' deselect chip
    

      RETURN
    

    
                            
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-04-10 03:25
    Rob,

    You are correct in thinking that your watched variable data is being interpreted as a signed number. Declare your watched variable to be UDEC (Unsigned Decimal) rather than SDEC (Signed Decimal). Additional WATCH command help can be found Under Help | Watch Window Help.


    As far as I know SHIFTIN can not process more than 8 bits at a time. The last paragraph of the SX/B Help file entry for the SHIFTIN command shows how to shift in 12 bits of data using two SHIFTIN statements. It looks like you have implemented that correctly. That should work, placing the full 12 bits into your result word variable.

    - Sparks
  • JonnyMacJonnyMac Posts: 8,957
    edited 2007-04-10 04:33
    Version 1.51.03 allows words in SHIFTOUT and a bit count of up to 16 so

    SHIFTOUT SDO, SCLK, MSBFIRST, adCh\12
    



    is valid (I tested it before posting my response). Sorry, though, that I didn't double-check the same thing on SHIFTIN; in fact, just now looking in the help file is specifies bytes only in SHIFTIN, so you do have to use the form

    SHIFTIN SDI, SCLK, MSBPRE, result_MSB\4
    SHFITIN SDI, SCLK, MSBPRE, result_LSB
    



    WATCH has recently been added as an SX/B keyword and it's not quite as flexible as the assembly counterpart. I do this in my programs:

    \ WATCH result, 12, udec
    



    The \ specifies inline assembly, "watching" the the lower 12 bits of result, using unsigned decimal output. I just did a quick little debug test on a two-line program

    result = %0000_1111_1111_1111
    \ watch result, 12, udec
    



    and the value in result was correctly specified as 4095.

    Post Edited (JonnyMac) : 4/10/2007 4:40:43 AM GMT
Sign In or Register to comment.