-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCP3428.cpp
More file actions
161 lines (135 loc) · 4.34 KB
/
Copy pathMCP3428.cpp
File metadata and controls
161 lines (135 loc) · 4.34 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
#include "MCP3428.h"
/**************************************************************************/
/*
Instantiates a new MCP3428 class with appropriate properties
*/
/***************************************************************************/
MCP3428::MCP3428(uint8_t devAddress)
{
Wire.begin();
devAddr = (uint8_t)(1101<<3);
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
The resolution makes the ouptut in 12/14/16-bit
LSB = (2 * VREF) / 2^N = (2 * 2.048 V) / 2^N
Where:
N = Resolution, which is programmed in the Configuration Register: 12, 14, or 16
*/
/**************************************************************************/
long MCP3428::readADC()
{
raw_adc = 0;
while(CheckConversion() == 1);
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 * LSB(1 mV)/PGA for PGA = 1;
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 * LSB(250 µV)/PGA for PGA = 1;
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 * LSB(62.5 µV)/PGA for PGA = 1;
break;
}
return raw_adc;
}