AnsweredAssumed Answered

How can I use the I2C read in BF548

Question asked by xiaoming on Jun 22, 2014
Latest reply on Aug 8, 2014 by Suraj

I'm using a BF548 to configur a CMOS sensoro with I2C , however I'm confusing with the

I2C, according to the spection, the I2C should be writen as Page 24 at

HV7131R中文资料_百度文库

 

will somone please help me on the following questions:

1) how can I first write the address and register address and stop it without the stop bit; and start it as the 6 steps;

2) how can I transit to read mode; and get the data as the right time?

 

 

---------------------------------------------------------------------------------------

My reading function is as below :

 

//********************************************

unsigned char ZD_TWI0_Read(unsigned char *Readbuffer, unsigned char I2C_ConLen)
{
int i;

unsigned char data;

//1. First write the address of register
*pTWI0_FIFO_CTL = XMTFLUSH | RCVFLUSH;
ssync();

*pTWI0_MASTER_STAT = BUFWRERR | BUFRDERR | LOSTARB | ANAK | DNAK;
ssync();

*pTWI0_FIFO_CTL = 0;

//2. Configure the Prescal value
*pTWI0_CONTROL = TWI_ENA | PRESCALE_VALUE;
*pTWI0_CLKDIV = ((CLKDIV_HI) << 8) | (CLKDIV_LO);

//3. Configure the slave address

*pTWI0_MASTER_ADDR = CMOS_ADD;


*pTWI0_MASTER_CTL = (0x1<<6) | MEN |(0x01<<5);


*pTWI0_XMT_DATA8 = 0x01; // slave register address

while (*pTWI0_FIFO_STAT == XMTSTAT)
ssync();

//4. Read the data

for (i = 0; i < I2C_ConLen; i++)
{


// waiting for the address+1 to be writen
*pTWI0_MASTER_ADDR = CMOS_ADD;
*pTWI0_MASTER_CTL = (0x1<<6) | MEN |I2C_Read;

while (*pTWI0_FIFO_STAT == XMTSTAT)
ssync();


while (*pTWI0_FIFO_STAT == RCVSTAT)
ssync();

//*Readbuffer++ = *pTWI0_RCV_DATA8;
data = *pTWI0_RCV_DATA8;
ssync();

while ((*pTWI0_INT_STAT & MCOMP) == 0)
ssync();

*pTWI0_INT_STAT = RCVSERV | MCOMP;
}

return data;
asm("nop;");
asm("nop;");
asm("nop;");

}


Outcomes