Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
ADS1115.h
Go to the documentation of this file.
1
5#ifndef ADS1115_H
6#define ADS1115_H
7
8#include "sys/i2c.h"
9
24private:
25 enum {
26 CONVERSION,
27 CONFIG,
28 LO_THRESH,
29 HI_THRESH
30 };
31
32 const double uV[6] = {
33 187.5,
34 125,
35 62.5,
36 31.25,
37 15.625,
38 7.8125
39 };
40
41public:
43 enum range {
44 FS6144,
45 FS4096,
46 FS2048,
47 FS1024,
48 FS0512,
49 FS0256
50 } eFullScale = FS2048;
51
53 enum dataRate {
54 SPS8,
55 SPS16,
56 SPS32,
57 SPS64,
58 SPS128,
59 SPS250,
60 SPS475,
61 SPS860
62 } eDataRate = SPS128;
63
72 enum mux {
73 AIN0_1,
74 AIN0_3,
75 AIN1_3,
76 AIN2_3,
77 AIN0,
78 AIN1,
79 AIN2,
80 AIN3
81 } eMux = AIN0_1;
82
84 // single conversion mode
85 bool single = false;
86 bool cMod = false;
87 bool cPol = false;
88 bool cLat = false;
89 uint8_t cQue = 0;
91
101 uint8_t address = 0b1001000, enum range fs = FS6144,
102 enum dataRate dr = SPS64, enum mux mx = AIN0):
103 I2C::Device(bus, address), eFullScale(fs),
104 eDataRate(dr), eMux(mx) {
105 updateConfig();
106 }
107
114 write(CONFIG, 0 << 15 |
115 eMux << 12 |
116 eFullScale << 9 |
117 (single & 1) << 8 |
118 eDataRate << 5 |
119 (cMod & 1) << 4 |
120 (cPol & 1) << 3 |
121 (cLat & 1) << 2 |
122 cQue
123 );
124 setPointer(CONVERSION);
125 }
126
130 void measure() {
131 read();
132 }
133
137 double volts() {
138 return adc;
139 }
140
141private:
142 void callback(const I2C::Request &rq) override {
143 if (rq.data.size != 2) return;
144 adc = (int16_t)(rq.data[0]<<8 | rq.data[1]) * uV[eFullScale] * 1e-6;
145 }
146 double adc{0};
147 void setPointer(uint8_t reg) {
148 bus.trypush({
149 .dev = this,
150 .data = {reg},
151 });
152 }
153
154 void read() {
155 bus.trypush({
156 .dev = this,
157 .data = 2,
158 .opts = {
159 .read = true,
160 },
161 });
162 }
163
164 void write(uint8_t reg, uint16_t val) {
165 bus.trypush({
166 .dev = this,
167 .data = {reg, (uint8_t)(val>>8), (uint8_t)val},
168 });
169 }
170};
171
172#endif //ADS1115_H
Implementation of the ADS1115 analog to digital converter.
Definition ADS1115.h:23
dataRate
output data rate (samples per second) options
Definition ADS1115.h:53
void measure()
async read data from device
Definition ADS1115.h:130
enum ADS1115::range eFullScale
current full scale setting
range
full scale range options
Definition ADS1115.h:43
void updateConfig()
update device config
Definition ADS1115.h:113
mux
multiplexer setting options
Definition ADS1115.h:72
enum ADS1115::mux eMux
current multiplexer setting
ADS1115(Sink< I2C::Request > &bus, uint8_t address=0b1001000, enum range fs=FS6144, enum dataRate dr=SPS64, enum mux mx=AIN0)
configure and start voltage conversions
Definition ADS1115.h:100
double volts()
Definition ADS1115.h:137
enum ADS1115::dataRate eDataRate
current data rate
Copyright (c) 2023 IACE.
size_t size
total capacity of buffer
Definition buffer.h:23
generic I2C device wrapper.
Definition i2c.h:21
const uint8_t address
unshifted 7bit I2C device address
Definition i2c.h:24
Device(Sink< Request > &bus, uint8_t address)
construct device on bus with given address
Definition i2c.h:26
struct defining an I2C request.
Definition i2c.h:37
Buffer< uint8_t > data
transfer data
Definition i2c.h:39
generic object sink, i.e.
Definition streams.h:15