Pages: [1]   Go Down
Author Topic: TSYS01 - function to read temperature......  (Read 528 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 273
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I have finally managed to solder my TSYS01 high accuracy temperature sensor to a PCB and now I am looking to write a function to read the 24bit temperature value via I2C but the example looks pretty daunting to my eyes.... wondering if anyone can point me in the right direction on how to do this in Arduino speak?  http://meas-spec.com/downloads/TSYS01_Application_Note.pdf

Any pointers very much appreciated.
Logged

UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 273
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

..... I'm attempting to get this going with the little knowledge I have...... so far my code looks like this clearly there are some big mistakes on my part  smiley-red  At this stage I just hoping to start the conversion and then read the ADC result - once I can get a result I can work on converting this to a temperature.

Code:
#include <Wire.h>
#define address 0x77
int temperature;
const int data_bytes = 7;
byte result_buffer [data_bytes];


void setup()
{
  Serial.begin(9600);
  Wire.begin();
  Serial.println("TSYS01 Test");
}

void loop()
{   
    Serial.println("");
    ReadTSYS01();
    Serial.println("----------------");
    delay(1000);
}

int ReadTSYS01()
{
  Serial.println("Read TSYS01");
  Wire.beginTransmission(address);
  Wire.write(0x48);
  Wire.endTransmission();
 
  Wire.beginTransmission(address);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.requestFrom(address, data_bytes);

  while (!Wire.available ());
  for (int i = 0; i < data_bytes; ++i)
  {result_buffer [i] = Wire.read();}
 
  for (int i=0;i<7;i++)
  {
   byte d = result_buffer [i];
   if (i==1) temperature = d;
   if (i==2) temperature = (temperature << 8) | d;
  }
 
  Serial.print("Result: ");
  Serial.println(temperature);
 
}
Logged

UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 273
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Getting a bit closer - this code makes much more sense and delivers a result but it is the same result each time round the loop regardless of temperature?

Code:
#include <Wire.h>
#define TSYS01Address 0x77
#define TSYS01Reset 0x1E
#define TSYS01StartReg 0x48
#define TSYS01TempReg 0x00
long tempReading = 0;
float finalTempC = 0.0000;
byte MSB;
byte OSB;
byte LSB;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  TSYS01INIT();
}

void loop() {
  delay(100);
  TSYS01GetTemp();
  finalTempC = (tempReading/128.0);
  Serial.print("Temperature: ");
  Serial.print(finalTempC);
  Serial.print((char)176); // Degree symbol.
  Serial.println('C');
  Serial.print("MSB: ");
  Serial.println(MSB);
  Serial.print("OSB: ");
  Serial.println(OSB);
  Serial.print("LSB: ");
  Serial.println(LSB);
  Serial.println("-------------------------");
  Serial.println(" ");
  delay(5000);
}

void TSYS01INIT() {
  Wire.beginTransmission(TSYS01Address);
  Wire.write(0x1E);
  Wire.endTransmission();
}

void TSYS01GetTemp()
{
  Wire.beginTransmission(TSYS01Address);
  Wire.write(TSYS01StartReg);
  Wire.endTransmission();
  delay(100);

  Wire.beginTransmission(TSYS01Address);
  Wire.write(TSYS01TempReg);
  Wire.endTransmission();
 
  Wire.requestFrom(TSYS01Address,3);
  MSB = Wire.read();
  OSB = Wire.read();
  LSB = Wire.read();

  tempReading = ((MSB << 16) | (OSB << 8) | LSB);
}
Logged

UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 273
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still chipping away at this :-)

I'm now wondering if this is something to do with Arduino WIRE using 7 bits and this device using 8 bits - could this be the issue, can I still use this sensor with the Arduino WIRE library and if so how ?

This is what the byte structure looks like:


Cheers


* 8bit.JPG (48.49 KB, 906x344 - viewed 15 times.)
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm now wondering if this is something to do with Arduino WIRE using 7 bits and this device using 8 bits - could this be the issue

No.

Code:
tempReading = ((MSB << 16) | (OSB << 8) | LSB);

Use:

Code:
tempReading = (((unsigned long)MSB << 16) | ((unsigned long)OSB << 8) | (unsigned long)LSB);

Your original code loses the value of MSB, because MSB is a byte, which gets converted to a signed int by integral promotion, then you shift all the bits out of it.

Also change the type of tempReading to unsigned long.
« Last Edit: November 28, 2013, 05:58:12 pm by dc42 » Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

PS - the sample I2C code in the datasheet appears to read 4 bytes, not 3. I don't know why, because the protocol diagram shows 3 bytes. The comment at the head of the function isn't helpful either, it says "Reads four bytes of ADC result (24bit)".

I presume the CSB pin of the chip is tied to ground, and the PS pin is tied to +3.3V?
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 273
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks DC42, much appreciated.

Thanks for pointing out my way too small variable - I really didn't spot that one   smiley-red

Yip, that datasheet needs correcting doesn't it - well spotted, I will email them.  I have my CSB and PS tied as you describe but I'm still not getting good responses - I do get something back which is a start I guess.

I will keep plugging away and post back some code here when I get this working - cheers.
Logged

Pages: [1]   Go Up
Jump to: