-
Notifications
You must be signed in to change notification settings - Fork 0
/
LM95071.cpp
156 lines (101 loc) · 2.81 KB
/
LM95071.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*Library: SPI Control of a Texas Instruments LM95071 SPI temperature Sensor
Version: 1.1.0
Developer: Daniel Melendrez
email: dmelendrez(at)gmail(dot)com
Category: Sensors
Date: Oct 2018 - 0.1.5 Initial release
July 2020 - 1.1.0 General tidy up, more comments added
March 2023 = 1.2.0 Added more exmaples. Cleaned up comments
*/
#include "LM95071.h"
// Constructor
LM95071::LM95071(uint8_t pin, boolean debug) {
_SelDev_Pin = pin;
_debug = debug;
}
void LM95071::begin(void) {
SPI.begin();
pinMode(_SelDev_Pin, OUTPUT);
digitalWrite(_SelDev_Pin, HIGH);
}
float LM95071::getTemperature(void) {
float final;
_tempHex = readTemp(BYTES_TO_READ);
final = celsiusConversion(_tempHex);
if (_debug) {
Serial.print("Current temp = ");
Serial.println(final);
Serial.print(char(194));
Serial.println("C");
}
return final;
}
short LM95071::readTemp(int bytesToRead) {
byte inByte = 0;
short result;
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE1));
digitalWrite(_SelDev_Pin, LOW);
result = SPI.transfer(CONTINUOUS_CONV);
bytesToRead--;
if (_debug) {
Serial.print("1st byte = ");
Serial.println(result, HEX);
}
if (bytesToRead > 0) {
result = result << 8;
inByte = SPI.transfer(CONTINUOUS_CONV);
result = result | inByte;
if (_debug) {
Serial.print("2nd byte = ");
Serial.println(inByte, HEX);
}
bytesToRead--;
}
SPI.endTransaction();
digitalWrite(_SelDev_Pin, HIGH);
return result;
}
float LM95071::celsiusConversion(short val) {
float result = 0.0;
short dummy, shifted;
dummy = val ^ MASK_XOR;
shifted = dummy / 0x04; // Complement of two operation for retrieving final temp
result = shifted * RESOLUTION;
if (_debug) {
Serial.print("Hex value = ");
Serial.print(_tempHex, HEX);
Serial.print(" ;");
Serial.print("Xor value = ");
Serial.print(dummy, HEX);
Serial.print(" ;");
Serial.print("Shifted = ");
Serial.print(shifted);
Serial.print(" ;");
}
return result;
}
short LM95071::readID(){
byte inByte = 0;
short ID;
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE1));
digitalWrite(_SelDev_Pin, LOW);
result = SPI.transfer(SHUTDOWN_MODE);
bytesToRead--;
if (_debug) {
Serial.print("1st byte = ");
Serial.println(result, HEX);
}
if (bytesToRead > 0) {
ID = ID << 8;
inByte = SPI.transfer(SHUTDOWN_MODE);
ID = ID | inByte;
if (_debug) {
Serial.print("2nd byte = ");
Serial.println(inByte, HEX);
}
bytesToRead--;
}
SPI.endTransaction();
digitalWrite(_SelDev_Pin, HIGH);
return ID;
}