-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMCP3428.java
More file actions
174 lines (121 loc) · 4.31 KB
/
MCP3428.java
File metadata and controls
174 lines (121 loc) · 4.31 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
/*
Author: Yadwinder Singh
4-Channel ADC with 16-Bit Resolution
4 Differential Analog Inputs
Programmable x1-x8 Gain Amplifier
Up to 8 Devices per I2C Port
Up to 3.4MHz Communication Speed
0x68 Start Address
Hardware version - Rev A.
Platform : Raspberry pi
Project uses pi4j library.
Please follow a detailed tutorial to install pi4j here.
http://pi4j.com/install.html
Compile the java program with command ~\[Directory of code]\ pi4j Filename.java
Run it with ~\[Directory of code]\ pi4j Filename
*/
import java.nio.channels.WritableByteChannel;
import java.nio.ByteBuffer;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class MCP3428
{
public final static int MCP3428_ADDRESS = 0x68;
public final static byte L = 0;
public final static byte H = 1;
public final static byte F = 2;
public final static int CHANNEL_0 = 0;
public final static int CHANNEL_1 = 1;
public final static int CHANNEL_2 = 2;
public final static int CHANNEL_3 = 3;
public final static int GAIN_1 = 0;
public final static int GAIN_2 = 1;
public final static int GAIN_4 = 2;
public final static int GAIN_8 = 3;
double VRef = 2.048; // Reference Voltage
//communication register
public final static int BIT_RDY = 7; //data ready
public final static int BIT_C1 = 6; //channel select
public final static int BIT_C0 = 5; //channel select
public final static int BIT_OC = 4; //conversion mode (one shot/continuous)
public final static int BIT_S1 = 3; //sample rate
public final static int BIT_S0 = 2; //sample rate
public final static int BIT_G1 = 1; //gain
public final static int BIT_G0 = 0; //gain
byte[] b = new byte[3];
int status = 0;
private static boolean verbose = "true".equals(System.getProperty("mcp3428.verbose", "false"));
private I2CBus bus;
private I2CDevice mcp3428;
public MCP3428()
{
this(MCP3428_ADDRESS);
}
public MCP3428(int address)
{
try
{
// Get i2c bus
bus = I2CFactory.getInstance(I2CBus.BUS_1); // Depends onthe RasPI version
if (verbose)
System.out.println("Connected to bus. OK.");
// Get device itself
mcp3428 = bus.getDevice(address);
if (verbose)
System.out.println("Connected to device. OK.");
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
}
public void setConfig(int channel, int gain) // Select Channel and Gain
{
//configuration register, assuming 16 bit resolution
byte reg = (byte)((1 << BIT_RDY) | (channel << BIT_C0) | (1 << BIT_OC) | (1 << BIT_S1) | gain);
try{
mcp3428.write(reg); // write configuration into config register.
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
public double readADC()
{
try{
mcp3428.read(b,0,3); // read byte data into byte buffer b
Thread.sleep(300);
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
ex.printStackTrace();
}
int h = b[0] & 0xff;
int l = b[1] & 0xff;
int r = b[2] & 0xff;
long t = h << 8 | l;
if (t >= 32768)
t = 65536l - t;
System.out.println("ADC output : " + t); // print Raw ADC value
double v = (double) t * VRef/32768.0; // convert the ADC value voltage
return v;
}
public static void main(String[] args)
{
MCP3428 sensor = new MCP3428();
final NumberFormat NF = new DecimalFormat("##00.00");
sensor.setConfig(CHANNEL_0,GAIN_1); //selected Channel = 0 and Gain = 1x
while(true)
{
System.out.println("OUTPUT FROM CHANNEL : " + sensor.readADC());
}
}
}