Interfacing The TC74 I2C Digital Thermal Sensor with PIC Microcontroller – XC8
Français
Figure 1: Digital Thermometer using TC74 I2C Thermal Sensor Circuit Diagram
TC74 is a serially accessible digital temperature sensor particularly suited for low cost and small form-factor applications. Temperature data is converted from the on-board thermal sensing element and made available as an 8-bit digital word.
Communication with the TC74 is accomplished via a simple 2-wire I2C compatible serial port. This bus also can be used to implement multi-drop/multi-zone monitoring. The SHDN bit in the CONFIG register can be used to activate the low-power Standby mode. Temperature resolution is 1°C. Conversion rate is a nominal 8 samples/sec. Power consumption in only 200 µA (5 µA Standby).
Small size, low installed cost and ease of use make the TC74 an ideal choice for implementing thermal management in a variety of systems
Pin Descriptions
Figure 2: TC74 pin descriptions
Below is the description of each pin:
Table 1: TC74 pin description
The TC74 stores the measured temperature into its 8-bit temperature register as 2’s complement binary format. The most significant bit is the sign bit, which is set to 1 for negative temperatures. Therefore, the maximum measurable positive temperature is + 127 °C (0111 1111).
Each unit value represents one degree (Celsius). For example the reading of 0000 0000b corresponds to 0°C and 0001 1001b corresponds to +25°C. Examples of this temperature to binary value relationship are shown in Table 2 below.
Table 2: TC74 Temperature to binary value relationship
The TC74 has also got another 8-bit Read/Write Configuration Register (RWCR) that is used to put the device into a low power (IDD = 5 µA, typical) Standby mode. In this mode, the A/D converter is halted and the temperature data registers are frozen. Bit 7 of RWCR must be set to put TC74 into Standby mode
Table 3: TC74 Configuration Register
For more information, please read the TC74 Datasheet.
Watch the Video Tutorial
Serial Port Operation
The Serial Clock input (SCLK) and bidirectional data port (SDA) form a 2-wire bidirectional serial port for programming and interrogating the TC74.
The host microcontroller communicate with the slave TC74 through the I2C bus. The host microcontroller provides the clock signal for all the data transfers and the TC74 always operates as a Slave. The default 7-bit I2C address of TC74 is 1001 101b (ox4D). However, 7 other address options are also available which can be identified from the part number of the device
The I2C protocol is illustrated in Figure 3 below. All data transfers have two phases and all bytes are transferred MSB first. Accesses are initiated by a START condition, followed by a device address byte and one or more data bytes.
The device address byte includes a Read/Write selection bit. Each access must be terminated by a STOP
condition. A convention called “Acknowledge” (ACK) confirms receipt of each byte. Note that SDA can change only during periods when SCLK is low (SDA changes while SCLK is high are reserved for START and STOP conditions)
Figure 3: TC74 Serial Port Operation
To learn more about the I2C bus communication, please read the article:
Steps to Read Temperature:
- The host microcontroller issues a Start condition followed by the address byte of the sensor. The address byte consists of the 7-bit slave address and a Read/Write bit (R/W). The R/W bit is always ‘0’ (Write) in the first phase.
- TC74 responds with an ACK if the received 7-bit address matches with its own slave address.
- Then the host microcontroller sends the command byte to TC74 to indicate which register it wants to access. For reading the temperature, the command byte should be 00h. The TC74 responds with an acknowledge pulse.
- The host microcontroller issues a new Start condition because the direction of data transfer is now going to be changed. The new address byte with R/W bit 1 is sent by the host, which is acknowledged by the slave.
- The TC74 transmits the 8-bit temperature data from the temperature register. Upon receiving the byte, the host doesn’t acknowledge, but generates a Stop condition
Buy the Components for this project from our Online Store
[porto_products view=”grid” ids=”4684,4682, 4924,4520,4541″]
The circuit diagram of the project is on figure 1 above, the PIC18F45K22 is used but any other PIC with I2C interface could be used. Don’t forget to put two pull-up resistors on SDA and SCL lines of I2C bus.
To simplify everything, we are going to use MPLAB Code Configurator (MCC).
- In MCC System module, we’re gonna set our Oscillator select to 8MHz Internal Oscillator. we’re gonna disable the MCLR pin as well.
- In pin module, set RB0 to RB6 as digital Output pins for our LCD connections.
To learn more how to use the LCD, please read the article:
What’s important to us in this tutorial is the I2C set with MCC. As shown on figure 4 below:
- Select the MSSP1 under Device Resources.
- Select I2C Master
- Slew rate control, select standard speed,
- Set the I2C clock frequency to 100KHz or below by changing the Baud rate generator value which is what is normally referred to as Standard Speed. This value will depend on the system clock frequency.
- Set the Slave address to 7-bit
Figure 4: TC74 I2C Sensor Configuration with MCC
By clicking on the MCC Generate button, a number of header and source files will be auto generated for us including the I2c1.h and I2C1.c
You can read the I2C1.h for a short descriptions of the functions prototypes and macros generated by MCC. You could also right click on the peripherals in project resources in MCC and select help for more information on how to use them with an option to a wiki page.
Below is a quick summary of some few functions that we are going to use in this tutorials, for more information, please read the description of each function in the i2c.h file generated by the MCC or read our I2C Communication tutorial.
- I2C1_MESSAGE_STATUS: Defines the different message status when processing TRBs (Transanction Request Block)
- I2C_Initialize(): This routine initializes the i2c1 driver instance for index, making it ready for clients to open and use it
- I2C_MasterWrite(): This function handles one i2c master write transaction with the supplied parameters. It will send data to i2c queue, waits for the transaction to complete and returns the result. The parameters are:
- address: The address of the i2c slave peripheral to be accessed
- length: The length of the data block to be sent
- *pdata: A pointer to the block of data to be sent
- *pstatus: A pointer to the status variable
- I2C_MasterRead(): This function handles one i2c master read transaction with the supplied parameters. It will send data to i2c queue, waits for the transaction to complete and returns the result. The parameters are:
- address: The address of the i2c slave peripheral to be accessed
- length: The length of the data block to be sent
- *pdata: A pointer to the block of data to be sent
- *pstatus: A pointer to the status variable
- I2C1_MasterTRBInsert(): Inserts a list of i2c transaction requests into the i2c transaction queue. The i2c processes lists of transaction requests. Each transaction list is handled as a string of i2c restarts. When the list of transactions is complete, an i2c stop is produced, the flag is set with the correct condition code and the next list is processed from the queue.
- I2C_MasterQueueIsEmpty(): This function returns the empty status of the Master queue. You can use this function to check if the queue is empty. This can verify if the Master is currently idle. This function will return True if the queue is empty and false if the queue is not empty.
The MCC I2C Master drivers are based on an Interrupt Driven State Machine designed to perform small I2C transactions automatically. These can be queued to obtain custom sequences as required by each specific application like for example a MasterWrite followed by a MasterRead.
During the call of a Write, Read or TRBInsert, we will pass the I2C driver a pointer to a status variable I2C1_MESSAGE_STATUS that will be able to interrogate periodically to check for the operation completion otherwise it will return fail, pending, lost state etc.
We must Enable Peripheral and Global Interrupts (uncomment the lines) immediately after the SYSTEM_Initialize() call in the main function, failure to do so, the I2C drivers state machine won’t be able to advance.
In this simple example below, the microcontroller reads the temperature word from the TC74’s Temperature Register and displays it on a 16×2 LCD. The Celsius temperature value is displayed on the 1st line of the LCD and the converted to Fahrenheit Temperature value on the 2nd line of the LCD. The default TC74 address of 0x4D is used.
You can easily create a PCB for this project as we have learned in our PCB start to finish tutorials, all you need is to get a good PCB prototyping company that will offer your good quality PCBs at a good price. We recommend PCBWay.com, a China Shenzhen-based manufacturer specializing in PCB prototyping, small-volume production and PCB Assembly service all under one.
They are now running a promotion for their PCB Assembly services, get a worldwide free shipping when you place an order for PCB Assembly.
To learn more, please click on the image below and to get an instant PCB Assembly quote online, please visit their home page: PCBWay.com
Main Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
/** Generated Main Source File www.studentcompanion.coza File Name: main.c Summary: This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs Description: This header file provides implementations for driver APIs for all modules selected in the GUI. Generation Information : Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.77 Device : PIC18F45K22 Driver Version : 2.00 MPLAB X IDE: v5.25 XC8: v2.05 MCC: v3.85.1 */ #include "mcc_generated_files/mcc.h" #include "lcd.h" /** Section: Global Variables */ uint8_t readValue; uint8_t TC74_cmd = 0; // read temperature command I2C1_MESSAGE_STATUS I2C_status = I2C1_MESSAGE_COMPLETE; uint8_t count = 0; uint16_t I2C_Address = 0x4D; // slave device address A5 void StateTemperature(void); /* Main application */ void main(void) { // Initialize the device SYSTEM_Initialize(); //Initialize the LCD LCD_Initialize(); // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts // Use the following macros to: // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); // Disable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptDisable(); LCDGoto(0,0); LCDPutStr("Temp with TC74"); LCDGoto(0,1); LCDPutStr("StudentCompanion"); __delay_ms(2000); //2s delay // Clear LCD for next display DisplayClr(); while (1) { //Function for the Temperature state StateTemperature(); } } /* This routine is called to read the temperature state) */ void StateTemperature(void) { uint8_t tempValueC =0; //Celsius Temperature Variable volatile uint8_t negValueC; uint8_t tempValueF =0; //Fahrenheit Temperature Variable volatile uint8_t negValueF; // i2c master write transaction with the supplied parameters I2C1_MasterWrite( &TC74_cmd, 1, I2C_Address, &I2C_status); LCDPutStr(I2C_status); while (I2C1_MESSAGE_PENDING == I2C_status); //i2c master read transaction with the supplied parameters I2C1_MasterRead( &readValue, 1, I2C_Address, &I2C_status); while (I2C1_MESSAGE_PENDING == I2C_status ); // Get current temperature reading tempValueC = readValue; // Check to see if Celsius Temperature is Negative if (tempValueC >= 128) { tempValueC = 256 - tempValueC; negValueC = 1; } else { tempValueC = tempValueC; negValueC = 0; } // Convert the Celsius Temperature as a Fahrenheit value if (negValueC == 0) { tempValueF = (((tempValueC*9)/5)+32); } else if (negValueC == 1) { // Values larger then 18; Fahrenheit goes negative. if (tempValueC < 18) { tempValueF = (((tempValueC)*9)/5); tempValueF = 32-tempValueF; negValueF = 0; } else { tempValueF = ((((tempValueC)*9)/5)-32); negValueF = 1; } } // Display the Celsius Temperature 1st line of LCD LCDGoto(0,0); LCDPutStr("Temp: "); if (negValueC == 1) { LCDPutChar('-'); } else { if (!(tempValueC/100)) { LCDPutChar(' '); } else { LCDPutChar(tempValueC/100+'0'); } } LCDPutChar(tempValueC/10+'0'); LCDPutChar(tempValueC % 10+'0'); LCDPutChar(223); //Display degree symbol LCDPutStr("[C] "); //Display C for Celsius // Display the Fahrenheit Temperature on 2nd line of LCD LCDGoto(0,1); LCDPutStr("Temp: "); if (negValueF == 1) { LCDPutChar('-'); } else { if (!(tempValueF/100)) { LCDPutChar(' '); } else { LCDPutChar(tempValueF/100+'0'); } } LCDPutChar(tempValueF/10+'0'); LCDPutChar(tempValueF % 10+'0'); LCDPutChar(223); //Display degree symbol LCDPutStr("[F] "); //Display C for Fahrenheit } /** End of File */ |
You can download the full project files (MPLAB XC8 source code and Proteus Schematic design) below here.
Note: All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).
Download: MPLAB X Project TC74
Download: Proteus Schematic Project TC74