SPI Protocol based TC72 Sensor with Arduino Uno is not working.

Hi,
I’m using TC72 sensor http://ww1.microchip.com/downloads/en/DeviceDoc/21743a.pdf

to measure the temp. data using Uno.

I have spend more then required time but this simple communication not working at all.
Please help me out here.

I have already check with this one http://forum.arduino.cc/index.php?topic=365351.0 but not getting success

here is the code for the same.


#include <SPI.h>

//Assign the Chip Select signal to pin 10.
const int CS=10;

//TC72 Registers
const char CNTL_R = 0x00;
const char CNTL_W = 0x80;
const char ID = 0x03;
const char TEMP_LSB = 0x01;
const char TEMP_MSB = 0x02;

char values[10];

byte SensorID,tempLSB,tempMSB,tempMSB1,tempMSB2,tempMSB3,tempMSB4;

unsigned int livetemp;

void setup()
{
//SPI Port Setup
SPI.begin();
SPI.setDataMode(SPI_MODE1);
pinMode(CS, OUTPUT);
digitalWrite(CS, LOW);

//Console Setup
Serial.begin(9600);

//Turn on the sensor for continous temp conversion mode
writeRegister(CNTL_W, 0x04);
}

void loop()
{
readRegister(ID, 1, values);
if(values[0] == 0x54)
{
Serial.print(“TC-72 sensor detected with ID - “);
Serial.print(values[0],HEX);
Serial.print(”\n”);
}
else
{
Serial.print(“TC-72 is not found, the ID is - “);
Serial.print(values[0],HEX);
Serial.print(”\n”);
}

//readRegister(TEMP_LSB, 1, values);
//tempLSB = values[0];

readRegister(TEMP_MSB, 5, values);
tempMSB = values[0];
tempMSB1 = values[1];
tempMSB2 = values[2];
tempMSB3 = values[3];
tempMSB4 = values[4];

Serial.print("Live temp is - ");
Serial.print(tempMSB, DEC);
Serial.print(tempMSB1, DEC);
Serial.print(tempMSB2, DEC);
Serial.print(tempMSB3, DEC);
Serial.print(tempMSB4, DEC);

Serial.print("\n");

delay(500);
}

void writeRegister(char registerAddress, char value)
{
digitalWrite(CS, LOW);
digitalWrite(CS, HIGH);

SPI.transfer(registerAddress);

SPI.transfer(value);

digitalWrite(CS, LOW);
}

void readRegister(char registerAddress, int numBytes, char * values)
{
digitalWrite(CS, LOW);
digitalWrite(CS, HIGH);

SPI.transfer(registerAddress);

for(int i=0; i<numBytes; i++)
{
values = SPI.transfer(0x00);

  • }*

  • digitalWrite(CS, LOW);*
    }
    -------------------------------------------------------------------------------------------------------------

Hi

  readRegister(TEMP_MSB, 5, values);
  tempMSB = values[0];
  tempMSB1 = values[1];
  tempMSB2 = values[2];
  tempMSB3 = values[3];
  tempMSB4 = values[4];

Looks like you are expectiing to read a string of characters from the TC72, showing the temperature?

That’s not how the TC72 works. Look at section 3.1 of the datasheet. It explains, only briefly :(, that the temperature is held as a 10-bit 2s complement binary number across the LSB and MSB bytes. So you need to read these two bytes and manipulate them to get the temperature.

Regards

Ray

Hi Ray,
I'm not expecting string.
the

readRegister(TEMP_MSB, 5, values);
tempMSB = values[0];
tempMSB1 = values[1];
tempMSB2 = values[2];
tempMSB3 = values[3];
tempMSB4 = values[4];

is just make to ensure that my TempMSB register values are not skip within this five read operation.

The syntax like,

readRegister(TEMP_MSB, 5, values);
tempMSB = values[0];

should work but give me a 0x00 value.

Right now I'm just expecting MSB value which is without floating point as per the data sheet.

You know what I always get 0x00 either sensor ID get or TempMSB get. the program always return 0x00 on the MISO line.

by the way thanks for your replay.
what is the reason behind the 0x00 values for all read operatio. BTW Loopback is working expected.

Try this inside the for loop:

values[i] = SPI.transfer(0x00);

Also, if you are expecting to get 8-bit unsigned data values back from the TC72, you should declare the relevant variables as byte or uint8_t rather than as char (which is a signed data type intended for text characters).

I have perform 255 iteration to get the MISO line data but just getting the 0 always.

Here, I am sharing my code which works perfectly…

#include <SPI.h>

//Assign the Chip Select signal to pin 09.
const int CS=9;

//TC72 Registers
const char MODE = 2; //1 = conti && 2 = one shot
const char CONT = 0x04;
const char SET = 0x14;
const char ONE_SHOT = 0x05;
const char CNTL_R = 0x00;
const char CNTL_W = 0x80;
const char ID = 0x03;
const char TEMP_LSB = 0x01;
const char TEMP_MSB = 0x02;

void writeRegister(char registerAddress, char value);
char readRegister(char registerAddress);

//Conversion mode
void mode_selection(int mode) //mode 1 == conti && mode2 == one_shot
{
if(mode == 1)
{
writeRegister(CNTL_W, CONT); // continues mode set
}
else if(mode == 2)
{
writeRegister(CNTL_W, ONE_SHOT); //shut-down mode set
}
}

void setup()
{
pinMode(CS, OUTPUT);
digitalWrite(CS, LOW);
delay(500);
digitalWrite(CS, HIGH);
//Console Setup
Serial.begin(9600);
//SPI Port Setup
SPI.begin();
mode_selection(MODE); //1 = conti && 2 = one_shot
}

void loop()
{
float new_data;
char msbvalue = readRegister(TEMP_MSB,MODE);
char lsbvalue = readRegister(TEMP_LSB,MODE);
if(lsbvalue && 0b10000000 == 0b10000000)
{
new_data = msbvalue + 0.50;
}
else if (lsbvalue && 0b01000000 == 0b01000000)
{
new_data = msbvalue + 0.25;
}
else
{
new_data = msbvalue;
}

Serial.print(“Live temp is = “);
Serial.print(new_data);
Serial.print(”\n”);
delay(200);
}

void writeRegister(char registerAddress, char value)
{
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1));
digitalWrite(CS, LOW);
digitalWrite(CS, HIGH);
SPI.transfer(registerAddress);
SPI.transfer(value);
digitalWrite(CS, LOW);
SPI.endTransaction();
}

char readRegister(char registerAddress,char mode)
{
if(mode == 2)writeRegister(CNTL_W, SET); //for one_Shot mode…
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1));
digitalWrite(CS, LOW);
digitalWrite(CS, HIGH);
SPI.transfer(registerAddress);
char values = SPI.transfer(0x00);
digitalWrite(CS, LOW);
SPI.endTransaction();
return values;
}