Lab_interaccio/2017/SmartCitizen/Test_ADC/Test_ADC.ino
2025-02-25 21:29:42 +01:00

68 lines
1.8 KiB
C++

#include <Wire.h>
#include <MCP342X.h>
MCP3424 adc1(0x69);
MCP3424 adc2(0x68);
void setup() {
SerialUSB.begin(57600);
Wire.begin();
adc1.generalCall(GC_RESET);
adc1.creg[CH1].bits = { GAINx1, SR18B, ONE_SHOT, CH1, 1 };
adc1.creg[CH2].bits = { GAINx1, SR18B, ONE_SHOT, CH2, 1 };
adc1.creg[CH3].bits = { GAINx1, SR18B, ONE_SHOT, CH3, 1 };
adc1.creg[CH4].bits = { GAINx1, SR18B, ONE_SHOT, CH4, 1 };
adc2.creg[CH1].bits = { GAINx1, SR18B, ONE_SHOT, CH1, 1 };
adc2.creg[CH2].bits = { GAINx1, SR18B, ONE_SHOT, CH2, 1 };
}
double value;
Channel active_ch = CH1;
bool blocking = false;
static char * errmsg[] = {"", "underflow", "overflow", "i2c", "in progress", "timeout"};
void loop() {
ConvStatus error = adc1.read(active_ch, value, blocking);
if (error == R_OK || error == R_OVERFLOW || error == R_UNDERFLOW) {
if (error != R_OK) {
SerialUSB.print("error: ");
SerialUSB.print(error, DEC);
if (active_ch == CH4) SerialUSB.println(" ");
else SerialUSB.print(" ");
} else {
SerialUSB.print("CH");
SerialUSB.print(1+active_ch, DEC);
SerialUSB.print(": ");
SerialUSB.print(value*1000, DEC);
if (active_ch == CH4) SerialUSB.println(" mV");
else SerialUSB.print(" mV ");
}
switch (active_ch) {
case CH1: active_ch = CH2; break;
case CH2: active_ch = CH3; break;
case CH3: active_ch = CH4; break;
case CH4: active_ch = CH1; break;
}
adc1.startNewConversion(active_ch);
} else {
#if 0
SerialUSB.print("CH");
SerialUSB.print(1+active_ch, DEC);
SerialUSB.print(": ");
SerialUSB.print("error: ");
SerialUSB.println(errmsg[error]);
#endif
}
asm volatile ("nop");
}