forked from chepo92/ADS1256
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADS1256.cpp
234 lines (200 loc) · 5.29 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
/*
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) {
pinMode(_CS, OUTPUT);
pinMode(_DRDY, INPUT);
if (useResetPin) {
pinMode(_RESET, OUTPUT);
digitalWrite(_RESET, 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(WREG | reg);
SPI.transfer(0);
SPI.transfer(wdata);
_delay4();
CSOFF();
}
unsigned char ADS1256::readRegister(unsigned char reg) {
unsigned char readValue;
CSON();
SPI.transfer(RREG | reg);
SPI.transfer(0);
_delay50();
readValue = SPI.transfer(0);
_delay4(); // t11 delay
CSOFF();
return readValue;
}
void ADS1256::sendCommand(unsigned char reg) {
CSON();
waitDRDY();
SPI.transfer(reg);
_delay4();
CSOFF();
}
void ADS1256::setConversionFactor(float val) { _conversionFactor = val; }
void ADS1256::readTest() {
unsigned char _highByte, _midByte, _lowByte;
CSON();
SPI.transfer(RDATA);
_delay50();
_highByte = SPI.transfer(WAKEUP);
_midByte = SPI.transfer(WAKEUP);
_lowByte = SPI.transfer(WAKEUP);
CSOFF();
}
float ADS1256::readCurrentChannel() {
CSON();
SPI.transfer(RDATA);
_delay50();
float adsCode = read_float32();
CSOFF();
return ((adsCode / 0x7FFFFF) * ((2 * _VREF) / (float)_pga)) *
_conversionFactor;
}
// Call this ONLY after RDATA command
unsigned long ADS1256::read_uint24() {
unsigned char _highByte, _midByte, _lowByte;
unsigned long value;
_highByte = SPI.transfer(WAKEUP);
_midByte = SPI.transfer(WAKEUP);
_lowByte = SPI.transfer(WAKEUP);
// 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 RDATA command
long ADS1256::read_int32() {
long value = read_uint24();
if (value & 0x00800000) {
value |= 0xff000000;
}
return value;
}
// Call this ONLY after RDATA command
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(MUX, MUX_CHANNEL);
sendCommand(SYNC);
sendCommand(WAKEUP);
CSOFF();
}
void ADS1256::begin(unsigned char drate, unsigned char gain, bool buffenable) {
_pga = 1 << gain;
sendCommand(
SDATAC); // send out SDATAC command to stop continous reading mode.
writeRegister(DRATE, drate); // write data rate register
uint8_t bytemask = B00000111;
uint8_t adcon = readRegister(ADCON);
uint8_t byte2send = (adcon & ~bytemask) | gain;
writeRegister(ADCON, byte2send);
if (buffenable) {
uint8_t status = readRegister(ADSSTATUS);
bitSet(status, 1);
writeRegister(ADSSTATUS, status);
}
sendCommand(SELFCAL); // perform self calibration
waitDRDY();
; // wait ADS1256 to settle after self calibration
}
void ADS1256::CSON() { digitalWrite(_CS, LOW); }
void ADS1256::CSOFF() { digitalWrite(_CS, HIGH); }
void ADS1256::_delay4() {
DELAY1; DELAY1; DELAY1; DELAY1;
}
void ADS1256::_delay50() {
DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1;
DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1;
DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1;
DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1;
DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1; DELAY1;
}
void ADS1256::waitDRDY() {
while (digitalRead(_DRDY))
;
}