i2c_adc_ads7828  v2.0.2
Arduino library for TI ADS7828 I2C A/D converter.
i2c_adc_ads7828.h
1 /*
4 
5  i2c_adc_ads7828.h - Arduino library for TI ADS7828 I2C A/D converter
6 
7  Library:: i2c_adc_ads7828
8  Author:: Doc Walker <4-20ma@wvfans.net>
9 
10  Copyright:: 2009-2016 Doc Walker
11 
12  Licensed under the Apache License, Version 2.0 (the "License");
13  you may not use this file except in compliance with the License.
14  You may obtain a copy of the License at
15 
16  http://www.apache.org/licenses/LICENSE-2.0
17 
18  Unless required by applicable law or agreed to in writing, software
19  distributed under the License is distributed on an "AS IS" BASIS,
20  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  See the License for the specific language governing permissions and
22  limitations under the License.
23 
24 */
25 
26 
71 
72 
73 #ifndef i2c_adc_ads7828_h
74 #define i2c_adc_ads7828_h
75 
76 // _________________________________________________________ STANDARD INCLUDES
77 // include types & constants of Wiring core API
78 #include "Arduino.h"
79 
80 
81 // __________________________________________________________ PROJECT INCLUDES
82 // include twi/i2c library
83 #include <Wire.h>
84 
85 
86 // ____________________________________________________________ UTILITY MACROS
87 
88 
89 // _________________________________________________________________ CONSTANTS
101 static const uint8_t DIFFERENTIAL = 0 << 7; // SD == 0
102 
103 
115 static const uint8_t SINGLE_ENDED = 1 << 7; // SD == 1
116 
117 
129 static const uint8_t REFERENCE_OFF = 0 << 3; // PD1 == 0
130 
131 
143 static const uint8_t REFERENCE_ON = 1 << 3; // PD1 == 1
144 
145 
157 static const uint8_t ADC_OFF = 0 << 2; // PD0 == 0
158 
159 
171 static const uint8_t ADC_ON = 1 << 2; // PD0 == 1
172 
173 
176 static const uint8_t DEFAULT_CHANNEL_MASK = 0xFF;
177 
178 
181 static const uint16_t DEFAULT_MIN_SCALE = 0;
182 
183 
186 static const uint16_t DEFAULT_MAX_SCALE = 0xFFF;
187 
188 
189 // _________________________________________________________ CLASS DEFINITIONS
190 class ADS7828;
192 {
193  public:
194  // ............................................... public member functions
195  ADS7828Channel() {};
196  ADS7828Channel(ADS7828* const, uint8_t, uint8_t, uint16_t, uint16_t);
197  uint8_t commandByte();
198  ADS7828* device();
199  uint8_t id();
200  uint8_t index();
201  void newSample(uint16_t);
202  void reset();
203  uint16_t sample();
204  uint8_t start();
205  uint16_t total();
206  uint8_t update();
207  uint16_t value();
208 
209  // ........................................ static public member functions
210 
211  // ..................................................... public attributes
222  uint16_t maxScale;
223 
234  uint16_t minScale;
235 
236  // .............................................. static public attributes
237 
238  private:
239  // .............................................. private member functions
240 
241  // ....................................... static private member functions
242 
243  // .................................................... private attributes
245  uint8_t commandByte_;
246 
249 
251  uint8_t index_;
252 
255  uint16_t samples_[1 << 4];
256 
258  uint16_t total_;
259 
260  // ............................................. static private attributes
264  static const uint8_t MOVING_AVERAGE_BITS_ = 4;
265 };
266 
267 
268 class ADS7828
269 {
270  public:
271  // ............................................... public member functions
272  ADS7828(uint8_t);
273  ADS7828(uint8_t, uint8_t);
274  ADS7828(uint8_t, uint8_t, uint8_t);
275  ADS7828(uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
276  uint8_t address();
277  ADS7828Channel* channel(uint8_t);
278  uint8_t commandByte();
279  uint8_t start();
280  uint8_t start(uint8_t);
281  uint8_t update(); // single device, all unmasked channel
282  uint8_t update(uint8_t); // single device, single channel
283 
284  // ........................................ static public member functions
285  static void begin();
286  static ADS7828* device(uint8_t);
287  static uint8_t updateAll(); // all devices, all unmasked channels
288 
289  // ..................................................... public attributes
292  uint8_t channelMask; // mask of active channels
293 
294  // .............................................. static public attributes
295 
296  private:
297  // .............................................. private member functions
298  void init(uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
299  uint16_t read();
300 
301  // ....................................... static private member functions
302  static uint16_t read(uint8_t);
303  static uint8_t start(uint8_t, uint8_t);
304  static uint8_t update(ADS7828*); // single device, all unmasked channels
305  static uint8_t update(ADS7828*, uint8_t); // single device, single channel
306 
307  // .................................................... private attributes
309  uint8_t address_;
310 
312  ADS7828Channel channels_[8];
313 
315  uint8_t commandByte_;
316 
317  // ............................................. static private attributes
319  static ADS7828* devices_[4];
320 
322  static const uint8_t BASE_ADDRESS_ = 0x48;
323 };
324 #endif
325 
uint16_t sample()
Return most-recent (unscaled) sample value from moving average array.
Definition: i2c_adc_ads7828.cpp:170
static const uint8_t REFERENCE_ON
Configure channels to turn internal reference ON between conversions (Command byte PD1=1)...
Definition: i2c_adc_ads7828.h:143
uint8_t commandByte()
Return command byte for channel object.
Definition: i2c_adc_ads7828.cpp:55
static const uint8_t MOVING_AVERAGE_BITS_
Quantity of samples to be averaged = 2MOVING_AVERAGE_BITS_.
Definition: i2c_adc_ads7828.h:264
static const uint8_t SINGLE_ENDED
Configure channels to use single-ended inputs (Command byte SD=1).
Definition: i2c_adc_ads7828.h:115
void reset()
Reset moving average array, index, totalizer to zero.
Definition: i2c_adc_ads7828.cpp:149
static const uint16_t DEFAULT_MAX_SCALE
Default scaling maximum value used in ADS7828 constructor.
Definition: i2c_adc_ads7828.h:186
static const uint16_t DEFAULT_MIN_SCALE
Default scaling minimum value used in ADS7828 constructor.
Definition: i2c_adc_ads7828.h:181
uint8_t address_
Device address as defined by pins A1, A0.
Definition: i2c_adc_ads7828.h:309
static const uint8_t DIFFERENTIAL
Configure channels to use differential inputs (Command byte SD=0).
Definition: i2c_adc_ads7828.h:101
ADS7828 * device()
Return pointer to parent device object.
Definition: i2c_adc_ads7828.cpp:71
static const uint8_t REFERENCE_OFF
Configure channels to turn internal reference OFF between conversions (Command byte PD1=0)...
Definition: i2c_adc_ads7828.h:129
static const uint8_t ADC_OFF
Configure channels to turn A/D converter OFF between conversions (Command byte PD0=0).
Definition: i2c_adc_ads7828.h:157
Definition: i2c_adc_ads7828.h:191
uint16_t total_
(Unscaled) running total of moving average array elements.
Definition: i2c_adc_ads7828.h:258
uint8_t update()
Initiate A/D conversion, read data, update moving average for channel object.
Definition: i2c_adc_ads7828.cpp:231
ADS7828 * device_
Pointer to parent device object.
Definition: i2c_adc_ads7828.h:248
uint8_t index()
Return index position within moving average array.
Definition: i2c_adc_ads7828.cpp:120
uint8_t commandByte_
Command byte for device object (PD1 PD0 bits only).
Definition: i2c_adc_ads7828.h:315
uint16_t samples_[1<< 4]
Array of (unscaled) sample values.
Definition: i2c_adc_ads7828.h:255
void newSample(uint16_t)
Add (unscaled) sample value to moving average array, update totalizer.
Definition: i2c_adc_ads7828.cpp:130
Definition: i2c_adc_ads7828.h:268
uint16_t minScale
Minimum value of moving average (defaults to 0x0000).
Definition: i2c_adc_ads7828.h:234
static const uint8_t DEFAULT_CHANNEL_MASK
Default channel mask used in ADS7828 constructor.
Definition: i2c_adc_ads7828.h:176
uint8_t index_
Index position within moving average array.
Definition: i2c_adc_ads7828.h:251
uint8_t id()
Return ID number of channel object (+IN connection).
Definition: i2c_adc_ads7828.cpp:102
uint8_t start()
Initiate A/D conversion for channel object.
Definition: i2c_adc_ads7828.cpp:192
uint16_t total()
Return (unscaled) totalizer value for channel object.
Definition: i2c_adc_ads7828.cpp:209
static const uint8_t ADC_ON
Configure channels to turn A/D converter ON between conversions (Command byte PD0=1).
Definition: i2c_adc_ads7828.h:171
uint8_t commandByte_
Command byte for channel object (SD C2 C1 C0 bits only).
Definition: i2c_adc_ads7828.h:245
uint8_t channelMask
Each bit position containing a 1 represents a channel that is to be read via update() / updateAll()...
Definition: i2c_adc_ads7828.h:292
uint16_t maxScale
Maximum value of moving average (defaults to 0x0FFF).
Definition: i2c_adc_ads7828.h:222
uint16_t value()
Return moving average value for channel object.
Definition: i2c_adc_ads7828.cpp:248