forked from chepo92/ADS1256
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ADS1256.cpp
271 lines (232 loc) · 7.03 KB
/
ADS1256.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
/*
ADS1256.h - Arduino Library for communication with Texas Instrument ADS1256 ADC
Written by Adien Akhmad, August 2015
Modfified Jan 2019 by Axel Sepulveda for ATMEGA328
*/
#include "ADS1256.h"
#include "Arduino.h"
#include "SPI.h"
ADS1256::ADS1256(float clockspdMhz, float vref, bool useResetPin) {
// Set DRDY as input
pinMode(pinDRDY, INPUT);
// Set CS as output
pinMode(pinCS, OUTPUT);
if (useResetPin) {
// set RESETPIN as output
pinMode(pinRST, OUTPUT );
// pull RESETPIN high
pinMode(pinRST, HIGH);
}
// Voltage Reference
_VREF = vref;
// Default conversion factor
_conversionFactor = 1.0;
// Start SPI on a quarter of ADC clock speed
SPI.begin();
SPI.beginTransaction(
SPISettings(clockspdMhz * 1000000 / 4, MSBFIRST, SPI_MODE1));
}
void ADS1256::writeRegister(unsigned char reg, unsigned char wdata) {
CSON();
SPI.transfer(ADS1256_CMD_WREG | reg); // opcode1 Write registers starting from reg
SPI.transfer(0); // opcode2 Write 1+0 registers
SPI.transfer(wdata); // write wdata
delayMicroseconds(1);
CSOFF();
}
unsigned char ADS1256::readRegister(unsigned char reg) {
unsigned char readValue;
CSON();
SPI.transfer(ADS1256_CMD_RREG | reg); // opcode1 read registers starting from reg
SPI.transfer(0); // opcode2 read 1+0 registers
delayMicroseconds(7); // t6 delay (4*tCLKIN 50*0.13 = 6.5 us)
readValue = SPI.transfer(0); // read registers
delayMicroseconds(1); // t11 delay (4*tCLKIN 4*0.13 = 0.52 us)
CSOFF();
return readValue;
}
void ADS1256::sendCommand(unsigned char reg) {
CSON();
waitDRDY();
SPI.transfer(reg);
delayMicroseconds(1); // t11 delay (4*tCLKIN 4*0.13 = 0.52 us)
CSOFF();
}
void ADS1256::setConversionFactor(float val) { _conversionFactor = val; }
void ADS1256::readTest() {
unsigned char _highByte, _midByte, _lowByte;
CSON();
SPI.transfer(ADS1256_CMD_RDATA);
delayMicroseconds(7); // t6 delay (4*tCLKIN 50*0.13 = 6.5 us)
_highByte = SPI.transfer(ADS1256_CMD_WAKEUP);
_midByte = SPI.transfer(ADS1256_CMD_WAKEUP);
_lowByte = SPI.transfer(ADS1256_CMD_WAKEUP);
CSOFF();
}
float ADS1256::readCurrentChannel() {
CSON();
SPI.transfer(ADS1256_CMD_RDATA);
delayMicroseconds(7); // t6 delay (4*tCLKIN 50*0.13 = 6.5 us)
float adsCode = read_float32();
CSOFF();
return ((adsCode / 0x7FFFFF) * ((2 * _VREF) / (float)_pga)) *
_conversionFactor;
}
// Reads raw ADC data, as 32bit int
long ADS1256::readCurrentChannelRaw() {
CSON();
SPI.transfer(ADS1256_CMD_RDATA);
delayMicroseconds(7); // t6 delay (4*tCLKIN 50*0.13 = 6.5 us)
long adsCode = read_int32();
CSOFF();
return adsCode;
}
// Call this ONLY after ADS1256_CMD_RDATA command
unsigned long ADS1256::read_uint24() {
unsigned char _highByte, _midByte, _lowByte;
unsigned long value;
_highByte = SPI.transfer(0);
_midByte = SPI.transfer(0);
_lowByte = SPI.transfer(0);
// Combine all 3-bytes to 24-bit data using byte shifting.
value = ((long)_highByte << 16) + ((long)_midByte << 8) + ((long)_lowByte);
return value;
}
// Call this ONLY after ADS1256_CMD_RDATA command
// Convert the signed 24bit stored in an unsigned 32bit to a signed 32bit
long ADS1256::read_int32() {
long value = read_uint24();
if (value & 0x00800000) { // if the 24 bit value is negative reflect it to 32bit
value |= 0xff000000;
}
return value;
}
// Call this ONLY after ADS1256_CMD_RDATA command
// Cast as a float
float ADS1256::read_float32() {
long value = read_int32();
return (float)value;
}
// Channel switching for single ended mode. Negative input channel are
// automatically set to AINCOM
void ADS1256::setChannel(byte channel) { setChannel(channel, -1); }
// Channel Switching for differential mode. Use -1 to set input channel to
// AINCOM
void ADS1256::setChannel(byte AIN_P, byte AIN_N) {
unsigned char MUX_CHANNEL;
unsigned char MUXP;
unsigned char MUXN;
switch (AIN_P) {
case 0:
MUXP = ADS1256_MUXP_AIN0;
break;
case 1:
MUXP = ADS1256_MUXP_AIN1;
break;
case 2:
MUXP = ADS1256_MUXP_AIN2;
break;
case 3:
MUXP = ADS1256_MUXP_AIN3;
break;
case 4:
MUXP = ADS1256_MUXP_AIN4;
break;
case 5:
MUXP = ADS1256_MUXP_AIN5;
break;
case 6:
MUXP = ADS1256_MUXP_AIN6;
break;
case 7:
MUXP = ADS1256_MUXP_AIN7;
break;
default:
MUXP = ADS1256_MUXP_AINCOM;
}
switch (AIN_N) {
case 0:
MUXN = ADS1256_MUXN_AIN0;
break;
case 1:
MUXN = ADS1256_MUXN_AIN1;
break;
case 2:
MUXN = ADS1256_MUXN_AIN2;
break;
case 3:
MUXN = ADS1256_MUXN_AIN3;
break;
case 4:
MUXN = ADS1256_MUXN_AIN4;
break;
case 5:
MUXN = ADS1256_MUXN_AIN5;
break;
case 6:
MUXN = ADS1256_MUXN_AIN6;
break;
case 7:
MUXN = ADS1256_MUXN_AIN7;
break;
default:
MUXN = ADS1256_MUXN_AINCOM;
}
MUX_CHANNEL = MUXP | MUXN;
CSON();
writeRegister(ADS1256_RADD_MUX, MUX_CHANNEL);
sendCommand(ADS1256_CMD_SYNC);
sendCommand(ADS1256_CMD_WAKEUP);
CSOFF();
}
/*
Init chip with set datarate and gain and perform self calibration
*/
void ADS1256::begin(unsigned char drate, unsigned char gain, bool buffenable) {
_pga = 1 << gain;
sendCommand(ADS1256_CMD_SDATAC); // send out ADS1256_CMD_SDATAC command to stop continous reading mode.
writeRegister(ADS1256_RADD_DRATE, drate); // write data rate register
uint8_t bytemask = B00000111;
uint8_t adcon = readRegister(ADS1256_RADD_ADCON);
uint8_t byte2send = (adcon & ~bytemask) | gain;
writeRegister(ADS1256_RADD_ADCON, byte2send);
if (buffenable) {
uint8_t status = readRegister(ADS1256_RADD_STATUS);
bitSet(status, 1);
writeRegister(ADS1256_RADD_STATUS, status);
}
sendCommand(ADS1256_CMD_SELFCAL); // perform self calibration
waitDRDY();
; // wait ADS1256 to settle after self calibration
}
/*
Init chip with default datarate and gain and perform self calibration
*/
void ADS1256::begin() {
sendCommand(ADS1256_CMD_SDATAC); // send out ADS1256_CMD_SDATAC command to stop continous reading mode.
uint8_t status = readRegister(ADS1256_RADD_STATUS);
sendCommand(ADS1256_CMD_SELFCAL); // perform self calibration
waitDRDY(); // wait ADS1256 to settle after self calibration
}
/*
Reads and returns STATUS register
*/
uint8_t ADS1256::getStatus() {
sendCommand(ADS1256_CMD_SDATAC); // send out ADS1256_CMD_SDATAC command to stop continous reading mode.
return readRegister(ADS1256_RADD_STATUS);
}
void ADS1256::CSON() {
//PORT_CS &= ~(1 << PINDEX_CS);
digitalWrite(pinCS, LOW);
} // digitalWrite(_CS, LOW); }
void ADS1256::CSOFF() {
digitalWrite(pinCS, HIGH);
//PORT_CS |= (1 << PINDEX_CS);
} // digitalWrite(_CS, HIGH); }
void ADS1256::waitDRDY() {
//while (PIN_DRDY & (1 << PINDEX_DRDY));
while (digitalRead(pinDRDY));
}
boolean ADS1256::isDRDY() {
return !digitalRead(pinDRDY);
}