forked from jpmulligan/Arduino-DS1620
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1620.cpp
executable file
·260 lines (217 loc) · 6.68 KB
/
ds1620.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Arduino DS1620 Library 0.1
Copyright (C) 2009 John P. Mulligan. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Project URL: http://wiki.thinkhole.org/ds1620
Datasheet URL: http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2735
DS1620 8-Pin DIP Pin Assignment:
1 - DQ 8 - VDD (2.7V - 5.5V)
2 - CLK/CONV 7 - T HIGH
3 - RST 6 - T LOW
4 - GND 5 - T COM
Serial Communications (3-Wire):
(1) Set RST high
(2) Send command, least significant bit first
(3) Read or write 8-bit config or 9-bit temperature
(5) Set RST low
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "ds1620.h"
// DS1620 Commands
#define READ_TEMP 0xAA // Read temperature register
#define WRITE_TH 0x01 // Write to the TH (High Temp) register
#define WRITE_TL 0x02 // Write to the TL (Low Temp) register
#define READ_TH 0xA1 // Read the TH (High Temp) register
#define READ_TL 0xA2 // Read the TL (Low Temp) register
#define READ_CNTR 0xA0 // Read the value of the counter byte
#define READ_SLOPE 0xA9 // Read the slope counter byte
#define START_CNV 0xEE // Begin temperature conversion
#define STOP_CNV 0x22 // Stop temperature conversion
#define WRITE_CFG 0x0C // Write configuration register
#define READ_CFG 0xAC // Read the value in the config register
#define WRITE_DELAY 20 // Time to wait (ms) after a EEPROM write
DS1620::DS1620(int DQ, int CLK, int RST)
{
pinMode(DQ, OUTPUT);
pinMode(RST, OUTPUT);
pinMode(CLK, OUTPUT);
_DQ = DQ;
_CLK = CLK;
_RST = RST;
}
int DS1620::read_temp()
{
short t;
rst_start();
send_command(READ_TEMP); // Next 9 clock cycles, last temp conv result
t = (short)receive_data();
rst_stop();
// Check sign bit from Temp
if (t & 0x0100)
{
// Temperature is negative
// According to specification: [256 (0x0100) - Temp]
t &= 0x00FF; // Remove the sign bit
t = 0x0100 - t;
t *= (short)-1;
}
t /= 2;
return(t);
}
void DS1620::write_th(int high_temp)
{
int bit;
high_temp = high_temp * 2;
rst_start();
send_command(WRITE_TH); // Next 9 clock cycles, value of the high temp limit
for(int n=0; n<9; n++){ // Send all nine bits of temperature
bit = (high_temp >> n) & 0x01;
digitalWrite(_DQ, bit); // DQ HIGH or LOW based on bit
digitalWrite(_CLK, LOW); // CLK LOW then HIGH to make one cycle
digitalWrite(_CLK, HIGH);
}
delay(WRITE_DELAY); // Write can take up to 10ms
rst_stop();
}
void DS1620::write_tl(int temp)
{
int bit;
temp = temp * 2;
rst_start();
send_command(WRITE_TL); // Next 9 clock cycles, value of the high temp limit
for(int n=0; n<9; n++){ // Send all nine bits of temperature
bit = (temp >> n) & 0x01;
digitalWrite(_DQ, bit); // DQ HIGH or LOW based on bit
digitalWrite(_CLK, LOW); // CLK LOW then HIGH to make one cycle
digitalWrite(_CLK, HIGH);
}
delay(WRITE_DELAY); // Write can take up to 10ms
rst_stop();
}
int DS1620::read_th()
{
int temp = 0;
rst_start();
send_command(READ_TH); // Next 8 clock cycles output value of config register
temp = receive_data()/2;
rst_stop();
return(temp);
}
int DS1620::read_tl()
{
int temp = 0;
rst_start();
send_command(READ_TL); // Next 8 clock cycles output value of config register
temp = receive_data()/2;
rst_stop();
return(temp);
}
int DS1620::read_counter()
{
int counter = 0;
rst_start();
send_command(READ_CNTR); // Next 9 clock cycles output value of counter
counter = receive_data();
rst_stop();
return(counter);
}
int DS1620::read_slope()
{
int slope = 0;
rst_start();
send_command(READ_SLOPE); // Next 9 clock cycles output value of counter
slope = receive_data();
rst_stop();
return(slope);
}
void DS1620::start_conv()
{
rst_start();
send_command(START_CNV); // Begins temp conv, depends on 1-shot mode
rst_stop();
}
void DS1620::stop_conv()
{
rst_start();
send_command(STOP_CNV); // Stops temperature conversion
rst_stop();
}
int DS1620::write_config(int config_register)
{
/*
Return codes: 0 = Write successful
1 = Write verification failed
2 = Bad config register
*/
if(config_register > 0) {
rst_start();
send_command(WRITE_CFG); // Next 8 clock cycles, value of config register;
send_command(config_register);
delay(WRITE_DELAY); // Write can take up to 10ms
rst_stop();
// Confirm that config was properly written
if(read_config() == config_register) { return 0; }
else { return 1; }
}
return 2;
}
int DS1620::read_config()
{
int config_register = 0;
rst_start();
send_command(READ_CFG); // Next 8 clock cycles output value of config register
config_register = receive_data();
rst_stop();
return(config_register);
}
int DS1620::receive_data()
{
int data = 0;
int n;
int bit;
pinMode(_DQ, INPUT); // Change Data/DQ pin mode to accept INPUT
for(n=0; n<9; n++) { // Always receive 9 bits of data
digitalWrite(_CLK, LOW);
bit = digitalRead(_DQ);
digitalWrite(_CLK, HIGH);
data = data | bit << n;
}
pinMode(_DQ, OUTPUT); // Done reading, set back to OUTPUT
return(data);
}
void DS1620::rst_start()
{
digitalWrite(_RST, LOW);
digitalWrite(_CLK, HIGH);
digitalWrite(_RST, HIGH); // All communications start by taking RST HIGH
}
void DS1620::rst_stop()
{
digitalWrite(_RST, LOW); // Taking RST LOW will terminate any communication
}
void DS1620::send_command(int command)
{
int n;
int bit;
for(n=0; n<8; n++){ // Always send 8 bits of data
// Arithmetic bitwise shift command to the right, then AND with
// bitmask (00000001) to return next bit, least significant (rightmost) first
bit = (command >> n) & 0x01;
digitalWrite(_DQ, bit); // DQ HIGH or LOW based on bit
digitalWrite(_CLK, LOW); // CLK LOW then HIGH to make one cycle
digitalWrite(_CLK, HIGH);
}
}