I am trying to read analog channels from AD7998 using the MODM7AE70.
In the past I read this successfully using the MOD5234 using the I2C.h however I am struggling to read 2 bytes from the device using the Wire.h library.
As this library is new to me, I am sure that the problem is the way I am going about this - any help would be appreciated.
MODM7AE70 - the read does not work.
bool I2C_Services::ReadADCChannel(uint16_t &result , int iChannel)
{
bool blnOK = false;
int idChan = -1;
uint8_t Buffer[2];
Buffer[0] = 0x80|((iChannel & 0x7)<<4); // MSB = Command Mode 2 (0x08) + Channel No. , LSB= pointer to Conversion Register = 0 ;
Wire.beginTransmission(AD7998_ADDR);
Wire.write(Buffer, 1);
Wire.endTransmission(true);
Wire.beginTransmission(AD7998_ADDR);
blnOK = Wire.read(Buffer,2) != 0;
Wire.endTransmission(true);
if(blnOK )
{
u_int Rx= ((Buffer[0] << 8) + Buffer[1]);
idChan = (( Rx & 0x7000)>> 12);
result = ((Buffer[0] & 0x0F) << 8) | Buffer[1];
iprintf("AD Channel No: %d Value : %d \r\n\r\n", idChan , result);
}
return blnOK;
}
MOD5234 - working
void ReadAnalogChannel(AnalogType * pChannel)
{
BYTE Buffer[2];
int iChannel = pChannel->ChannelNo - 1; // ADC starts at channel 0
Buffer[0] = 0x80|((iChannel & 0x7)<<4); //Command Mode 2 - Set the channel number
if(I2C_OK == I2CSendBuf(I2C_AI , Buffer, 1))
{
if(I2C_OK == I2CReadBuf(I2C_AI, Buffer,2)) // Read the GPIO Registers
{
u_int Result= ((Buffer[0] << 8) + Buffer[1]);
int ChnNo= (( Result & 0x7000)>> 12);
if(iChannel == ChnNo)
{
AnalogScaling(pChannel, Result);
}
}
}
}
MODM7AE70 - I2C AD7998
Re: MODM7AE70 - I2C AD7998
Okay I managed to get it working. This is the working code for anyone else who needs it.
// Read the analog values and convert
bool I2C_Services::ReadADCChannel(float &result , int iChannel)
{
bool blnOK = false;
uint8_t Buffer[2];
uint16_t iResult;
Buffer[0] = 0x80|((iChannel & 0x7)<<4); // MSB = Command Mode 2 (0x08) + Channel No. , LSB= pointer to Conversion Register = 0 ;
Buffer[1] = 0;
OS_CRIT cs; // The critical section
{
OSCriticalSectionObj UserMainCriticalSection(cs);
Wire.beginTransmission(AD7998_ADDR);
int iBytes = Wire.write(Buffer, 1);
iBytes= Wire.requestFrom(AD7998_ADDR,2,true),
blnOK = Wire.available() && iBytes ==2 ;
if(blnOK ) // slave may send less than requested
{
Buffer[0] = Wire.read();
Buffer[1] = Wire.read();
int idChan = Buffer[0] >> 4;
iResult = ((Buffer[0] & 0x0F) << 8) | Buffer[1];
result = iResult * ADC_V_MAX/ADC_BINS ; // Apply Scaling
iprintf("AD Channel No: %d Value : %.3f volts \r\n", idChan , result);
}
}
return blnOK;
}
// Read the analog values and convert
bool I2C_Services::ReadADCChannel(float &result , int iChannel)
{
bool blnOK = false;
uint8_t Buffer[2];
uint16_t iResult;
Buffer[0] = 0x80|((iChannel & 0x7)<<4); // MSB = Command Mode 2 (0x08) + Channel No. , LSB= pointer to Conversion Register = 0 ;
Buffer[1] = 0;
OS_CRIT cs; // The critical section
{
OSCriticalSectionObj UserMainCriticalSection(cs);
Wire.beginTransmission(AD7998_ADDR);
int iBytes = Wire.write(Buffer, 1);
iBytes= Wire.requestFrom(AD7998_ADDR,2,true),
blnOK = Wire.available() && iBytes ==2 ;
if(blnOK ) // slave may send less than requested
{
Buffer[0] = Wire.read();
Buffer[1] = Wire.read();
int idChan = Buffer[0] >> 4;
iResult = ((Buffer[0] & 0x0F) << 8) | Buffer[1];
result = iResult * ADC_V_MAX/ADC_BINS ; // Apply Scaling
iprintf("AD Channel No: %d Value : %.3f volts \r\n", idChan , result);
}
}
return blnOK;
}
Re: MODM7AE70 - I2C AD7998
Thanks for sharing that Bryan