forked from nadavmatalon/MCP3221
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCP3221.cpp
284 lines (225 loc) · 12.8 KB
/
MCP3221.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*==============================================================================================================*
@file MCP3221.cpp
@author Nadav Matalon
@license MIT (c) 2016 Nadav Matalon
MCP3221 Driver (12-BIT Single Channel ADC with I2C Interface)
Ver. 1.0.0 - First release (16.10.16)
*==============================================================================================================*
LICENSE
*==============================================================================================================*
The MIT License (MIT)
Copyright (c) 2016 Nadav Matalon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*==============================================================================================================*/
#include "MCP3221.h"
/*==============================================================================================================*
CONSTRUCTOR
*==============================================================================================================*/
MCP3221::MCP3221(
byte devAddr,
unsigned int vRef,
unsigned int res1,
unsigned int res2,
unsigned int alpha,
voltage_input_t voltageInput,
smoothing_t smoothingMethod,
byte numSamples) :
_devAddr(devAddr),
_vRef(vRef),
_alpha(alpha),
_voltageInput(voltageInput),
_smoothing(smoothingMethod),
_numSamples(numSamples)
{
memset(_samples, 0, sizeof(_samples));
if (((res1 != 0) && (res2 != 0)) && (_voltageInput == VOLTAGE_INPUT_12V)) {
_res1 = res1;
_res2 = res2;
} else if (_voltageInput == VOLTAGE_INPUT_5V) {
_res1 = 0;
_res2 = 0;
} else {
_res1 = DEFAULT_RES_1;
_res2 = DEFAULT_RES_2;
}
_comBuffer = COM_SUCCESS;
}
/*==============================================================================================================*
DESTRUCTOR
*==============================================================================================================*/
MCP3221::~MCP3221() {}
/*==============================================================================================================*
PING (0 = SUCCESS / 1, 2, ... = ERROR CODE)
*==============================================================================================================*/
// See meaning of I2C Error Code values in README
byte MCP3221::ping() {
Wire.beginTransmission(_devAddr);
return _comBuffer = Wire.endTransmission();
}
/*==============================================================================================================*
GET VOLTAGE REFERENCE (2700mV - 5500mV)
*==============================================================================================================*/
unsigned int MCP3221::getVref() {
return _vRef;
}
/*==============================================================================================================*
GET VOLTAGE DIVIDER RESISTOR 1 (Ω)
*==============================================================================================================*/
unsigned int MCP3221::getRes1() {
return _res1;
}
/*==============================================================================================================*
GET VOLTAGE DIVIDER RESISTOR 2 (Ω)
*==============================================================================================================*/
unsigned int MCP3221::getRes2() {
return _res2;
}
/*==============================================================================================================*
GET ALPHA (RELEVANT ONLY FOR EMAVG SMOOTHING METHOD, RANGE: 1 - 256)
*==============================================================================================================*/
unsigned int MCP3221::getAlpha() {
return _alpha;
}
/*==============================================================================================================*
GET NUMBER OF SAMPLES (RELEVANT ONLY FOR ROLLING-AVAREGE SMOOTHING METHOD, RANGE: 1-20 SAMPLES)
*==============================================================================================================*/
byte MCP3221::getNumSamples() {
return _numSamples;
}
/*==============================================================================================================*
GET VOLTAGE INPUT (0 = VOLTAGE_INPUT_5V / 1 = VOLTAGE_INPUT_12V)
*==============================================================================================================*/
byte MCP3221::getVinput() {
return _voltageInput;
}
/*==============================================================================================================*
GET SMOOTHING METHOD (0 = NONE / 1 = ROLLING-AVAREGE / 2 = EMAVG)
*==============================================================================================================*/
byte MCP3221::getSmoothing() {
return _smoothing;
}
/*==============================================================================================================*
GET DATA
*==============================================================================================================*/
unsigned int MCP3221::getData() {
return (_smoothing == NO_SMOOTHING) ? getRawData() : smoothData(getRawData());
}
/*==============================================================================================================*
GET VOLTAGE (Vref 4.096V: 2700 - 4096mV)
*==============================================================================================================*/
unsigned int MCP3221::getVoltage() {
if (_voltageInput == VOLTAGE_INPUT_5V) return round((_vRef / (float)DEFAULT_VREF) * getData());
else return round(getData() * ((float)(_res1 + _res2) / _res2));
}
/*==============================================================================================================*
GET LATEST I2C COMMUNICATION RESULT (0 = OK / 1, 2, ... = ERROR)
*==============================================================================================================*/
byte MCP3221::getComResult() {
return _comBuffer;
}
/*==============================================================================================================*
SET REFERENCE VOLTAGE (2700mV - 5500mV)
*==============================================================================================================*/
void MCP3221::setVref(unsigned int newVref) { // PARAM RANGE: 2700-5500
newVref = constrain(newVref, MIN_VREF, MAX_VREF);
_vRef = newVref;
}
/*==============================================================================================================*
SET VOLTAGE DIVIDER RESISTOR 1 (Ω)
*==============================================================================================================*/
void MCP3221::setRes1(unsigned int newRes1) {
_res1 = newRes1;
}
/*==============================================================================================================*
SET VOLTAGE DIVIDER RESISTOR 2 (Ω)
*==============================================================================================================*/
void MCP3221::setRes2(unsigned int newRes2) {
_res2 = newRes2;
}
/*==============================================================================================================*
SET ALPHA (RELEVANT ONLY FOR EMAVG SMOOTHING METHOD)
*==============================================================================================================*/
void MCP3221::setAlpha(unsigned int newAlpha) { // PARAM RANGE: 1-256
newAlpha = constrain(newAlpha, MIN_ALPHA, MAX_ALPHA);
_alpha = newAlpha;
}
/*==============================================================================================================*
SET NUMBER OF SAMPLES (RELEVANT ONLY FOR ROLLING-AVAREGE SMOOTHING METHOD)
*==============================================================================================================*/
void MCP3221::setNumSamples(byte newNumSamples) { // PARAM RANGE: 1-20
newNumSamples = constrain(newNumSamples, MIN_NUM_SAMPLES, MAX_NUM_SAMPLES);
_numSamples = newNumSamples;
for (byte i=0; i<MAX_NUM_SAMPLES; i++) _samples[i] = 0;
}
/*==============================================================================================================*
SET VOLTAGE INPUT (NOTE: 12V INPUT READINGS REQUIRE A HARDWARE VOLTAGE DIVIDER)
*==============================================================================================================*/
void MCP3221::setVinput(voltage_input_t newVoltageInput) { // PARAMS: VOLTAGE_INPUT_5V / VOLTAGE_INPUT_12V
_voltageInput = newVoltageInput;
if (newVoltageInput == VOLTAGE_INPUT_12V) {
if (!_res1) _res1 = DEFAULT_RES_1;
if (!_res2) _res2 = DEFAULT_RES_2;
}
}
/*==============================================================================================================*
SET SMOOTHING METHOD
*==============================================================================================================*/
void MCP3221::setSmoothing(smoothing_t newSmoothing) { // PARAMS: NO_SMOOTHING / ROLLING / EMAVG
_smoothing = newSmoothing;
}
/*==============================================================================================================*
RESET
*==============================================================================================================*/
void MCP3221::reset() {
setVref(DEFAULT_VREF);
setAlpha(DEFAULT_ALPHA);
setVinput(VOLTAGE_INPUT_5V);
setSmoothing(EMAVG);
setRes1(0);
setRes2(0);
setNumSamples(DEFAULT_NUM_SAMPLES);
}
/*==============================================================================================================*
GET RAW DATA
*==============================================================================================================*/
unsigned int MCP3221::getRawData() {
unsigned int rawData = 0;
Wire.requestFrom(_devAddr, DATA_BYTES);
if (Wire.available() == DATA_BYTES) rawData = (Wire.read() << 8) | (Wire.read());
else ping();
return rawData;
}
/*==============================================================================================================*
SMOOTH DATA
*==============================================================================================================*/
unsigned int MCP3221::smoothData(unsigned int rawData) {
unsigned int smoothedData;
if (_smoothing == EMAVG) { // Exmponential Moving Average
unsigned int emAvg = rawData;
emAvg = (_alpha * (unsigned long)rawData + (MAX_ALPHA - _alpha) * (unsigned long)emAvg) / MAX_ALPHA;
smoothedData = emAvg;
} else { // Rolling-Average
unsigned long sum = 0;
if (_samples[_numSamples - 1] != 0) {
for (byte i = 1; i<_numSamples; i++) _samples[i - 1] = _samples[i]; // drop last reading & rearrange array
_samples[_numSamples - 1] = rawData; // add a new sample at the end of array
for (byte j=0; j<_numSamples; j++) sum += _samples[j]; // aggregate all samples
smoothedData = sum / _numSamples; // calculate average
} else {
for (byte i = 0; i<_numSamples; i++) _samples[i] = rawData;
smoothedData = rawData;
}
}
return smoothedData;
}