TimC
Joined: 12 Nov 2005 Posts: 7
|
TC74 Temperature Sensor Driver |
Posted: Wed Oct 09, 2024 8:34 pm |
|
|
Hi All,
Here is a driver for the Microchip TC74 Temperature Sensor.
I am posting this because it adds a power saving standby mode.
Also results in C or F, below freezing, and a Status request.
If you are having problems getting this driver to work:
First run a I2C scanner to see if the 0x9A address comes back.
Check the I2C speed, it should not be faster than 100K.
Don't mix a 5V TC74 with a 3.3V uP, SMBUS is more sensitive to voltage differences.
Sample Code Snip-it:
Code: |
#use I2C(master, I2C1, SMBUS)
//#define TC74_ADDR 0x9A // Default I2C slave address for TC74 (u5 or v5 markings)
#include <TC74_driver.c>
signed int dataC, dataF;
.
.
dataC = TC74_Temp();
dataF = TC74_Temp(1);
printf ("Temp: %d f %d c\r\n", dataF,dataC);
|
Here is the TC74 Driver:
Code: |
#ifndef _TC74_DRIVER_C_
#define _TC74_DRIVER_C_
// The TC74 is a SMBUS / I2C 8bit digital temperature sensor.
// Accuracy at best is + or - 2 deg C
// Conversion rate is a nominal 8 samples/sec
// 200uA operating. 5uA Standby.
// You need pull ups on SDA and SCL, 4.7K to 10K
// Written by TimC Oct 9 2024 version 1.0
// Make sure you have a line similar to this in your code
//#use I2C(master, I2C1, SMBUS) // SMBUS has timeouts but works without
// SOT-23 Package Marking Codes show address u0-u7 or v0-v7
// TO-220 Package Marking Information show TC74A0-TC74A7
// Default I2C slave address for TC74 is 0x9A. Marking Code u5,v5 or A5
#define TC74_ADDR 0x9A
#define TC74_STANDBY 0x80
#define TC74_OPERATE 0x00
#define TC74_CONFIG_REG 0x01
#define TC74_TEMP_REG 0x00
void TC74_Config(unsigned int control) {
// Control byte should equal 0 for normal, or 0x80 for lower current standby
// In standby the temperature will not change or will be 0 C after restart
if(control != TC74_STANDBY)
control = TC74_OPERATE; // only other valid value
i2c_start();
i2c_write(TC74_ADDR);
i2c_write(TC74_CONFIG_REG); // Config register = 1
i2c_write(control);// 0=Power on state, 0x80=standby Temp with return 0 deg C
i2c_stop();
}
int TC74_Status() {
// Returns the Control Byte - Control bit 7 should equal 0 for normal, or 1 for lower current standby
// if Bit 6 = 1 then data is ready if Bit 6 = 0 then data is NOT ready
int data;
i2c_start();
i2c_write(TC74_ADDR);
i2c_write(TC74_CONFIG_REG); // Config register = 1
i2c_start(); // Restart to change Direction
i2c_write(TC74_ADDR+1); // Ask to read the Status
data = i2c_read(0); // after reading we NACK
i2c_stop();
return data;
}
signed int TC74_Temp(boolean format=0) {
// Gets the one byte signed value in C or format 1 for deg F
// works below freezing with neg values.
signed int dataC, dataF;
i2c_start();
i2c_write(TC74_ADDR);
i2c_write(TC74_TEMP_REG); // Temperature register = 0x00
i2c_start(); // Restart to change Direction
i2c_write(TC74_ADDR+1); // Ask to read the Temperature
dataC = i2c_read(0); // after reading we NACK
i2c_stop();
if(format == 0) // If all we want is C then we are done
return dataC;
// Convert to Deg F
dataF = (9 * (signed int16)dataC) / 5;
dataF += 32;
return dataF;
}
#endif //_TC74_DRIVER_C_
|
|
|