-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADS7828.cpp
More file actions
253 lines (226 loc) · 7.32 KB
/
ADS7828.cpp
File metadata and controls
253 lines (226 loc) · 7.32 KB
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
/**************************************************************************/
/*
Distributed with a free-will license.
Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
ADS7828
This code is designed to work with the ADS7828_I2CADC I2C Mini Module available from ControlEverything.com.
https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS7828_I2CADC#tabs-0-product_tabset-2
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "ADS7828.h"
/**************************************************************************/
/*
Abstract away platform differences in Arduino wire library
*/
/**************************************************************************/
static uint8_t i2cread(void)
{
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
/**************************************************************************/
/*
Abstract away platform differences in Arduino wire library
*/
/**************************************************************************/
static void i2cwrite(uint8_t x)
{
#if ARDUINO >= 100
Wire.write((uint8_t)x);
#else
Wire.send(x);
#endif
}
/**************************************************************************/
/*
Writes 8-bits to the destination register
*/
/**************************************************************************/
static void writeRegister(uint8_t i2cAddress, uint8_t reg)
{
Wire.beginTransmission(i2cAddress);
i2cwrite((uint8_t)reg);
Wire.endTransmission();
}
/**************************************************************************/
/*
Reads 16-bits from the destination register
*/
/**************************************************************************/
static uint16_t readRegister(uint8_t i2cAddress)
{
Wire.beginTransmission(i2cAddress);
Wire.endTransmission();
Wire.requestFrom(i2cAddress, (uint8_t)2);
return (int16_t)((i2cread() << 8) | i2cread());
}
/**************************************************************************/
/*
Instantiates a new ADS7828 class with appropriate properties
*/
/**************************************************************************/
void ADS7828::getAddr_ADS7828(uint8_t i2cAddress)
{
ads_i2cAddress = i2cAddress;
ads_conversionDelay = ADS7828_CONVERSIONDELAY;
}
/**************************************************************************/
/*
Sets up the Hardware
*/
/**************************************************************************/
void ADS7828::begin()
{
Wire.begin();
}
/**************************************************************************/
/*
Sets the Single-Ended/Differential Inputs
*/
/**************************************************************************/
void ADS7828::setSDMode(adsSDMode_t sdmode)
{
ads_sdmode = sdmode;
}
/**************************************************************************/
/*
Gets the Single-Ended/Differential Inputs
*/
/**************************************************************************/
adsSDMode_t ADS7828::getSDMode()
{
return ads_sdmode;
}
/**************************************************************************/
/*
Sets the Power-Down Mode
*/
/**************************************************************************/
void ADS7828::setPDMode(adsPDMode_t pdmode)
{
ads_pdmode = pdmode;
}
/**************************************************************************/
/*
Gets the Power-Down Mode
*/
/**************************************************************************/
adsPDMode_t ADS7828::getPDMode()
{
return ads_pdmode;
}
/**************************************************************************/
/*
Reads the conversion results, measuring the voltage
for a single-ended ADC reading from the specified channel
Negative voltages cannot be applied to this circuit because the
ADS7828 can only accept positive voltages in this mode
*/
/**************************************************************************/
uint16_t ADS7828::Measure_SingleEnded(uint8_t channel)
{
if (channel > 7)
{
return 0;
}
uint8_t config = 0;
// Set Single-Ended/Differential Inputs
config |= ads_sdmode;
// Set Power-Down Selection
config |= ads_pdmode;
// Set single-ended input channel
switch (channel)
{
case (0):
config |= ADS7828_REG_COMMAND_CH_SINGLE_0;
break;
case (1):
config |= ADS7828_REG_COMMAND_CH_SINGLE_1;
break;
case (2):
config |= ADS7828_REG_COMMAND_CH_SINGLE_2;
break;
case (3):
config |= ADS7828_REG_COMMAND_CH_SINGLE_3;
break;
case (4):
config |= ADS7828_REG_COMMAND_CH_SINGLE_4;
break;
case (5):
config |= ADS7828_REG_COMMAND_CH_SINGLE_5;
break;
case (6):
config |= ADS7828_REG_COMMAND_CH_SINGLE_6;
break;
case (7):
config |= ADS7828_REG_COMMAND_CH_SINGLE_7;
break;
}
// Write config register to the ADC
writeRegister(ads_i2cAddress, config);
// Wait for the conversion to complete
delay(ads_conversionDelay);
// Read the conversion results
// 16-bit unsigned results for the ADS7828
return readRegister(ads_i2cAddress);
}
/**************************************************************************/
/*
Reads the conversion results, measuring the voltage
difference between the P (CH#) and N (CH#) input
Generates a signed value since the difference can be either
positive or negative
*/
/**************************************************************************/
int16_t ADS7828::Measure_Differential(uint8_t channel)
{
uint8_t config = 0;
// Set Single-Ended/Differential Inputs
config |= ads_sdmode;
// Set Power-Down Selection
config |= ads_pdmode;
// Set Differential input channel
switch (channel)
{
case (01):
config |= ADS7828_REG_COMMAND_CH_DIFF_0_1; // CH0 = P, CH1 = N
break;
case (10):
config |= ADS7828_REG_COMMAND_CH_DIFF_1_0; // CH1 = P, CH0 = N
break;
case (23):
config |= ADS7828_REG_COMMAND_CH_DIFF_2_3; // CH2 = P, CH3 = N
break;
case (32):
config |= ADS7828_REG_COMMAND_CH_DIFF_3_2; // CH3 = P, CH2 = N
break;
case (45):
config |= ADS7828_REG_COMMAND_CH_DIFF_4_5; // CH4 = P, CH5 = N
break;
case (54):
config |= ADS7828_REG_COMMAND_CH_DIFF_5_4; // CH5 = P, CH4 = N
break;
case (67):
config |= ADS7828_REG_COMMAND_CH_DIFF_6_7; // CH6 = P, CH7 = N
break;
case (76):
config |= ADS7828_REG_COMMAND_CH_DIFF_7_6; // CH7 = P, CH6 = N
break;
}
// Write config register to the ADC
writeRegister(ads_i2cAddress, config);
// Wait for the conversion to complete
delay(ads_conversionDelay);
// Read the conversion results
uint16_t raw_adc = readRegister(ads_i2cAddress);
return (int16_t)raw_adc;
}