Trevor Switkowski

5213 I2C read function

Discussion created by Trevor Switkowski on Sep 5, 2006
Latest reply on Sep 5, 2006 by Nouchi
hi everyone,

I am in need of some code to read 7 consecutive bytes from a i2c real time clock. Basically, you first have to send the internal register pointer with a write to the device. then, you can begin reading as many bytes as you want, while the register pointer autoincrements. can you take a look at this code and let me know if this is near right? by the way, the part number for the clock is DS3231. here is the code:

uint8[7] readClock()
{
uint8 data[7],i;

MCF_I2C_I2CR |= MCF_I2C_I2CR_MTX; ///Tx ready
// send start condition
MCF_I2C_I2CR |= MCF_I2C_I2CR_MSTA;
MCF_I2C_I2DR = 0xD0; // devide ID
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ));
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2DR = reg; // memory address
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ));
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2CR &= ~MCF_I2C_I2CR_MSTA; //stop!

my_pause(100);

MCF_I2C_I2CR |= MCF_I2C_I2CR_MTX; ///Tx ready
// send start condition
MCF_I2C_I2CR |= MCF_I2C_I2CR_MSTA;
MCF_I2C_I2DR = 0xD1; // device id
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ));
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2CR &= ~MCF_I2C_I2CR_MTX; // Rx ready
// MCF_I2C_I2CR |= MCF_I2C_I2CR_TXAK; // send NO ACK
data[0] = MCF_I2C_I2DR; // dummy read of regsiter
for(i=0 ; i6 ; i++)
{
data[i] = MCF_I2C_I2DR; //start the xfer
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ));
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
data[i] = MCF_I2C_I2DR;
}
MCF_I2C_I2CR |= MCF_I2C_I2CR_TXAK;
data[6] = MCF_I2C_I2DR;
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ));
data[6] = MCF_I2C_I2DR;
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2CR &= ~MCF_I2C_I2CR_MSTA; //stop!


return data;
}

i would appreciate any feedback you can give me!

Thanks,
Trevor

Outcomes