-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMCP3428.cpp
More file actions
executable file
·192 lines (164 loc) · 5.21 KB
/
MCP3428.cpp
File metadata and controls
executable file
·192 lines (164 loc) · 5.21 KB
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
/****************************************************************************
Distributed with a free-will license.
Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
MCP3428
This code is designed to work with the MCP3428_I2CADC I2C Mini Module available from ControlEverything.com.
https://www.controleverything.com/content/Analog-Digital-Converters?sku=MCP3428_I2CADC#tabs-0-product_tabset-2
****************************************************************************/
#include <Wire.h>
#include <MCP3428.h>
/**************************************************************************/
/*
Instantiates a new MCP3428 class with appropriate properties
*/
/***************************************************************************/
MCP3428::MCP3428(uint8_t devAddress)
{
Wire.begin();
devAddr = 0x68;
devAddr |= devAddress;
}
MCP3428::~MCP3428()
{
}
/***************************************************************************/
/*
Verify the I2C connection and Sets up the Hardware
*/
/***************************************************************************/
bool MCP3428::testConnection()
{
Wire.beginTransmission(devAddr);
return (Wire.endTransmission() == 0);
}
/**************************************************************************/
/*
Set the Configuration register with , Sample Rate, Conversion Mode and PGA Gain
channel: This determines an ADC reading from the specified channel
resolution: This determines the resolution (Sample Rate) for the device
mode: This determines the current operational status of the device
PGA: This configures the programmable gain amplifier
*/
/**************************************************************************/
void MCP3428::SetConfiguration(uint8_t channel, uint8_t resolution, bool mode, uint8_t PGA)
{
GAIN = PGA;
if(resolution!=12 && resolution!=14 && resolution!=16)
{
SPS = 12;
}
else
{
SPS = resolution;
}
MODE = mode;
config = 0;
config = config<<2;
// Setting the Channel
config |= (channel-1);
config = config<<1;
// Setting the Conversion Mode
config |= mode;
config = config<<2;
// Setting the Resolution (Sample Rate)
config |= int((SPS-12)/2);
config = config<<2;
// Setting the PGA Gain
config|=int(log(PGA)/log(2));
// Start a conversion using configuration settings
Wire.beginTransmission(devAddr);
// 128: This bit is the data ready flag
// One-Shot Conversion mode
// Initiate a new conversion
Wire.write((config |= 128));
Wire.endTransmission();
}
/**************************************************************************/
/*
Check the adc conversion
*/
/**************************************************************************/
bool MCP3428::CheckConversion()
{
uint8_t i = 0;
no_of_bytes = 3;
Wire.requestFrom(devAddr, no_of_bytes);
while(Wire.available())
{ data[i++] = Wire.read();
testvar = data[no_of_bytes-1] >> 7;
}
return testvar;
}
/**************************************************************************/
/*
Reads the conversion results, measuring the voltage
for an ADC reading from the specified channel
Generates a signed value since the difference can be either
positive or negative
The resolution makes the ouptut in 12/14/16-bit
*/
/**************************************************************************/
long MCP3428::readADC()
{
raw_adc = 0;
int tries = 0;
while(CheckConversion() == 1 && tries++ < 5) {
unsigned long convTime = getConversionTime(SPS);
if (convTime > 16383) {
convTime /= 1000;
delay(convTime);
} else {
delayMicroseconds(convTime);
}
}
switch (SPS)
{
case 12:
raw_adc = data[0];
raw_adc &= 0b00001111;
raw_adc = raw_adc << 8;
raw_adc |= data[1];
if(raw_adc > 2047)
{
raw_adc = raw_adc - 4096;
}
raw_adc = raw_adc*1000/GAIN;
break;
case 14:
raw_adc = data[0];
raw_adc &= 0b00111111;
raw_adc = raw_adc << 8;
raw_adc |= data[1];
if(raw_adc > 8191)
{
raw_adc = raw_adc - 16384;
}
raw_adc = raw_adc*250/GAIN;
break;
case 16:
raw_adc = data[0];
raw_adc = raw_adc << 8;
raw_adc |= data[1];
if(raw_adc > 32767)
{
raw_adc = raw_adc - 65536;
}
raw_adc = raw_adc*62.5/GAIN;
break;
}
return raw_adc;
}
unsigned long MCP3428::getConversionTime(uint8_t resolution) {
switch (resolution) {
case 12:
return 4167; // 240 SPS
case 14:
return 16667; // 60 SPS
case 16:
return 66667; // 15 SPS
case 18:
return 266667; // 3.75 SPS
default:
return 0;
}
}