TC77 Temperature Sensor Interface
The TC77 device operates as a temperature sensor via an SPI interface. Its circuit connections are very simple, and the device has a temperature range of -55°C to +125°C.
The Positron8 BASIC Compiler has built in SPI commands, but the program below shows how to construct a software SPI interface using BASIC commands. It is very fast and very efficient.
The TC77's datasheet can be downloaded from here: TC77.pdf
The circuit for the TC77 is shown below:
The interfacing code is listed below:
' Interface to a TC77 temperature sensor
' Transmits the temperature (in degrees Centigrade) on a serial terminal
'
' For the Amicus18 board, which uses a PIC18F25K20 device operating at 64MHz
' Written by Les Johnson for the Positron8 BASIC Compiler
'
Include "Amicus18.inc" ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board
Declare Float_Display_Type = Fast ' Set for faster and more accurate floating point displaying
'
' Create some variables
'
Dim TC_fTemperature As Float ' Holds the temperature
Dim TC_bBitCounter As Byte ' Used to count the bits in the bit-bashed SPI interface
Dim TC_wTemperature As SWord ' Used to hold the raw data from the device
'
' Define some pins for the SPI interface to the TC77 device
'
$define TC_SCK_Pin PORTB.0 ' Connects to the TC77's SCK pin
$define TC_CS_Pin PORTB.1 ' Connects to the TC77's CS pin
$define TC_SIO_Pin PORTB.2 ' Connects to the TC77's SIO pin
'-------------------------------------------------------------------------------
GoTo Main ' Jump to the main program loop
'-------------------------------------------------------------------------------
' Setup the TC77 device's SPI interface
' Input : None
' Output : None
' Notes : None
'
$define TC77_Init() '
High TC_CS_Pin '
Low TC_SCK_Pin '
Input TC_SIO_Pin
'----------------------------------------------------------------------
' Read the data from the TC77 device's SPI interface
' Input : None
' Output : TC_wTemperature holds the 16-bit temperature value
' Notes : None
'
Proc TC77_SPI_Read()
PinClear TC_CS_Pin ' Enable the SPI interface
PinClear TC_SCK_Pin ' Pull TC_SCK_Pin low
TC_bBitCounter = 15 ' Set bit counter for 16-bits
Repeat ' Create a loop for the bits
PinSet TC_SCK_Pin ' TC_SCK_Pin rising edge
TC_wTemperature.0 = TC_SIO_Pin ' Receive the bit from the device
PinClear TC_SCK_Pin ' Pull the TC_SCK_Pin low
TC_wTemperature = TC_wTemperature << 1 ' Rotate the bit
Dec TC_bBitCounter
Until TC_bBitCounter = 0
PinSet TC_CS_Pin ' Disable the SPI interface
EndProc
'--------------------------------------------------------------------------------
' Interface to a TC77 temperature sensor
' Input : None
' Output : Returns the temperature (in degrees Centigrade)
' Notes : The TC77 Temperature Register is a 16-bit read only register.
' The temperature data format is a 13-bit two's complement digital
' word (bits 15:3). The least significant bit (LSb) is equal to
' 0.0625 degrees C. Bit 2 is set to a logic '1' after the completion
' of the first temperature conversion following a power-up or reset
' event. Bits 1:0 are tri-stated.
'
Proc TC77_GetTemperature(), Float
TC77_SPI_Read() ' Read the data from the device into TC_wTemperature
TC_wTemperature = TC_wTemperature >> 6 ' Right adjust 13-bit 2's complement in TC_wTemperature
If TC_wTemperature.9 = 1 Then ' Is it a negative temperature?
TC_wTemperature.Byte1 = TC_wTemperature.Byte1 | $FE ' Yes. So sign extend bits 15 to 10 in TC_wTemperature
TC_wTemperature = ~TC_wTemperature ' \ 2's complement
Inc TC_wTemperature ' /
TC_wTemperature = -TC_wTemperature ' Make negative
EndIf
Result = TC_wTemperature / 2 ' Divide by 2 for the temperature
EndProc
'--------------------------------------------------------------------------------
' The main program loop starts here
' Read the temperature and transmit the value as ASCII to a serial terminal
'
Main:
TC77_Init() ' Initialise the TC77 interface
Do
TC_fTemperature = TC77_GetTemperature() ' Get the temperature
HRSOutLn Dec1 TC_fTemperature ' Transmit the temperature serially
DelayMS 500
Loop
Below is the program running in the Proteus simulator:
The above simulation files can be downloaded from here: