Quantcast
Channel: PIC – xAppSoftware Blog
Viewing all articles
Browse latest Browse all 7

Working with the TCN75 temperature sensor

$
0
0

How to use the TCN75 temperature sensor

In this article I'm going to show to you how to use the TCN75 we have mounted on our boards.

The TCN75 is a very simple temperature sensor but at the same time it is very accurate, with a precision of ±0,5 ° C, fra 25°C≤ TA ≤ 100°C e ±3 °C -55°C≤ TA ≤ +125°C.

The TCN75 needs only 0,25mA when it is in operative mode and 1uA when acting in stand-by mode.

The TCN75 communicates over an I2C™ bus and provides three programmable address pins, they can be programmed using hardware jumpers allowing up to 7 devices on the same bus.


It has a programmable pin used to signal if the temperature overcomes a defined value(user programmed set point). The TCN75 comes with a default value for the set point at 80 °C with 5 °C for the hysteresis.

The most important characteristics for this sensor are the ones shown in the following table:

 

Solid state sensor for temperature measurement
accuracy 0.5 °C [25°, 100°]
Working range [-55, 125] °C  
Interface I2C  
Power supply [2.7, 5.5] V  
Packages SOIC or MSOP (8pin)  

 

The TCN75 has two different temperature probes, the first one is calibrated for a supply voltage of 3,3 V and the second one calibrated for a supply voltage of 5V. Both the probes can be used with different values of the supply voltage, but in this case the accuracy decreases up to +-3 °C following this rule: 1°C/V.

To work properly with the TCN75 we have to access 5 registers. (See the TCN75 datasheets for more information).

1. POINTER REGISTER

It is a 8-bit Write Only register and it is used to address one of the 4 following registers (D0 and D1, D2 to D7 must be set to zero)

 

 

D[7] D[6] D[5] D[4] D[3] D[2] D[1] D[0]
Must be set to 0 Pointer

 

Register Selection via the Pointer Register
D[1] D[0] Register Selection
0 0 TEMP
0 1 CONFIG
1 0 T_HYST
1 1 T_SET

 

2. CONFIGURATION REGISTER

It is a 8-bit Read/Write Register, it is used to configure next operation:

 

D[7] D[6] D[5] D[4] D[3] D[2] D[1] D[0]
Must be set to 0 Fault Queue INT/CMPTR, Polarity COMP/INT Shutdown

3. TEMPERATURE REGISTER

It is a 16-bit Read Only register. It contains the temperature after a conversion cycle.

4. SETPOINT REGISTER

It is a 16-bit Read/Write register, it contains the setpoint temperature.

5. HYSTERESIS REGISTER

It is a 16-bit Read/Write register, it contains the threshold value for the hysteresis.

 

Speaking to the device

To speak to the device we will use the functions defined for the Pic 12f1840 in this post

 

 

void i2c_start(void) sends the start bit
void i2c_restart(void) sends the restart bit
void i2c_stop(void) sends the stop bit
void i2c_ack(void) sends ACK
void i2c_nack(void) sends NACK
void i2c_wr(unsigned char i2c_data)
unsigned char  i2c_rd(void)

Reading the Configuration Register

<span style="color: #CC6600;">void</span> tcn75_rd_8(<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">char</span> ucTcn75Reg)  <span style="color: #7E7E7E;">// reads an 8 bit register of the TCN75</span>
{
&nbsp;&nbsp;gucI2CError&nbsp;=&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// zero the I2C error</span>
&nbsp;&nbsp;i2c_start()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// start I2C communication</span>
&nbsp;&nbsp;i2c_wr(TCN75_I2C_ADDR_WR)&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// write DEVICE ADDR for TCN75 WRITES</span>
&nbsp;&nbsp;i2c_wr(ucTcn75Reg)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// write the register ADDRESS</span>
&nbsp;&nbsp;i2c_restart()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// RESTART for READS</span>
&nbsp;&nbsp;i2c_wr(TCN75_I2C_ADDR_RD)&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// send the DEVICE ADDRESS for TCN75 READS.  </span>
&nbsp;&nbsp;gLSBI2C&nbsp;=&nbsp;i2c_rd()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// read register</span>
&nbsp;&nbsp;i2c_nack()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// NOACK from MASTER (last read byte)</span>
&nbsp;&nbsp;i2c_stop()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// stop I2C communication</span>
}&nbsp;

Writing the Configuration Register

<span style="color: #7E7E7E;">/**&nbsp;Configuration&nbsp;register&nbsp;write&nbsp;8&nbsp;bit</span>
<span style="color: #7E7E7E;">*/</span>
<span style="color: #CC6600;">void</span> tcn75_wr_8(<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">char</span> ucTcn75Reg)  <span style="color: #7E7E7E;">// writes a 8 bit value in a TCN75 register</span>
{
&nbsp;&nbsp;gucI2CError&nbsp;=&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// zero the I2C error</span>
&nbsp;&nbsp;i2c_start()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// start I2C communication     </span>
&nbsp;&nbsp;i2c_wr(TCN75_I2C_ADDR_WR)&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// write DEVICE ADDR for TCN75 WRITES</span>
&nbsp;&nbsp;i2c_wr(ucTcn75Reg)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// write the register&#39;s ADDRESS</span>
&nbsp;&nbsp;i2c_wr(gLSBI2C)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// write byte variable in the register</span>
&nbsp;&nbsp;i2c_stop()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// stop I2C communication</span>
}&nbsp;

Writing 16-bit registers

<span style="color: #CC6600;">void</span> tcn75_wr_16(<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">char</span> ucTcn75Reg) 
{
&nbsp;&nbsp;gucI2CError&nbsp;=&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// zero the I2C error</span>
&nbsp;&nbsp;i2c_start()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// start I2C communication     </span>
&nbsp;&nbsp;i2c_wr(TCN75_I2C_ADDR_WR)&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// write DEVICE ADDR for RTCC WRITES</span>
&nbsp;&nbsp;i2c_wr(ucTcn75Reg)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// write the register&#39;s ADDRESS</span>
&nbsp;&nbsp;i2c_wr(gMSBI2C)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;<span style="color: #7E7E7E;">// write byte variable in the register MOST SIGNIFICANT BIT</span>
&nbsp;&nbsp;i2c_wr(gLSBI2C)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;<span style="color: #7E7E7E;">// write byte variable in the register LESS SIGNIFICANT BIT</span>
&nbsp;&nbsp;i2c_stop()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;<span style="color: #7E7E7E;">// stop I2C communication</span>
}&nbsp;

Reading 16-bit registers

<span style="color: #CC6600;">void</span> tcn75_rd_16(<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">char</span> ucTcn75Reg)
{
&nbsp;&nbsp;gucI2CError&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// zero the I2C error</span>
&nbsp;&nbsp;i2c_start();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// start I2C communication</span>
&nbsp;&nbsp;i2c_wr(TCN75_I2C_ADDR_WR);&nbsp;&nbsp;<span style="color: #7E7E7E;">// write DEVICE ADDR for TCN75 WRITES</span>
&nbsp;&nbsp;i2c_wr(ucTcn75Reg);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// write the register ADDRESS POINTER BYTE</span>
&nbsp;&nbsp;i2c_restart();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// RESTART for READS</span>
&nbsp;&nbsp;i2c_wr(TCN75_I2C_ADDR_RD);&nbsp;&nbsp;<span style="color: #7E7E7E;">// send the DEVICE ADDRESS for TCN75 READS.</span>
&nbsp;&nbsp;gMSBI2C&nbsp;=&nbsp;i2c_rd();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// read the MSB part of the register</span>
&nbsp;&nbsp;i2c_ack();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Master ack</span>
&nbsp;&nbsp;gLSBI2C&nbsp;=&nbsp;i2c_rd();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// read the LSB part of register</span>
&nbsp;&nbsp;i2c_nack();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// NOACK from MASTER (last read byte)</span>
&nbsp;&nbsp;i2c_stop();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// stop I2C communication</span>
}

Enjoy,

Gg1.


Viewing all articles
Browse latest Browse all 7

Trending Articles