在线时间57 小时
UID3080775
注册时间2014-12-5
NXP金币0
该用户从未签到
中级会员
 
- 积分
- 357
- 最后登录
- 2015-12-1
|

楼主 |
发表于 2015-4-23 15:56:07
|
显示全部楼层
本帖最后由 cjpx84 于 2015-4-23 15:58 编辑
我现在的代码就是参考手册上的中断流程写的。我用的是master,那块代码。
unsigned short i2c_read_word(unsigned char slaveID,unsigned int cmdCode)
{
unsigned char result[2];
unsigned short retval=0;
unsigned char dumpbyte;
clear_iicif();
start:
while( i2c_bus_busy());-------------总线判忙
/* send start signal */
i2c_Start();
/* Send Slave Address */
i2c_send_slave_addr(slaveID,MWSR);
/*wait the data transfer complete*/
if(i2c_Wait())
goto start;
i2c_get_ack();
/* Write command code*/
i2c_write_byte(cmdCode);
if(i2c_Wait())
goto start;
i2c_get_ack();
/* Do a repeated start */
i2c_RepeatedStart();
/* Send Slave Address */
i2c_send_slave_addr(slaveID,MRSW);
if(i2c_Wait())
goto start;
i2c_get_ack();
/* Put in Rx Mode */
i2c_EnterRxMode();
/* Dummy read */
dumpbyte = I2C0_D ;
if(i2c_Wait())
goto start;
/*2nd to last byte to be read*/
/*set txack*/
i2c_give_nack();
result[0] = I2C0_D;
if(i2c_Wait())
goto start;
/*last byte to be read generate stop signal*/
i2c_Stop();
/* Read byte */
resu return retval;
}
int i2c_Wait()
{
int ret=0;
/**
* wait one byte transfer completes
* or loss of arbitration
* or detection I2C bus stop
**/
while((I2C0_S & I2C_S_IICIF_MASK)==0) { }
/*loss of arbitration*/
if(I2C0_S & I2C_S_ARBL_MASK){
/*write 1 to clear the ARBL flag*/
I2C0_S |= I2C_S_ARBL_MASK;
ret = 1;
}
/*detection I2C bus stop*/
if(I2C0_FLT & I2C_FLT_STOPF_MASK){
/*write 1 to clear the STOPF flag*/
I2C0_FLT |= I2C_FLT_STOPF_MASK;
ret =1;
}
/*write 1 clear the IICIF FLAG*/
I2C0_S |= I2C_S_IICIF_MASK;
return ret;
|
|