Link here

Using I2C Temperature Sensors
Interface your microcontroller to the Microchip Technology TC74 I2C single-chip temperature sensor.


This video shows how to use the PDQ Board to interface with an I2C (I Squared C) temperature sensor. The specific device here is the TC74, manufactured by Microchip Technology. The source code for this program is structured so that it can be modified to work with any I2C device.

If you have any questions please give us a call. We will be glad to discuss your application with you! See this link: Contact Us
 

Video


Click here to view the video in a new window.

 

TC74 Datasheet

This video demonstrates a very simple way to interface an I2C device using the PDQ Board. In this example, the I2C device is a TC74 temperature sensor. The TC74 has a resolution of 1 degree Celcius, with a range covering -40 to 125 degrees. The measured temperature is printed to the RS232 serial port four times per second. Built in error checking is performed for every transmitted byte, a feature of the I2C protocol. If an error occurs, a message is printed in place of the measured temperature.

 

Definitions

  1. I2C: This stands for "Inter-Integrated Circuit" and can also be abbreviated as IIC. This protocol uses two signal wires to communicate.
  2. Master: The I2C Master device controls the clock line and requests data from slave devices.
  3. Slave: There can be multiple I2C slave devices. Each has a unique address from a pool of 128 possible addresses.
  4. ACK: (Acknowledge) The slave pulls the data line low in response to a correct address. This action provides error checking to detect a disconnected or non responsive slave.
 

I2C Protocol

The I2C protocol defines communications between a slave and master device.

All messages begin with a start bit sent by the master. This is followed by a 7 bit slave address. Next the master sends a single bit to indicate the direction of the communication. A bit with the value 1 means the master is reading from the slave. A bit valued 0 means the master is writing.

If a slave with the correct address is present, it will respond with an ACK bit by pulling the data line low. Next the slave or master will transmit data depending on the value of the direction bit. Messages can be multiple bytes long, but they always terminate with a STOP bit.

The I2C protocol is described well by this Wikipedia article.

For a complete description of how to use I2C on the Mosaic PDQ board, see its documentation in the Users Manual: Inter-IC (IIC, I²C, I2C) Serial Bus

 

Software

The following software is involved in this demonstration:

  1. Mosaic IDE Plus - Compiles GNU C source code, resulting in a .DLF binary file, or "download file."
  2. Mosaic Terminal - Communicates with the PDQ Board over RS232 serial.
  3. QED-Forth Real Time Operating System (RTOS) - Embedded Operating system controlling the PDQ Board

The Mosiac IDE Plus compiles your C source code into a binary file. The Mosaic Terminal sends this file to the PDQ Board Users Guide. The QED-Forth RTOS on the PDQ Board Users Guide receives this file into ram, and then saves to flash memory after the download is complete. If you type "main" into the terminal followed by the 'enter' key, the RTOS will execute main() from the loaded source code. You also have the option to "Autostart" your program as seen in this video. Autostarting instructs the PDQ Board to execute main() at device power-on; When I flip on the power switch at time 1:30, the device instantly starts measuring and printing temperature readings.

 

Hardware

This project is constructed from a PDQ Board Starter Kit, four wires, and a TC74A0-5.0V.

 

Mosaic Hardware

  1. Docking Panel, sold together with the PDQ Board as a Starter Kit
 

Other

Schematic

The schematic for this project is extremely simple. Connect 4 wires from the Analog I/O Header H4 to the sensor. The only component used is the TC47, with no external resistors or capacitors needed.

H4: Analog Field Header
Pins Signal TC74 Pin
1— AGND —2
2— +5VAN —3
3— GND
4— V+RAW
5— SCL —4
6— SDA —5
7— PM7
8— PM6
9— AN15
10— AN14
11— AN13
12— AN12
13— AN11
14— AN10
15— AN9
16— AN8
17— AN7
18— AN6
19— AN5
20— AN4
21— AN3
22— AN2
23— AN1
24— AN0
 

Source Code

#include <mosaic\allqed.h>
 
// This sample program writes and reads data from an IIC device.
// IIC is pronounced "I squared C", and operates over two wires.
// The clock line is pin 5 on the Analog I/O Header, H4
// The data  line is pin 6 on the Analog I/O Header, H4
 
// This specific program is designed to read temperature from this chip:
//   TC74A0-5.0V
// This chip is Manufactured by Microchip Technology.
// Adopt this program to interface with your own IIC chip by adjusting the
// values below.  Most imporantly is the slave address.  The slave address
// and other paramaters can be found in the data sheet for your device.
 
 
// Maximum read/write size
#define MAX_BUFF_SIZE (8)
 
// The number of bytes we will read and write over IIC
// Check the datasheet for your IIC device
#define READ_SIZE  (1)
#define WRITE_SIZE (1)
 
// The address of the slave
// Check the datasheet for your IIC device
#define SLAVE_ADDRESS (0x90)
 
// This timeout determines how long the PDQ Board will wait for the IIC
// hardware to be ready.  This makes sure that the pending transmit operation
// will not begin before any previous operations have finished.
// The timeout is specified in timeslicer periods, and
// the default timeslicer period is 1.024 milliseconds
// With this value at 100, the timeout is 102.4 milliseconds
#define IIC_TIMEOUT (100)
 
// How many microseconds we wait before reading from the device
#define READ_DELAY (16000)
 
// Read and Write buffers
unsigned char readbuf[MAX_BUFF_SIZE];
unsigned char writebuf[MAX_BUFF_SIZE];
 
// The IIC functions IICSendNextFrame() and IICReceiveFrame() take
// the xaddress of the buffer they will access. These xaddr
// variables will point to the buffers above, and get passed
// into these functions.
static xaddr readbuf_xadd;
static xaddr writebuf_xadd;
 
 
 
 
// This function initializes buffers and the IIC hardware
void initialize_iic( void )
{
  int i;
 
  // First we start the timeslicer.  This system
  // allows for IIC messages to be sent correctly by Mosaic
  // functions.
  StartTimeslicer();
 
  // Next we fill both buffers with 0's
  for ( i=0; i<MAX_BUFF_SIZE; i++ )
  {
    readbuf[i] = 0;
    writebuf[i] = 0;
  }
 
  // Next we need to take the address of each buffer, and expand
  // them into an xaddress.
  // The TO_XADDR macro takes a 16 bit address and a 8 bit memory page
  // and returns a xaddress.
  readbuf_xadd = TO_XADDR( readbuf, 0 );
  writebuf_xadd = TO_XADDR( writebuf, 0 );
 
  // Finally we initialize the IIC hardware.  The '2' below is our
  // slave address.  Since we are operating in master mode, this value does
  // not matter.
  IICInit( IIC_10KHZ_13PERCENT, 2 );
  IICSend( 0, 0, 0 );
 
  // If you would like to see a table of avaliable IIC frequencies, type
  //   IIC.FREQUENCIES
  // into the terminal at the forth prompt.
  // If you wanted IIC to use the last frequency listed in the table,
  // Replace the line above with this:
  //   IICInit( 0xBF, 2 );
}
 
 
void check_error( int e )
{
    if( e & IIC_TIMEOUT_ERROR )
    {
        printf("IIC comunication timed out.  Is the device connected and address set correctly?\n");
        return;
    }
 
    if( e & IIC_ARB_LOST_ERROR )
    {
        printf("IIC bus conflict.  Are there two master devices?\n");
        return;
    }
 
    if( e & IIC_NAK_ERROR )
    {
        printf("IIC NAK error.  Slave device may not be responding correctly.\n");
        return;
    }
 
    if( e & IIC_XMIT_BUF_OVERFLOW )
    {
        printf("IIC transmit buffer overflow.\n");
        return;
    }
 
    if( e & IIC_RCV_BUF_OVERFLOW )
    {
        printf("IIC receive buffer overflow.\n");
        return;
    }
}
 
// This function displays data we recieved
// You should replace the body of this function with your own code.
// Depending on your application, you may want to print returned data
// or control output pins on the PDQ Board.
void display_data( int e1, int e2 )
{
  signed char temperature;
  int i;
 
  // if both errors are zero, we can print valid data
  if( e1 == 0 && e2 == 0 )
  {
    temperature = readbuf[0];
 
    printf( "Temperature: %d (celcius)\n", temperature );
  }
 
  // Loop 5 times and call a software delay of 50 ms
  // This results in a 250 ms or (1/4) second delay
  // This loop is needed because the maximum value that
  // MicrosecDelay() accept is 65535 or 65.5ms
  for( i = 0; i < 5; i++ )
  {
    MicrosecDelay( 50000 );
  }
}
 
int main()
{
  // Both Read and Write operations can result in error
  // These variables hold error values.  A non zero value indicates
  // that an error has occured.
  int error1, error2;
 
  // Initialize buffers and IIC Hardware
  initialize_iic();
 
 
  while( 1 )
  {
    // Prepare data to be sent
    // This device requires that 1 byte with a value of 0 be sent
    writebuf[0] = 0x00;
 
    // Write data to the IIC Slave
    error1 = IICSendNextFrame( IIC_TIMEOUT, writebuf_xadd, WRITE_SIZE, SLAVE_ADDRESS );
 
    // Delay before reading data
    MicrosecDelay( READ_DELAY );
 
    // Read data
    error2 = IICReceiveFrame( IIC_TIMEOUT, readbuf_xadd, READ_SIZE, SLAVE_ADDRESS );
 
    // Check for errors
    // It is important that no characters print between writing and reading from
    // this specific IIC device.  Other devices may be more lenient and allow for
    // a longer delay.
    check_error( error1 );
    check_error( error2 );
 
    // Display received data
    display_data( error1, error2 );
  }
    return 0;
}



See also →  How to Measure Analog Distance Sensor

 
This page is about: Using I2C Temperature Sensors, Interface Your Microcontroller to Microchip Technology TC74 I2C Single-chip Temperature Sensor – This video shows how to use PDQ Board to interface with an I2C (I Squared C) temperature sensor. The specific device here is TC74, manufactured by Microchip Technology. The source code for this program is structured so that it can be modified to work …
 
 
Navigation