5213 I2C read function

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

5213 I2C read function

2,553 Views
airswit
Contributor III
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
Labels (1)
0 Kudos
1 Reply

509 Views
Nouchi
Senior Contributor II
hi,

I did something like this to read or write IIC bus (It works for a ST M41T56):

Code:
int rdWrIIc(uint8 *dataPtr, uint8 nBytes, uint8 slaveAddr, uint8 subAddr, uint8 rw){if(MCF_I2C_I2SR&MCF_I2C_I2SR_IBB) /* check if bus busy */{MCF_I2C_I2CR = 0; /* cf doc chap 22.6.1 */MCF_I2C_I2CR = MCF_I2C_I2CR_IEN + MCF_I2C_I2CR_MSTA;MCF_I2C_I2SR = 0; /* STOP condition */MCF_I2C_I2CR = 0;}MCF_I2C_I2CR = MCF_I2C_I2CR_IEN; /* enable IIC module */MCF_I2C_I2CR |= MCF_I2C_I2CR_IIEN; /* enable interrupt module IIC if ISR used */MCF_I2C_I2CR |= MCF_I2C_I2CR_MTX; /* send slave addess after START */MCF_I2C_I2CR |= MCF_I2C_I2CR_MSTA; /* master START condition */MCF_I2C_I2DR = slaveAddr; /* write slave address *//* here you have to wait for end of transfert *//* poll IIC ISR flag for example */MCF_I2C_I2DR = subAddr; /* write sub address *//* here you have to wait for end of transfert *//* poll IIC ISR flag for example */if(rw==IICWRITE) /* write device */{while(nBytes--) /* last byte — */{MCF_I2C_I2DR = *(dataPtr++); /* écriture de la donnée *//* here you have to wait for end of transfert *//* poll IIC ISR flag for example */}}else /*------------------------*/{ /* read device*/MCF_I2C_I2CR |= MCF_I2C_I2CR_RSTA; /* repeated START */MCF_I2C_I2DR = (uint8)(slaveAddr+1);/* Slave address + bit Read *//* here you have to wait for end of transfert *//* poll IIC ISR flag for example */if(nBytes 2) /* 1 byte ==> no ACK */MCF_I2C_I2CR |= MCF_I2C_I2CR_TXAK;MCF_I2C_I2CR &= ~MCF_I2C_I2CR_MTX; /* read mode */MCF_I2C_I2DR; /* dummy read to start */while(nBytes--) /* last byte – */{/* here you have to wait for end of transfert *//* poll IIC ISR flag for example */if(nBytes==1) /* if last but one */MCF_I2C_I2CR |= MCF_I2C_I2CR_TXAK; /* no ACK 4 last read */if(nBytes==0) /* if last byte */MCF_I2C_I2CR = 0; /* STOP condition */*(dataPtr++) = MCF_I2C_I2DR; /* read last */}}MCF_I2C_I2CR = 0; /* STOP condition */}

 

Emmanuel

Message Edited by Alban on 2006-09-06 02:20 PM

0 Kudos