-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathADS123X.cpp
307 lines (261 loc) · 6.95 KB
/
ADS123X.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
V0.2
ADS123X.cpp - Library for reading from an ADS1232 and ADS1234 24-bit ADC.
Created by Hamid Saffari @ Jan 2018. https://github.com/HamidSaffari/ADS123X
Released into the public domain.
*/
#include "ADS123X.h"
#if ARDUINO_VERSION <= 106
// "yield" is not implemented as noop in older Arduino Core releases, so let's define it.
// See also: https://stackoverflow.com/questions/34497758/what-is-the-secret-of-the-arduino-yieldfunction/34498165#34498165
void yield(void) {};
#endif
ADS123X::ADS123X() {
}
ADS123X::~ADS123X() {
}
void ADS123X::begin(byte pin_DOUT, byte pin_SCLK, byte pin_PDWN, byte pin_GAIN0, byte pin_GAIN1, byte pin_SPEED, byte pin_A0, byte pin_A1_or_TEMP, Gain gain, Speed speed){
_pin_DOUT = pin_DOUT;
_pin_SCLK = pin_SCLK;
_pin_PDWN = pin_PDWN;
_pin_GAIN0 = pin_GAIN0;
_pin_GAIN1 = pin_GAIN1;
_pin_SPEED = pin_SPEED;
_pin_A0 = pin_A0;
_pin_A1_or_TEMP = pin_A1_or_TEMP;
pinMode(_pin_DOUT, INPUT_PULLUP);
pinMode(_pin_SCLK, OUTPUT);
pinMode(_pin_PDWN, OUTPUT);
pinMode(_pin_GAIN0, OUTPUT);
pinMode(_pin_GAIN1, OUTPUT);
pinMode(_pin_SPEED, OUTPUT);
pinMode(_pin_A0, OUTPUT);
pinMode(_pin_A1_or_TEMP, OUTPUT);
setGain(gain);
setSpeed(speed);
power_up();
}
bool ADS123X::is_ready(void)
{
return digitalRead(_pin_DOUT) == LOW;
}
void ADS123X::setGain(Gain gain)
{
switch(gain)
{
case GAIN1:
{
digitalWrite(_pin_GAIN1, LOW);
digitalWrite(_pin_GAIN0, LOW);
break;
}
case GAIN2:
{
digitalWrite(_pin_GAIN1, LOW);
digitalWrite(_pin_GAIN0, HIGH);
break;
}
case GAIN64:
{
digitalWrite(_pin_GAIN1, HIGH);
digitalWrite(_pin_GAIN0, LOW);
break;
}
case GAIN128:
{
digitalWrite(_pin_GAIN1, HIGH);
digitalWrite(_pin_GAIN0, HIGH);
break;
}
}
}
void ADS123X::power_up(void)
{
digitalWrite(_pin_PDWN, HIGH);
// Set CLK low to get the ADS1231 out of suspend
digitalWrite(_pin_SCLK, LOW);
}
void ADS123X::power_down(void)
{
digitalWrite(_pin_PDWN, LOW);
digitalWrite(_pin_SCLK, HIGH);
}
void ADS123X::setSpeed(Speed speed)
{
_speed = speed;
switch(speed)
{
case SLOW:
{
digitalWrite(_pin_SPEED, LOW);
break;
}
case FAST:
{
digitalWrite(_pin_SPEED, HIGH);
break;
}
}
}
void ADS123X::setChannel(Channel channel)
{
switch(channel)
{
case AIN1:
{
digitalWrite(_pin_A1_or_TEMP, LOW);
digitalWrite(_pin_A0, LOW);
break;
}
case AIN2:
{
digitalWrite(_pin_A1_or_TEMP, LOW);
digitalWrite(_pin_A0, HIGH);
break;
}
#if defined ADS1232
case TEMP:
{
digitalWrite(_pin_A1_or_TEMP, HIGH);
digitalWrite(_pin_A0, LOW);
break;
}
#elif defined ADS1234
case AIN3:
{
digitalWrite(_pin_A1_or_TEMP, HIGH);
digitalWrite(_pin_A0, LOW);
break;
}
case AIN4:
{
digitalWrite(_pin_A1_or_TEMP, HIGH);
digitalWrite(_pin_A0, HIGH);
break;
}
#endif
}
}
/*
* Get the raw ADC value. Can block up to 100ms in normal operation.
* Returns 0 on success, an error code otherwise.
*/
ERROR_t ADS123X::read(Channel channel,long& value, bool Calibrating)
{
int i=0;
unsigned long start;
unsigned int waitingTime;
unsigned int SettlingTimeAfterChangeChannel=0;
if(channel!=lastChannel){
setChannel(channel);
if(_speed==FAST) SettlingTimeAfterChangeChannel=55;
else SettlingTimeAfterChangeChannel=405;
lastChannel=channel;
}
/* A high to low transition on the data pin means that the ADS1231
* has finished a measurement (see datasheet page 13).
* This can take up to 100ms (the ADS1231 runs at 10 samples per
* second!).
* Note that just testing for the state of the pin is unsafe.
*/
if(Calibrating){
if(_speed==FAST) waitingTime=150;
else waitingTime=850;
}
else{
if(_speed==FAST) waitingTime=20;
else waitingTime=150;
}
waitingTime+=SettlingTimeAfterChangeChannel;
waitingTime+=600; //[ms] Add some extra time ( sometimes takes longer than what datasheet claims! )
start=millis();
while(digitalRead(_pin_DOUT) != HIGH)
{
if(millis()-start > waitingTime)
return TIMEOUT_HIGH; // Timeout waiting for HIGH
}
start=millis();
while(digitalRead(_pin_DOUT) != LOW)
{
if(millis()-start > waitingTime)
return TIMEOUT_LOW; // Timeout waiting for LOW
}
// Read 24 bits
for(i=23 ; i >= 0; i--) {
digitalWrite(_pin_SCLK, HIGH);
value = (value << 1) + digitalRead(_pin_DOUT);
digitalWrite(_pin_SCLK, LOW);
}
if(Calibrating){
// 2 extra bits for calibrating
for(i=1 ; i >= 0; i--) {
digitalWrite(_pin_SCLK, HIGH);
digitalWrite(_pin_SCLK, LOW);
}
}
/* Bit 23 is acutally the sign bit. Shift by 8 to get it to the
* right position (31), divide by 256 to restore the correct value.
*/
value = (value << 8) / 256;
if(!Calibrating){
/* The data pin now is high or low depending on the last bit that
* was read.
* To get it to the default state (high) we toggle the clock one
* more time (see datasheet).
*/
digitalWrite(_pin_SCLK, HIGH);
digitalWrite(_pin_SCLK, LOW);
}
return NoERROR; // Success
}
ERROR_t ADS123X::read_average(Channel channel, float& value, byte times, bool Calibrating) {
long sum = 0;
ERROR_t err;
for (byte i = 0; i < times; i++) {
long val;
err = read(channel, val, Calibrating);
if(err!=NoERROR) return err;
sum += val;
yield();
}
if(times==0) return DIVIDED_by_ZERO;
value = (float)sum / times;
return NoERROR;
}
ERROR_t ADS123X::get_value(Channel channel, float& value, byte times, bool Calibrating) {
float val = 0;
ERROR_t err;
err = read_average(channel, val, times, Calibrating);
if(err!=NoERROR) return err;
value = val - OFFSET[channel-1];
return NoERROR;
}
ERROR_t ADS123X::get_units(Channel channel, float& value, byte times, bool Calibrating) {
float val = 0;
ERROR_t err;
err = get_value(channel, val, times, Calibrating);
if(err!=NoERROR) return err;
if(SCALE[channel-1]==0) return DIVIDED_by_ZERO;
value = val / SCALE[channel-1];
return NoERROR;
}
ERROR_t ADS123X::tare(Channel channel, byte times, bool Calibrating) {
ERROR_t err;
float sum = 0;
err = read_average(channel, sum, times, Calibrating);
if(err!=NoERROR) return err;
set_offset(channel, sum);
return NoERROR;
}
void ADS123X::set_scale(Channel channel, float scale) {
SCALE[channel-1] = scale;
}
float ADS123X::get_scale(Channel channel) {
return SCALE[channel-1];
}
void ADS123X::set_offset(Channel channel, float offset) {
OFFSET[channel-1] = offset;
}
float ADS123X::get_offset(Channel channel) {
return OFFSET[channel-1];
}