-
Notifications
You must be signed in to change notification settings - Fork 7
/
ADS1219.cpp
183 lines (162 loc) · 3.37 KB
/
ADS1219.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
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "ADS1219.h"
static uint8_t i2cRead(void) {
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
static void i2cWrite(uint8_t x) {
#if ARDUINO >= 100
Wire.write(x);
#else
Wire.send(x);
#endif
}
ADS1219::ADS1219(int drdy, uint8_t addr) {
data_ready = drdy;
address = addr;
config = 0x00;
singleShot = true;
}
void ADS1219::begin() {
Wire.begin();
}
void ADS1219::start(){
Wire.beginTransmission(address);
i2cWrite(0x08);
Wire.endTransmission();
}
void ADS1219::powerDown(){
Wire.beginTransmission(address);
i2cWrite(0x02);
Wire.endTransmission();
}
uint8_t ADS1219::readRegister(uint8_t reg){
Wire.beginTransmission(address);
i2cWrite(reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)address,(uint8_t)1);
return i2cRead();
}
void ADS1219::writeRegister(uint8_t data){
Wire.beginTransmission(address);
i2cWrite(CONFIG_REGISTER_ADDRESS);
i2cWrite(data);
Wire.endTransmission();
}
long ADS1219::readConversionResult(){
Wire.beginTransmission(address);
i2cWrite(0x10);
Wire.endTransmission();
Wire.requestFrom((uint8_t)address,(uint8_t)3);
long data32 = i2cRead();
data32 <<= 8;
data32 |= i2cRead();
data32 <<= 8;
data32 |= i2cRead();
return (data32 << 8) >> 8;
}
void ADS1219::resetConfig(){
writeRegister(0x00);
}
long ADS1219::readSingleEnded(int channel){
config &= MUX_MASK;
switch (channel){
case (0):
config |= MUX_SINGLE_0;
break;
case (1):
config |= MUX_SINGLE_1;
break;
case (2):
config |= MUX_SINGLE_2;
break;
case (3):
config |= MUX_SINGLE_3;
break;
default:
break;
}
writeRegister(config);
start();
while(digitalRead(data_ready)==1);
return readConversionResult();
}
long ADS1219::readDifferential_0_1(){
config &= MUX_MASK;
config |= MUX_DIFF_0_1;
writeRegister(config);
start();
while(digitalRead(data_ready)==1);
return readConversionResult();
}
long ADS1219::readDifferential_2_3(){
config &= MUX_MASK;
config |= MUX_DIFF_2_3;
writeRegister(config);
start();
while(digitalRead(data_ready)==1);
return readConversionResult();
}
long ADS1219::readDifferential_1_2(){
config &= MUX_MASK;
config |= MUX_DIFF_1_2;
writeRegister(config);
start();
while(digitalRead(data_ready)==1);
return readConversionResult();
}
long ADS1219::readShorted(){
config &= MUX_MASK;
config |= MUX_SHORTED;
writeRegister(config);
while(digitalRead(data_ready)==1);
return readConversionResult();
}
void ADS1219::setGain(adsGain_t gain){
config &= GAIN_MASK;
config |= gain;
writeRegister(config);
}
void ADS1219::setDataRate(int rate){
config &= DATA_RATE_MASK;
switch (rate){
case (20):
config |= DATA_RATE_20;
break;
case (90):
config |= DATA_RATE_90;
break;
case (330):
config |= DATA_RATE_330;
break;
case (1000):
config |= DATA_RATE_1000;
break;
default:
break;
}
writeRegister(config);
}
void ADS1219::setConversionMode(adsMode_t mode){
config &= MODE_MASK;
config |= mode;
writeRegister(config);
if (mode == CONTINUOUS){
singleShot = false;
} else {
singleShot = true;
}
}
void ADS1219::setVoltageReference(adsRef_t vref){
config &= VREF_MASK;
config |= vref;
writeRegister(config);
}