CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

TC74 Temperature Sensor Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
TimC



Joined: 12 Nov 2005
Posts: 7

View user's profile Send private message

TC74 Temperature Sensor Driver
PostPosted: Wed Oct 09, 2024 8:34 pm     Reply with quote

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_

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group