17 void MCP342x::reset(
void){
19 Wire.beginTransmission(B00000000);
20 Wire.write(B00000110);
21 Wire.endTransmission();
26 void MCP342x::readOutputRegister(byte adc_addr)
29 byte address = MCP342x_ADR + adc_addr;
32 Wire.beginTransmission(address);
33 Wire.endTransmission();
35 if (resolution<3) out_bytes = 3;
38 Wire.requestFrom((
int)address, (
int)out_bytes);
39 while(Wire.available()<out_bytes) ;
40 for(
int i=0; i<out_bytes; i++)
42 output_bcd[i]=Wire.read();
66 void MCP342x::setConf(byte adc_addr, byte rdy, byte ch, byte mode, byte rate, byte gain)
68 byte conf = ((B00000001 & rdy) << 7) |
69 ((B00000011 & ch) << 5) |
70 ((B00000001 & mode) << 4) |
71 ((B00000011 & rate) << 2) |
77 void MCP342x::storeConf(byte rate, byte gain)
79 _conf_rg = ((B00000011 & rate) << 2) |
83 void MCP342x::runADC(byte ch){
84 setConf(_adr,(B10000000 | _conf_rg | ((B00000011 & ch)<<5)));
94 byte address = MCP342x_ADR + adc_addr;
97 resolution = (conf & B0001100) >> 2;
98 gain = (conf & B00000011);
100 Wire.beginTransmission(address);
102 Wire.endTransmission();
105 int MCP342x::getADCTime(
void){
130 boolean readOK =
false;
132 for(byte ii=0; ii<5; ii++) {
133 readOutputRegister(adc_addr);
135 if ((output_bcd[3]&B10000000) == 0) {readOK =
true;
break;}
138 if ((output_bcd[2]&B10000000) == 0) {readOK =
true;
break;}
141 if (!readOK)
return 100.0;
144 zahl = output_bcd[0];
147 case 0: zahl &= B00001111;
break;
148 case 1: zahl &= B00111111;
break;
150 case 3: zahl &= B00000011;
break;
154 zahl += output_bcd[1];
156 if (resolution ==3) {
158 zahl += output_bcd[2];
161 lsb = 2*2.048 / pow(2,12+resolution*2);
163 if(output_bcd[0] & B10000000) {
166 case 0: zahl &= 0x00000fff;
break;
167 case 1: zahl &= 0x00003fff;
break;
168 case 2: zahl &= 0x0000ffff;
break;
169 case 3: zahl &= 0x0003ffff;
break;
171 return -((zahl*lsb)/pow(2, gain));
173 else return (zahl*lsb)/pow(2, gain);
float getData(byte adc_addr)
void setConf(uint8_t adc_addr, uint8_t conf)