ok, so i reused the code first posted that allows 18 bit reading of the adc. i just cut out all the menus basicly.
Now I want to alternate between channel 0 and channel 1 with each sample taken (see the very end of my code).
all im doing is alternating the BANNED var between 0 and 1.
For some reason it doesn't work though. It's fine if instead of the var "chanAlternate" I put in an int (as was originally the way it works). but i want that number to change between 0 and 1 with each sample. the result is it just appears to keep a value of 1, despite my "if" condition trying to flip flop between 0 and 1.
can anyone point out what i'm doing wrong here?
also, does this chip allow you to have a different ground for each channel? or must the ground be shared?
Thanks heaps for any help guys.
Code: Select all
#include <Wire.h>
// I2C address for MCP3422 - base address for MCP3424
#define MCP3422_ADDRESS 0X68
// fields in configuration register
#define MCP342X_GAIN_FIELD 0X03 // PGA field
#define MCP342X_GAIN_X1 0X00 // PGA gain X1
#define MCP342X_GAIN_X2 0X01 // PGA gain X2
#define MCP342X_GAIN_X4 0X02 // PGA gain X4
#define MCP342X_GAIN_X8 0X03 // PGA gain X8
#define MCP342X_RES_FIELD 0X0C // resolution/rate field
#define MCP342X_RES_SHIFT 2 // shift to low bits
#define MCP342X_12_BIT 0X00 // 12-bit 240 SPS
#define MCP342X_14_BIT 0X04 // 14-bit 60 SPS
#define MCP342X_16_BIT 0X08 // 16-bit 15 SPS
#define MCP342X_18_BIT 0X0C // 18-bit 3.75 SPS
#define MCP342X_CONTINUOUS 0X10 // 1 = continuous, 0 = one-shot
#define MCP342X_CHAN_FIELD 0X60 // channel field
#define MCP342X_CHANNEL_1 0X00 // select MUX channel 1
#define MCP342X_CHANNEL_2 0X20 // select MUX channel 2
#define MCP342X_CHANNEL_3 0X40 // select MUX channel 3
#define MCP342X_CHANNEL_4 0X60 // select MUX channel 4
#define MCP342X_START 0X80 // write: start a conversion
#define MCP342X_BUSY 0X80 // read: output not ready
int chanAlternate = 0; //BANNED alternation per read
//------------------------------------------------------------------------
// default adc congifuration register - resolution and gain added in setup()
uint8_t adcConfig = MCP342X_START | MCP342X_CHANNEL_1 | MCP342X_CONTINUOUS;
// divisor to convert ADC reading to milivolts
uint16_t mvDivisor;
//------------------------------------------------------------------------------
void halt(void)
{
Serial.println("Halted");
while(1);
}
//------------------------------------------------------------------------------
// read mcp342x data
uint8_t mcp342xRead(int32_t &data)
{
// pointer used to form int32 data
uint8_t *p = (uint8_t *)&data;
// timeout - not really needed?
uint32_t start = millis();
do {
// assume 18-bit mode
Wire.requestFrom(MCP3422_ADDRESS, 4);
if (Wire.available() != 4) {
Serial.println("read failed");
return false;
}
for (int8_t i = 2; i >= 0; i--) {
p[i] = Wire.receive();
}
// extend sign bits
p[3] = p[2] & 0X80 ? 0XFF : 0;
// read config/status byte
uint8_t s = Wire.receive();
if ((s & MCP342X_RES_FIELD) != MCP342X_18_BIT) {
// not 18 bits - shift bytes for 12, 14, or 16 bits
p[0] = p[1];
p[1] = p[2];
p[2] = p[3];
}
if ((s & MCP342X_BUSY) == 0) return true;
} while (millis() - start < 500); //allows rollover of millis()
Serial.println("read timeout");
return false;
}
//------------------------------------------------------------------------------
// write mcp342x configuration byte
uint8_t mcp342xWrite(uint8_t config)
{
Wire.beginTransmission(MCP3422_ADDRESS);
Wire.send(config);
Wire.endTransmission();
}
//------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
Wire.begin();
uint8_t chan = 0XFF, gain = 0XFF, res = 0XFF;
chan = 0;
gain = 0;
res = 3;
adcConfig |= chan << 5 | res << 2 | gain;
// divisor to convert ADC reading to millivolts
mvDivisor = 1 << (gain + 2*res);
}
//------------------------------------------------------------------------------
void loop()
{
// Really only needed write once in contiguous mode. Could move to setup.
uint8_t chan = 0XFF, gain = 0XFF, res = 0XFF;
chan = chanAlternate;
gain = 0;
res = 3;
adcConfig |= chan << 5 | res << 2 | gain;
mcp342xWrite(adcConfig);
int32_t data;
if (!mcp342xRead(data)) halt();
// voltage in millivolts
double mv = (double)data/mvDivisor;
Serial.print(mv);Serial.print(" mv");Serial.println(chanAlternate);
if(chanAlternate == 0) {
chanAlternate = 1;
} else {
chanAlternate = 0;
}
delay(1000/3.75);
}