-
Notifications
You must be signed in to change notification settings - Fork 13
/
TSYS01.cpp
86 lines (65 loc) · 1.77 KB
/
TSYS01.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "TSYS01.h"
#include <Wire.h>
#define TSYS01_ADDR 0x77
#define TSYS01_RESET 0x1E
#define TSYS01_ADC_READ 0x00
#define TSYS01_ADC_TEMP_CONV 0x48
#define TSYS01_PROM_READ 0XA0
TSYS01::TSYS01() {
}
bool TSYS01::init() {
// Reset the TSYS01, per datasheet
Wire.beginTransmission(TSYS01_ADDR);
Wire.write(TSYS01_RESET);
Wire.endTransmission();
delay(10);
int received_bytes = 0;
// Read calibration values
for ( uint8_t i = 0 ; i < 8 ; i++ ) {
Wire.beginTransmission(TSYS01_ADDR);
Wire.write(TSYS01_PROM_READ+i*2);
Wire.endTransmission();
received_bytes += Wire.requestFrom(TSYS01_ADDR,2);
C[i] = (Wire.read() << 8) | Wire.read();
}
return received_bytes > 0;
}
void TSYS01::read() {
Wire.beginTransmission(TSYS01_ADDR);
Wire.write(TSYS01_ADC_TEMP_CONV);
Wire.endTransmission();
delay(10); // Max conversion time per datasheet
Wire.beginTransmission(TSYS01_ADDR);
Wire.write(TSYS01_ADC_READ);
Wire.endTransmission();
Wire.requestFrom(TSYS01_ADDR,3);
D1 = 0;
D1 = Wire.read();
D1 = (D1 << 8) | Wire.read();
D1 = (D1 << 8) | Wire.read();
calculate();
}
void TSYS01::readTestCase() {
C[0] = 0;
C[1] = 28446; //0xA2 K4
C[2] = 24926; //0XA4 k3
C[3] = 36016; //0XA6 K2
C[4] = 32791; //0XA8 K1
C[5] = 40781; //0XAA K0
C[6] = 0;
C[7] = 0;
D1 = 9378708.0f;
adc = D1/256;
calculate();
}
void TSYS01::calculate() {
adc = D1/256;
TEMP = (-2) * float(C[1]) / 1000000000000000000000.0f * pow(adc,4) +
4 * float(C[2]) / 10000000000000000.0f * pow(adc,3) +
(-2) * float(C[3]) / 100000000000.0f * pow(adc,2) +
1 * float(C[4]) / 1000000.0f * adc +
(-1.5) * float(C[5]) / 100 ;
}
float TSYS01::temperature() {
return TEMP;
}