forked from T81/AD779X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAD779X.h
170 lines (147 loc) · 6.58 KB
/
AD779X.h
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
/*************************************************************************
* AD7799 libary
* by Christodoulos P. Lekkos <[email protected]> , September 03, 2014.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 3
* published by the Free Software Foundation.
*************************************************************************/
/*************************************************************************
* Mode register MSB Byte
**************************************************************************
* 7:5 * MD2-MD0 * Operation Mode
* 4 * PSW * Power Switch Control
* 3:0 * 0 * Always 0
*************************************************************************/
/*************************************************************************
* Mode register LSB Byte
**************************************************************************
* 7:4 * 0 * Always 0
* 3:0 * FS3-FS0 * Filter Update Rate
*************************************************************************/
/*************************************************************************
* Configuration register MSB Byte
**************************************************************************
* 7:6 * 0 * Always 0
* 5 * BO * Burnout Current (only when buffer or amp is active)
* 4 * U/~B * Unipolar Bipolar
* 3 * 0 * Always 0
* 2:0 * G2-G0 * Gain Select 1,2,4,..,128
*************************************************************************/
/*************************************************************************
* Configuration register LSB Byte
**************************************************************************
* 7:6 * 0 * Always 0
* 5 * REF_DET * Reference detect function on/off
* 4 * BUF * Enable buffer, can disable only when gain equals 1 or 2
* 3 * 0 * Always 0
* 2:0 * CH2-CH0 * Channel Select 0,1,2
*************************************************************************/
/*************************************************************************
* Communication register Byte
**************************************************************************
* 7 * ~WEN * Always 0 to clock the data to Communication register
* 6 * R/~W * Specifies if the next operation is Read or Write
* 5:3 * RS2-RS0 * Register Selection Bits 0,1,...,7
* 2 * CREAD * Set for Continuous Read of the Data register
* 1:0 * CR1-CR0 * Always 0
*************************************************************************/
/*************************************************************************
* Status register Byte
**************************************************************************
* 7 * ~RDY * Monitor for end of conversion and calibration
* 6 * ERR * In case of overrange or underrange
* 5 * NOREF * Cleared to indicate that a valid reference is applied
* 4 * 0 * Always 0
* 3 * 0/1 * 0 indicates an AD7798, 1 indicates an AD7799
* 2:0 * CH2-CH0 * Indicates which channel is being converted by the ADC
*************************************************************************/
/*************************************************************************
* IO register Byte
**************************************************************************
* 7 * 0 * Always 0
* 6 * IOEN * 0 AiN3(+)/P1 and AIN3(-)/P2 pins as DO, 1 as AI
* 5 * IO2DAT * When IOEN is set, 0 set P2 LOW, 1 HIGH
* 4 * IO1DAT * When IOEN is set, 0 set P1 LOW, 1 HIGH
* 3:0 * 0 * Always 0
*************************************************************************/
#ifndef AD779X_H
#define AD779X_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "SPI.h"
// Communication Register
#define READ_REG 0x40
#define WRITE_REG 0x00
#define ENTER_CREAD 0x5C
#define EXIT_CREAD 0x58
// Register Selection (0x00 : 0x07) << 3
#define COMM_REG 0x00
#define STATUS_REG 0x00
#define MODE_REG 0x08
#define CONFIG_REG 0x10
#define DATA_REG 0x18
#define ID_REG 0x20
#define IO_REG 0x28
#define OFFSET_REG 0x30
#define FULL_SCALE_REG 0x38
// Operating Modes (0x00 : 0x07) << 5
#define CONT_CONV_MODE 0x00
#define SNGL_CONV_MODE 0x20
#define IDLE_MODE 0x40
#define PWR_DWN_MODE 0x60
#define INT_ZERO_SCALE_CAL 0x80
#define INT_FULL_SCALE_CAL 0xA0
#define SYS_ZERO_SCALE_CAL 0xC0
#define SYS_FULL_SCALE_CAL 0xE0
// Register Masks
#define OPERATING_MODE_MASK 0x1F
#define CHANNEL_MASK 0xF8
// Useful declarations
#define RESET_ADC 0xFF
#define STUFFIN 0x00
// adcFlags
#define CLEAR 0x00
#define SET 0x01
#define ADC_MODEL 0x00
#define FIRST_MEASUREMENT 0x01
#define CALIBRATE 0x02
#define CREAD 0x03
#define DEBUG_ADC 0 // set to 1 for debugging
class AD779X
{
public:
unsigned char adcFail;
AD779X(float vRef);
void Begin(int inPin, int outPin, int clkPin, int csPin);
void Setup(unsigned char numberOfChannels = 3, unsigned char firstChannel = 0, unsigned char secondChannel = 1, unsigned char thirdChannel = 2);
void Config(unsigned char gain = 0x07, unsigned char coding = 0x01, unsigned char updateRate = 0x09, unsigned char buffer = 0x01, unsigned char refDet = 0x00, unsigned char burnoutCurrent = 0x00, unsigned char powerSwitch = 0x00);
void cRead(unsigned char channel, unsigned char enter);
void readID();
bool Update();
unsigned char StatusReg();
unsigned long readRaw(unsigned char channel);
float readmV(unsigned char channel);
private:
bool _adcPresent;
unsigned long _previousMillis, _settleTime, _offsetReg[3], _fullScaleReg[3], _dataRaw[3];
unsigned char _csPin, _inPin, _outPin, _clkPin, _nBytes, _adcChannels, _numberOfChannels, _channelIndex, _modeRegFByte, _modeRegSByte, _configRegFByte,_configRegSByte, _adcFlags, _channelArray[3];
float _vRef, _gain, _datamV[3];
long _interval;
void Init();
void adcReset();
void adcResetVars();
void adcWrite(unsigned char registerSelection, unsigned char val);
void adcCalibrate(unsigned char mode);
void adcFlag(unsigned char bit, unsigned char flag);
// void adcCheck();
void startConversion(unsigned char channel);
bool adcFlag(unsigned char flag);
unsigned char adcCommRegByte(unsigned char registerAddressBits, unsigned char operation);
unsigned long adcRead(unsigned char registerSelection);
uint8_t softwareSPI(int csPin, int inPin, int outPin, int clkPin, uint8_t shOut);
};
#endif