BayEOS-Arduino  1.8.0_0.0.4
MCP342x.cpp
1 #include <Wire.h>
2 #include "MCP342x.h"
3 
5 {
6  Wire.begin();
7  _adr=0;
8 }
9 
10 MCP342x::MCP342x(byte adc_adr)
11 {
12  Wire.begin();
13  _adr=adc_adr;
14 }
15 
16 
17 void MCP342x::reset(void){
18  // General Call Reset as per Datasheet of the mcp3422/4
19  Wire.beginTransmission(B00000000);
20  Wire.write(B00000110);
21  Wire.endTransmission();
22  setConf(B10000000);
23 }
24 
25 
26 void MCP342x::readOutputRegister(byte adc_addr)
27 {
28  byte out_bytes = 0;
29  byte address = MCP342x_ADR + adc_addr;
30 
31 // cli();
32  Wire.beginTransmission(address);
33  Wire.endTransmission();
34 
35  if (resolution<3) out_bytes = 3;
36  else out_bytes = 4;
37 
38  Wire.requestFrom((int)address, (int)out_bytes);
39  while(Wire.available()<out_bytes) ;
40  for(int i=0; i<out_bytes; i++)
41  {
42  output_bcd[i]=Wire.read();
43  }
44 // sei();
45 }
46 
47 
48 // Parameter:
49 // rdy - ready bit -> in continuous mode has no effect
50 // in one-shot mode rdy=0 -> no efect
51 // rdy=1 -> initiate a new conversion
52 // ch - channel selection ch = 0 -> channel 1
53 // ch = 1 -> channel 2
54 // mode - mode = 1 -> continuous mode: the ADC performs data conversion continuously
55 // mode = 0 -> one-shot mode: the ADC performs a single conversion
56 // an enters standby mode
57 // rate - sample rate selection: rate = 0 240 SPS - 12-bits conversion
58 // rate = 1 60 SPS - 14-bits conversion
59 // rate = 2 15 SPS - 16-bits conversion
60 // rate = 3 3.75 SPS - 18-bits conversion
61 // gain - amplifier gain selection:
62 // gain = 0 -> x1 gain
63 // gain = 1 -> x2 gain
64 // gain = 2 -> x4 gain
65 // gain = 3 -> x8 gain
66 void MCP342x::setConf(byte adc_addr, byte rdy, byte ch, byte mode, byte rate, byte gain)
67 {
68  byte conf = ((B00000001 & rdy) << 7) |
69  ((B00000011 & ch) << 5) |
70  ((B00000001 & mode) << 4) |
71  ((B00000011 & rate) << 2) |
72  (B00000011 & gain);
73 
74  setConf(adc_addr, conf);
75 }
76 
77 void MCP342x::storeConf(byte rate, byte gain)
78 {
79  _conf_rg = ((B00000011 & rate) << 2) |
80  (B00000011 & gain);
81 }
82 
83 void MCP342x::runADC(byte ch){
84  setConf(_adr,(B10000000 | _conf_rg | ((B00000011 & ch)<<5)));
85 }
86 
87 void MCP342x::setConf(uint8_t conf){
88  setConf(_adr,conf);
89 }
90 
91 void MCP342x::setConf(uint8_t adc_addr, uint8_t conf)
92 {
93 // confByte = conf;
94  byte address = MCP342x_ADR + adc_addr;
95 
96 
97  resolution = (conf & B0001100) >> 2;
98  gain = (conf & B00000011);
99 
100  Wire.beginTransmission(address);
101  Wire.write(conf);
102  Wire.endTransmission();
103 }
104 
105 int MCP342x::getADCTime(void){
106  switch(resolution){
107  case 3:
108  return 1000/3.75+1;
109  break;
110  case 2:
111  return 1000/15+1;
112  break;
113  case 1:
114  return 1000/60+1;
115  break;
116  case 0:
117  return 1000/240+1;
118  break;
119  }
120 }
121 
122 float MCP342x::getData(void){
123  return getData(_adr);
124 }
125 
126 
127 float MCP342x::getData(byte adc_addr)
128 {
129  float lsb = 0.0;
130  boolean readOK = false;
131 
132  for(byte ii=0; ii<5; ii++) {
133  readOutputRegister(adc_addr);
134  if (resolution==3) {
135  if ((output_bcd[3]&B10000000) == 0) {readOK = true; break;}
136  }
137  else
138  if ((output_bcd[2]&B10000000) == 0) {readOK = true; break;}
139  }
140 
141  if (!readOK) return 100.0;
142 
143  zahl = 0;
144  zahl = output_bcd[0];
145 
146  switch(resolution){
147  case 0: zahl &= B00001111; break;
148  case 1: zahl &= B00111111; break;
149  case 2: break;
150  case 3: zahl &= B00000011; break;
151  }
152 
153  zahl = (zahl <<8);
154  zahl += output_bcd[1];
155 
156  if (resolution ==3) {
157  zahl = (zahl<<8);
158  zahl += output_bcd[2];
159  }
160 
161  lsb = 2*2.048 / pow(2,12+resolution*2);
162 
163  if(output_bcd[0] & B10000000) {
164  zahl = ~zahl + 1;
165  switch(resolution){
166  case 0: zahl &= 0x00000fff; break;
167  case 1: zahl &= 0x00003fff; break;
168  case 2: zahl &= 0x0000ffff; break;
169  case 3: zahl &= 0x0003ffff; break;
170  }
171  return -((zahl*lsb)/pow(2, gain));
172  }
173  else return (zahl*lsb)/pow(2, gain);
174 }
175 
176 
177 
178 
179 
float getData(byte adc_addr)
Definition: MCP342x.cpp:127
MCP342x()
Definition: MCP342x.cpp:4
void setConf(uint8_t adc_addr, uint8_t conf)
Definition: MCP342x.cpp:91