-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathADC128D818.cpp
309 lines (234 loc) · 6.03 KB
/
ADC128D818.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
308
309
#include "ADC128D818.h"
// Public functions
ADC128::ADC128(uint8_t address, TwoWire *theWire)
{
_i2caddr = address;
_i2c = theWire;
}
bool ADC128::available(void)
{
_i2c->beginTransmission(_i2caddr);
if (_i2c->endTransmission() != 0)
{
return false;
}
return true;
}
bool ADC128::start(void)
{
_configuration |= 1;
return write(CONFIGURATION, _configuration);
}
bool ADC128::stop(void)
{
_configuration &= ~1;
return write(CONFIGURATION, _configuration);
}
bool ADC128::reset(void)
{
_configuration = 0;
return write(CONFIGURATION, 1 << 7);
}
bool ADC128::write_configuration(ADC128_configuration configuration)
{
uint8_t data = (configuration.start) + (configuration.interupt_enable << 1) + (configuration.clear_interrupt << 3) + (configuration.clear_initialization << 7);
_configuration = data;
return write(CONFIGURATION, data);
}
bool ADC128::read_configuration(ADC128_configuration *configuration)
{
uint8_t reply;
if (!read(CONFIGURATION, &reply, 1, 0))
{
return false;
}
_configuration = reply;
configuration->start = reply & 0x01;
configuration->interupt_enable = (reply >> 1) & 0x01;
configuration->clear_interrupt = (reply >> 3) & 0x01;
configuration->clear_initialization = (reply >> 7) & 0x01;
return true;
}
bool ADC128::read_interrupt_state(ADC128_channels *interrupts)
{
uint8_t reply;
if (!read(INTERRUPT_STATUS, &reply, 1, 0))
{
return false;
}
interrupts->from_byte(reply);
return true;
}
bool ADC128::write_interrupt_mask(ADC128_channels masks)
{
return write(INTERRUPT_MASK, masks.to_byte());
}
bool ADC128::read_interrupt_mask(ADC128_channels *masks)
{
uint8_t reply;
if (!read(INTERRUPT_MASK, &reply, 1, 0))
{
return false;
}
masks->from_byte(reply);
return true;
}
// set_conversion_rate sets the conversion rate to either
// CONTINUOUS: continuous conversion mode
// LOW_POWER: low power conversion mode
// As the setting can only be done when the device is in shutdown mode we will first stop the device, edit the conversion rate and start it again.
bool ADC128::set_conversion_rate(bool mode)
{
uint8_t temp = _configuration;
if (temp & 0x01) // Check if the ADC has been started yet.
{
if (!ADC128::stop()) // Stop the ADC.
{
return false;
}
}
if (!write(CONVERSION_RATE, mode))
{
return false;
}
if (temp & 0x01) // Check if the ADC was started before changing the conversion rate.
{
if (!ADC128::start()) // Start the ADC.
{
return false;
}
}
return true;
}
bool ADC128::write_disabled_channels(ADC128_channels disabled)
{
uint8_t temp = _configuration;
if (temp & 0x01) // Check if the ADC has been started yet.
{
if (!ADC128::stop()) // Stop the ADC.
{
return false;
}
}
if (!write(CHANNEL_DISABLE, disabled.to_byte()))
{
return false;
}
if (temp & 0x01) // Check if the ADC was started before changing the conversion rate.
{
if (!ADC128::start()) // Start the ADC.
{
return false;
}
}
return true;
}
bool ADC128::read_disabled_channels(ADC128_channels *disabled)
{
uint8_t reply;
if (!read(CHANNEL_DISABLE, &reply, 1, 0))
{
return false;
}
disabled->from_byte(reply);
return true;
}
bool ADC128::initiate_single_conversion(void)
{
return write(ONE_SHOT, true);
}
bool ADC128::initiate_deep_shutdown(void)
{
ADC128::stop();
return write(DEEP_SHUTDOWN, true);
}
bool ADC128::write_advanced_configuration(ADC128_advanced_configuration configuration)
{
uint8_t data = (configuration.external_reference_enable) + (configuration.selected_mode << 1);
return write(ADVANCED_CONFIGURATION, data);
}
bool ADC128::read_advanced_configuration(ADC128_advanced_configuration *configuration)
{
uint8_t reply;
if (!read(ADVANCED_CONFIGURATION, &reply, 1, 0))
{
return false;
}
configuration->external_reference_enable = reply & 0x01;
configuration->selected_mode = (reply & 0x06) >> 1;
return true;
}
// read_busy_status returns true if the device is busy with either converting or starting up.
bool ADC128::read_busy_status(void)
{
uint8_t reply;
read(BUSY_STATUS, &reply, 1, 0);
if ((reply & 0x01) == 1 || ((reply >> 1) & 0x1) == 1)
{
return true;
}
return false;
}
uint16_t ADC128::read_ADC_channel(uint8_t channel)
{
if (channel > 7)
{
return false;
}
uint8_t reply[2];
read(CHANNEL_READING_START + channel, reply, 2, 0);
return (uint16_t)(reply[0] << 4) + (reply[1] >> 4); // The data is MSB first with 4 bytes padded on the right side.
}
// set_channel_limits sets the upper and lower channel limits.
bool ADC128::set_channel_limits(uint8_t channel, int8_t high_limit, int8_t low_limit)
{
if (channel > 7)
{
return false;
}
if (!write(CHANNEL_LIMITS_START + (channel * 2), high_limit))
{
return false;
}
if (!write(CHANNEL_LIMITS_START + (channel * 2) + 1, low_limit))
{
return false;
}
}
uint8_t ADC128::read_manufacturer_id(void)
{
uint8_t reply;
read(MANUFACTURER_ID, &reply, 1, 0);
return reply;
}
uint8_t ADC128::read_revision_id(void)
{
uint8_t reply;
read(REVISION_ID, &reply, 1, 0);
return reply;
}
// Private functions
bool ADC128::write(uint8_t command, uint8_t data)
{
_i2c->beginTransmission(_i2caddr);
_i2c->write(command);
_i2c->write(data);
if (_i2c->endTransmission() != 0)
{
return false;
}
return true;
}
bool ADC128::read(uint8_t command, uint8_t *readdata, uint8_t readlen, uint8_t _delay)
{
_i2c->beginTransmission(_i2caddr);
_i2c->write(command);
_i2c->endTransmission();
delay(_delay);
_i2c->requestFrom(_i2caddr, readlen);
for (int i = 0; i < readlen; i++)
{
readdata[i] = _i2c->read();
}
return true;
}