-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSHT7x.c
247 lines (212 loc) · 4.28 KB
/
SHT7x.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* SHT71.c
*
* Created: 12/29/2016 11:56:57 PM
* Author: Will Choi
*/
#include "SHT7x.h"
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#define SCKPORT 0
#define VDDPORT 1
#define GNDPORT 2
#define DATAPORT 3
#define COMMAND_READ_TEMPTERATURE 0b00000011
#define COMMAND_READ_HUMIDITY 0b00000101
unsigned char crc_ = 0;
void InitializeSensor() {
// Set ports 0 and 3 (SCK, and DATA respectively) as "output".
DDRB = _BV(SCKPORT) | _BV(VDDPORT) | _BV(GNDPORT) | _BV(DATAPORT);
// DATA line starts HIGH.
PORTB |= _BV(DATAPORT);
// Supply voltage to Vdd.
PORTB |= _BV(VDDPORT);
// Wait for the sensor to set up.
_delay_ms(20);
}
static void ClockUp() {
// Source current in port E0.
PORTB |= _BV(SCKPORT);
asm("nop");
asm("nop");
asm("nop");
}
static void ClockDown() {
// Sink current in port E0.
PORTB &= ~_BV(SCKPORT);
asm("nop");
asm("nop");
asm("nop");
}
static void DataUp() {
PORTB |= _BV(DATAPORT);
asm("nop");
asm("nop");
asm("nop");
}
static void DataDown() {
PORTB &= ~_BV(DATAPORT);
asm("nop");
asm("nop");
asm("nop");
}
static void IssueTransmitStart() {
ClockUp();
DataDown();
ClockDown();
ClockUp();
DataUp();
ClockDown();
DataDown();
}
static void StartCRC() {
crc_ = 0b00001111 & 0; /* TODO status register */
}
static unsigned char EndCRC() {
unsigned char temp = 0;
temp |= (crc_ << 7) & (1 << 7);
temp |= (crc_ << 5) & (1 << 6);
temp |= (crc_ << 3) & (1 << 5);
temp |= (crc_ << 1) & (1 << 4);
temp |= (crc_ >> 1) & (1 << 3);
temp |= (crc_ >> 3) & (1 << 2);
temp |= (crc_ >> 5) & (1 << 1);
temp |= (crc_ >> 7) & (1);
return temp;
}
static void CalcCRCBitOne() {
// If bit 7 isn't set,
if((crc_ & 0b10000000) == 0) {
crc_ <<= 1;
// Invert bits 4 and 5.
crc_ ^= 1 << 4;
crc_ ^= 1 << 5;
// Set bit 0 to 1.
crc_ |= 1;
} else {
crc_ <<= 1;
crc_ &= ~0b00000001;
}
}
static void CalcCRCBitZero() {
// If bit 7 is set,
if(crc_ & 0b10000000) {
crc_ <<= 1;
// Invert bits 4 and 5.
crc_ ^= 1 << 4;
crc_ ^= 1 << 5;
// Set bit 0 to 1.
crc_ |= 1;
} else {
crc_ <<= 1;
crc_ &= ~0b00000001;
}
}
unsigned char IssueCommand(const unsigned char command) {
for(unsigned char i = 0; i < 8; ++i) {
if(command & (1 << (7-i))) {
DataUp();
ClockUp();
ClockDown();
if(i != 7) {
DataDown();
}
CalcCRCBitOne();
}
else {
ClockUp();
ClockDown();
CalcCRCBitZero();
}
}
// Read ACK bit.
PORTB &= ~_BV(DATAPORT);
DDRB &= ~_BV(DATAPORT);
ClockUp();
if(PINB & _BV(PINB3)) {
// ACK not received
PORTB |= _BV(DATAPORT);
DDRB |= _BV(DATAPORT);
ClockDown();
return 1;
}
ClockDown();
return 0;
}
static unsigned char ReadSensor(double* value, unsigned char command) {
IssueTransmitStart();
StartCRC();
if(IssueCommand(command)) {
return 1;
}
// Loop until sensor pulls DATA low.
loop_until_bit_is_clear(PINB, PINB3);
unsigned char high = 0, low = 0, crc = 0;
for(char i = 0; i < 8; ++i) {
ClockUp();
if(PINB & _BV(PINB3)) {
high |= 1 << (7-i);
CalcCRCBitOne();
}
else {
CalcCRCBitZero();
}
ClockDown();
}
// Send ACK.
DDRB |= _BV(DATAPORT);
ClockUp();
ClockDown();
DDRB &= ~_BV(DATAPORT);
for(char i = 0; i < 8; ++i) {
ClockUp();
if(PINB & _BV(PINB3)) {
low |= 1 << (7-i);
CalcCRCBitOne();
}
else {
CalcCRCBitZero();
}
ClockDown();
}
// Send ACK.
DDRB |= _BV(DATAPORT);
ClockUp();
ClockDown();
DDRB &= ~_BV(DATAPORT);
for(char i = 0; i < 8; ++i) {
ClockUp();
if(PINB & _BV(PINB3)) {
crc |= 1 << (7-i);
}
ClockDown();
}
// Send ACK.
DDRB |= _BV(DATAPORT);
DataUp();
ClockUp();
ClockDown();
uint16_t raw_value = 0;
raw_value |= low;
raw_value |= high << 8;
*value = (double)raw_value;
return 0;
}
unsigned char ReadTemperature(double* temperature) {
double raw_value;
unsigned char return_val = ReadSensor(&raw_value, COMMAND_READ_TEMPTERATURE);
if(!return_val) {
*temperature = -40.1 + 0.01 * raw_value;
}
return return_val;
}
unsigned char ReadHumidity(double* humidity, double temperature) {
double raw_value;
unsigned char return_val = ReadSensor(&raw_value, COMMAND_READ_HUMIDITY);
if(!return_val) {
double rh_linear = -2.0468 + 0.0367 * raw_value + -1.5955E-6 * raw_value * raw_value;
*humidity = (temperature - 25.0) * (0.01 + 0.00008 * raw_value) + rh_linear;
}
return return_val;
}