Proton BASIC Compiler - MC77 Temperature Sensor Interface

  • PicŪ Basic

  • 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 TC77's datasheet can be downloaded from here: TC77.pdf

    The circuit for the TC77 is shown below:

    Click here for a more detailed image

    The interfacing code is listed below:
    ' Interface to a TC77 temperature sensor
    ' Transmits the temperature (in degrees Centigrade) on the serial terminal
    ' For the Amicus18 board, which uses a PIC18F25K20 device operating at 64MHz
    ' Written by Les Johnson 06-01-2016
        Include ""                                      ' 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 over the subroutines
    ' 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
    $define TC77_SPI_Read() GoSub _TC77_SPI_Read
        Clear TC_CS_Pin                                             ' Enable the SPI interface
        Clear TC_SCK_Pin                                            ' Pull TC_SCK_Pin low   
        TC_bBitCounter = 15                                         ' Set bit counter for 16-bits
        Repeat                                                      ' Create a loop for the bits
            Set TC_SCK_Pin                                          ' TC_SCK_Pin rising edge     
            TC_wTemperature.0 = TC_SIO_Pin                          ' Receive the bit from the device
            Clear TC_SCK_Pin                                        ' Pull the TC_SCK_Pin low
            TC_wTemperature = TC_wTemperature << 1                  ' Rotate the bit
            Dec TC_bBitCounter
        Until TC_bBitCounter = 0    
        Set TC_CS_Pin                                               ' Disable the SPI interface
    ' Interface to a TC77 temperature sensor
    ' Input     : None
    ' Output    : TC_fTemperature holds 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.
    $define TC77_GetTemperature(pResult) '
        GoSub _TC77_ReadTemp             '
        pResult = TC_fTemperature
        TC77_SPI_Read()                                             ' Read the data from the device into TC_wTemperature
        TC_wTemperature = TC_wTemperature >> 6                      ' Right adjust 13-bit 2's complement temperature value into 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
        TC_fTemperature = TC_wTemperature / 2                       ' Divide by 2 for the temperature    
    ' The main program loop starts here
        TC77_Init()                                                 ' Initialise the TC77 interface 
            TC77_GetTemperature(TC_fTemperature)                    ' Get the temperature
            HRSOut Dec1 TC_fTemperature, 13                         ' Transmit the temperature serially
            DelayMS 500
    Below is the program running in the Proteus simulator:

    The above simulation files can be downloaded from here: