Connecting the TPA81 to the BS2, BS2e, BS2sx, BS2p  

Introduction
The TPA81 module uses the I2C bus for communication. This example shows how to connect the TPA81 module to the BS2 family. The SDA (data) and SCL (clock) lines are connected to pins P8 and P9 on the Stamp. These are the pins used by the BS2p for the I2CIN and I2COUT commands. The internal 5v regulator is not suitable for powering much external circuitry. I therefore recommend you use a separate 5v regulator.

Circuit Schematic for connecting a TPA81 Module to the Basic Stamp BS2p

The schematic above shows 4k7 pull-up resistors on the SCL and SDA lines to Vdd, as recommended by Parallax. 
For greater noise immunity, I recommend pulling up to the TPA81's 5v supply instead (so as not to place any more load on the BS2) and using 1k8 resistors.

Displaying temperature readings in a PC Debug window - BS2p Version
The following sample code will display the firmware revision, ambient temperature and the 8 pixel temperatures in a Debug window on the PC .
The sample code below can be downloaded here.


'{$STAMP BS2p}
'***********************************************************
'**                                                       **
'**        I2C Routines for the Basic Stamp BS2p          **
'**         with the TPA81 Thermal sensor module          **
'**                                                       **
'**             Copyright 2005 - Devantech Ltd            **
'**     Commercial use of this software is prohibited     **
'**     Private and educational use only is permitted     **
'**                                                       **
'**        Written by Gerald Coe - February 2005          **
'**                                                       **
'***********************************************************
'   Connect P8 (pin13) of the BS2p to the TPA81 SDA
'   Connect P9 (pin14) of the BS2p to the TPA81 SCL
'   Use 4k7 pull-up resistors from SCL and SDA to 5v supply
SDA     CON 8           ' SDA on pin13, SCL on pin14
Ver     VAR Byte
Ambient VAR Byte
Pix1    VAR Byte
Pix2    VAR Byte
Pix3    VAR Byte
Pix4    VAR Byte
Pix5    VAR Byte
Pix6    VAR Byte
Pix7    VAR Byte
Pix8    VAR Byte
Loop:
  I2CIN SDA, $d1, 0, [Ver, Ambient, Pix1, Pix2, Pix3, Pix4, Pix5, Pix6, Pix7, Pix8 ]
  DEBUG 2,0,1, "BS2p - TPA81 Demonstration "
  DEBUG 2,0,2, "Firmware Version ", DEC Ver, "    "
  DEBUG 2,0,3, "Ambient ", DEC Ambient, "    "
  DEBUG 2,0,4, "Pix1 Pix2 Pix3 Pix4 Pix5 Pix6 Pix7 Pix8"
  DEBUG 2,0,5, DEC3 Pix1, "  ", DEC3 Pix2, "  ", DEC3 Pix3, "  ", DEC3 Pix4, "  ", DEC3 Pix5, "  ", DEC3 Pix6, "  ", DEC3 Pix7, "  ", DEC3 Pix8
  GOTO Loop
   

 Displaying temperature readings in a PC Debug window - BS2, BS2e, BS2sx Versions
The following sample code will display the firmware revision, ambient temperature and the 8 pixel temperatures in a Debug window on the PC .
The sample code below can be downloaded here.

  
'{$STAMP BS2}
'***********************************************************
'**                                                       **
'**    I2C Routines for the Basic Stamp BS2/BS2e/BS2sx    **
'**         with the TPA81 Thermal sensor module          **
'**                                                       **
'**             Copyright 2006 - Devantech Ltd            **
'**     Commercial use of this software is prohibited     **
'**     Private and educational use only is permitted     **
'**                                                       **
'**          Written by Gerald Coe - March 2006           **
'**                                                       **
'***********************************************************
'   Connect P8 (pin13) of the BS2 to the TPA81 SDA
'   Connect P9 (pin14) of the BS2 to the TPA81 SCL
'   Use 4k7 pull-up resistors from SCL and SDA to 5v supply
SDA     CON 8      ' I2C data
SCL     CON 9      ' I2C clock
SDAin   VAR IN8
SDAout  VAR OUT8
SDAdir  VAR DIR8
Ver     VAR Byte
Ambient VAR Byte
Pix1    VAR Byte
Pix2    VAR Byte
Pix3    VAR Byte
Pix4    VAR Byte
Pix5    VAR Byte
Pix6    VAR Byte
Pix7    VAR Byte
Pix8    VAR Byte
I2cBuf  VAR Byte      ' I2c read/write buffer
I2cAddr VAR Byte      ' Address of I2C device
I2cReg  VAR Byte      ' Register number within I2C device
I2cData VAR Word      ' Data to read/write
I2cAck  VAR Bit       ' Acknowledge bit
  DEBUG 2,0,1, "BS2/BS2e/BS2sx - TPA81 Demonstration "
  DEBUG 2,0,2, "Firmware Version "
  DEBUG 2,0,3, "Ambient "
  DEBUG 2,0,4, "Pix1 Pix2 Pix3 Pix4 Pix5 Pix6 Pix7 Pix8"
  I2cAddr = $D0
  I2cReg = 0
MainLoop:
  GOSUB I2cTPA81Read    ' read data from TPA81
  DEBUG 2,17,2, DEC Ver, "    "
  DEBUG 2,8,3, DEC Ambient, "    "
  DEBUG 2,0,5, DEC3 Pix1, "  ", DEC3 Pix2, "  ", DEC3 Pix3, "  ", DEC3 Pix4, "  ", DEC3 Pix5, "  ", DEC3 Pix6, "  ", DEC3 Pix7, "  ", DEC3 Pix8
  GOTO MainLoop
'--------------------------------------------------------------------------------------------
' I2C subroutines follow
'--------------------------------------------------------------------------------------------
I2cTPA81Read:
  I2cAck = 1             ' send Ack
  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  GOSUB  I2cStart        ' repeated start
  I2cBuf = I2cAddr | 1
  I2cAck = 1             ' send Ack
  GOSUB  I2cOutByte      ' send device address (with read set)
  GOSUB  I2cInByte
  Ver = I2cBuf           ' read the data
  GOSUB  I2cInByte
  Ambient = I2cBuf
  GOSUB  I2cInByte
  Pix1 = I2cBuf
  GOSUB  I2cInByte
  Pix2 = I2cBuf
  GOSUB  I2cInByte
  Pix3 = I2cBuf
  GOSUB  I2cInByte
  Pix4 = I2cBuf
  GOSUB  I2cInByte
  Pix5 = I2cBuf
  GOSUB  I2cInByte
  Pix6 = I2cBuf
  GOSUB  I2cInByte
  Pix7 = I2cBuf
  I2cAck = 0             ' send Nak
  GOSUB  I2cInByte
  Pix8 = I2cBuf
  GOSUB  I2cStop
  RETURN
I2cByteWrite:        ' writes I2cData.lowbyte to I2cReg at I2cAddr
  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  I2cBuf = I2cData.LOWBYTE
  GOSUB  I2cOutByte      ' send the data
  GOSUB  I2cStop
  RETURN
I2cWordWrite:        ' writes I2cData to I2cReg at I2cAddr
  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  I2cBuf = I2cData.HIGHBYTE
  GOSUB  I2cOutByte      ' send the data - high byte
  I2cBuf = I2cData.LOWBYTE
  GOSUB  I2cOutByte      ' send the data - low byte
  GOSUB  I2cStop
  RETURN
I2CByteRead:
  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  GOSUB  I2cStart      ' repeated start
  I2cBuf = I2cAddr | 1
  GOSUB  I2cOutByte      ' send device address (with read set)
  I2cAck = 0        ' send Nak
  GOSUB  I2cInByte
  I2cData.LOWBYTE = I2cBuf  ' read the data
  I2cData.HIGHBYTE = 0
  GOSUB  I2cStop
  RETURN
I2CWordRead:
  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  GOSUB  I2cStart      ' repeated start
  I2cBuf = I2cAddr | 1
  I2cAck = 1        ' send Ack
  GOSUB  I2cOutByte      ' send device address (with read set)
  GOSUB  I2cInByte
  I2cData.HIGHBYTE = I2cBuf  ' read the data
  I2cAck = 0        ' send Nak
  GOSUB  I2cInByte
  I2cData.LOWBYTE = I2cBuf
  GOSUB  I2cStop
  RETURN
I2cOutByte:
  SHIFTOUT SDA, SCL, MSBFIRST, [I2cBuf]
  INPUT  SDA
  HIGH  SCL          ' clock in the ack' bit
  LOW  SCL
  RETURN
I2cInByte:
  SHIFTIN SDA, SCL, MSBPRE, [I2cBuf]
  SDAout = 0
  SDAdir = I2cAck
  HIGH  SCL          ' clock out the ack' bit
  LOW   SCL
  INPUT  SDA
  RETURN
I2cStart            ' I2C start bit sequence
  HIGH  SDA
  HIGH  SCL
  LOW  SDA
  LOW  SCL
  RETURN
I2cStop:            ' I2C stop bit sequence
  LOW  SDA
  HIGH  SCL
  HIGH  SDA
  RETURN