-
Notifications
You must be signed in to change notification settings - Fork 0
/
LM75.cpp
118 lines (94 loc) · 2.72 KB
/
LM75.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
/*
LM75 - An arduino library for the LM75 temperature sensor
Copyright (C) 2011 Dan Fekete <thefekete AT gmail DOT com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Energia.h>
#ifdef __TM4C123GH6PM__
#include <../Wire/Wire.h>
#else
#include <Wire.h>
#endif
#include "LM75.h"
LM75::LM75 () {
address = LM75_ADDRESS;
}
LM75::LM75 (byte addr) {
address = addr;
}
word LM75::float2regdata (float temp)
{
// First multiply by 8 and coerce to integer to get +/- whole numbers
// Then coerce to word and bitshift 5 to fill out MSB
return (word)((int)(temp * 8) << 5);
}
float LM75::regdata2float (word regdata)
{
return ((float)(int)regdata / 32) / 8;
}
word LM75::_register16 (byte reg) {
Wire.beginTransmission(address);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(address, 2);
word regdata = (Wire.read() << 8) | Wire.read();
return regdata;
}
void LM75::_register16 (byte reg, word regdata) {
byte msb = (byte)(regdata >> 8);
byte lsb = (byte)(regdata);
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(msb);
Wire.write(lsb);
Wire.endTransmission();
}
word LM75::_register8 (byte reg) {
Wire.beginTransmission(address);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(address, 1);
return Wire.read();
}
void LM75::_register8 (byte reg, byte regdata) {
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(regdata);
Wire.endTransmission();
}
float LM75::temp (void) {
return regdata2float(_register16(LM75_TEMP_REGISTER));
}
byte LM75::conf () {
return _register8(LM75_CONF_REGISTER);
}
void LM75::conf (byte data) {
_register8(LM75_CONF_REGISTER, data);
}
float LM75::tos () {
return regdata2float(_register16(LM75_TOS_REGISTER));
}
void LM75::tos (float temp) {
_register16(LM75_TOS_REGISTER, float2regdata(temp));
}
float LM75::thyst () {
return regdata2float(_register16(LM75_THYST_REGISTER));
}
void LM75::thyst (float temp) {
_register16(LM75_THYST_REGISTER, float2regdata(temp));
}
boolean LM75::shutdown () {
return conf() & 0x01;
}
void LM75::shutdown (boolean val) {
conf(val << LM75_CONF_SHUTDOWN);
}