Interfacing with a TLC2543 11-Ch 12-bit A/D


' Program TLC2543_1.Bas
'
' Illustrates how to perform a single measurment on a specific
' channel.
'
' Performs unipolar measurments on each of the 11 channels, displaying
' the results on the terminal (Com 1) and then powers down A/D.
'
' Note that the format of the command byte is;
'
'  C C C C L1 L0 LSB BIPOLAR
'
' where C is the channel (0 - 10).
' L1 and L0 = 11 binary indicates 16-bit format.
' LSB is zero for most sig byte first.
' BIPOLAR is 0 for unipolar measurement.
'
' The command byte is sent followed by a dummy byte so as to send 16 bits.
'
' This is followed by a 16-bit read of the result where the 12-bit result
' is in the high 12-bits.  This is shifted right (\16) such that the result
' is in the lower 12 bits.
'
' Note that in TLC2543_2, the process is speeded up a bit by sending the
' command for the next measurement while the previous result is being
' fetched.
'
' BX24          TLC2543
'
' MISO <---------------- D_Out (Term 16)
' MOSI ----------------> D_in (Term 17)
' SCLK ----------------> CLK (Term 18)
'
' /CS (term 13) -------> /CS (Term 15)
'
' See http://www.ti.com for a data sheet.
'
' copyright, Peter H. Anderson, Baltimore, MD, Sept, '00

Const CSPin as Byte = 13

Sub Main()

   Dim N as Byte, Val as Integer, SPISetUpByte as Byte, SPIChannel as Byte
   Dim BiPolar as Byte
   Dim Str as String *10
   Str = " ... "    ' used for debugging

   Call OpenSerialPort(1, 19200)
   SPISetUpByte = 1
   SPIChannel = 1
   BiPolar = 0      ' unipolar measurements
   Call OpenSPI(SPIChannel, SPISetUpByte, CSPin)

   Do
     For N = 0 to 10
        Val = TLC2543SingleMeas(SPIChannel, N, BiPolar)
        ' SPIChannel, Channel N, Bipolar
        Call PutB(N)
        Call PutStr(Str)
        Call PutI(Val)
        Call NewLine()
        Call Sleep(0.5)
      Next

      Call TLC2543Sleep(SPIChannel)
      Call Sleep(5.0)
   Loop
End Sub

Function TLC2543SingleMeas(ByVal SPIChannel as Byte, _
                           ByVal Channel as Byte, _
                           ByVal BiPolar as Byte) _
                           as Integer

  Dim PutData(1 to 2) as Byte, GetData(1 to 2) as Byte

  PutData(1) = Channel * 16 + bx00001100 + BiPolar
           ' channel plus 16-bit mode + BiPolar
  PutData(2) = bx00000000

  Call SPICmd(SPIChannel, 2, PutData(1), 0, GetData(1))
    ' send command byte followed by dummy byte
  Call Sleep(0.05)
        ' receive 16 bit result
  Call SPICmd(SPIChannel, 0, PutData(1), 2, GetData(1))

'  Call PutB(GetData(1))
'  Call PutByte(Asc(" "))
'  Call PutB(GetData(2))
'  Call NewLine()
  ' Put the high and low bytes together.  Result is in lower 12-bits.
  TLC2543SingleMeas = CInt(GetData(1)) * 16 + CInt(GetData(2))\16

End Function

Sub TLC2543Sleep(ByVal SPIChannel as Byte)
  Dim PutData(1 to 2) as Byte, GetData(1 to 2) as Byte

  PutData(1) = &H0e * 16 + bx00001100  ' sleep plus 16-bit mode
  PutData(2) = bx00000000

  Call SPICmd(SPIChannel, 2, PutData(1), 0, GetData(1))
    ' send command byte followed by dummy byte
End Sub