I2C with MCP3021

Discussion to talk about software related topics only.
Post Reply
jojahn
Posts: 3
Joined: Mon Mar 07, 2011 7:09 am

I2C with MCP3021

Post by jojahn »

Hi there, thanks for the info from my previous posing. It helped a lot!

Now, I like to use the Microchip MCP 3021 (A/D converter) on my I2C bus. I can see the address of the chip, I can partially communicate with the chip but, get a time-out (Erorcode #4) and the same data back 2x 0x02.

The data sheet says, the master has to send an Ackn to the slave to get the next data set, how do I do that?

Thank's alot.
Josh

Here is my code:
// ------------------------- READ FROM MCP 3021 ------------------------------//
// Reads data from the A/D converter MCP3021
// Address = base address of chip 0x4D
int AD_Read_3021(BYTE Address)
{
BYTE Read_buffer[4]; // a little big larger - just in case
int i2cStatus;

I2CInit(0x16); // just in case re-init
i2cStatus = I2CStart( Address, 1 ); // read from address
if(i2cStatus > 4) // >4 = error
{
iprintf( "Bad I2CStatus after start conversation : %x\r\n", i2cStatus );
}
i2cStatus = I2CReadBuf(Address,Read_buffer,2);
iprintf( "Bad I2CStatus after reading data back : %x\r\n", i2cStatus );
iprintf( "Read Data A/D hex: %x ", Read_buffer[0]);
iprintf( "Read Data A/D hex: %x ", Read_buffer[1]);
// iprintf( "Read Data A/D hex: %x\r\n ", Read_buffer[2]);

I2CStop();
return(i2cStatus);
}
greengene
Posts: 164
Joined: Wed May 14, 2008 11:20 am
Location: Lakeside, CA

Re: I2C with MCP3021

Post by greengene »

i think you're mixing two types of I2C functions (well, at least on the
5270 or 5282 they would be).

if you only want to grab a single sample at a time, your best bet would
be just use the simple one function call with:
i2cStatus = I2CReadBuf(Address,Read_buffer,2);

or if you want to use the advanced functions, then use all 3:
I2CStart, I2CRead, and I2CStop

I2CRead and I2CReadBuf aren't equivalent.
Post Reply