Other Parts Discussed in Thread: PCF8575, HALCOGEN, TMS570LS3137
Zhaohong,
While playing I2C interface write to and read from I2C device PCF8575 (TI, I/O expansion), I am running into some issue. Hope to get some advice from you.
(1) Write data to PCF8575. Below code can successfull write data to PCF8675. But it has two byte "delay" after the salve address is sent out, so I have to write two dummy bytes following the data bytes that expected to write.
My question: Why I have to use two dummy byte write to "push" the data "55" and "FF" out?
I2CMDR_bit.nIRS =0;
// prescale and Baud rate setting here */
// mode control set below
I2CMDR_bit.NACKMOD = 0; //send ACK out
I2CMDR_bit.STB = 0; // not start byte
I2CMDR_bit.MST =1; //master
I2CMDR_bit.TRX = 1; //transmit
I2CMDR_bit.RM = 1; // repeate mode
I2CMDR_bit.DLB = 0; // no loop-back
I2CMDR_bit.FREE = 0; // free run
I2CMDR_bit.FDF = 1; // '1'=free data; '0'= Slave address at front
I2CMDR_bit.nIRS = 1; // out of reset
I2CMDR_bit.STT = 1; // start to send
i2cSendByte(i2cREG1, 0x40); // Send Addr 0x40 and R/W=0 - i2cSendByte() just checks and waits TXRDY=1, send write data to I2CDXR
i2cSendByte(i2cREG1, 0x55); // Send first byte "55"
i2cSendByte(i2cREG1, 0xFF); // Send 2nd byte "FF"
i2cSendByte(i2cREG1, 0x00); // 1st byte dummy
i2cSendByte(i2cREG1, 0x00); // 2nd byte dummy
I2CMDR_bit.STP = 1; // stop send
(2) Below code can read data from PCF8575. However, PCF8575 read input mode needs to write '1' to the I/O pins before reading.
When I use below read code following above write code, It receiving does not work, it stuck at checking "RXRDY" while loop in i2cReceiveByte().
I tried to insert I2C reset between write and read codes, which helps to read ony the 1st byte, then stuck.
Please advice if have something wrong or this related above "two bytes delay" issue?
I2CMDR_bit.MST = 1; // Still as Master
I2CMDR_bit.NACKMOD = 0; //send ACK to Slave
I2CMDR_bit.RM = 1; // repeat mode
I2CMDR_bit.TRX = 0; //set as Receiving mode
I2CMDR_bit.STT = 1; // Start
i2cSendByte(i2cREG1, 0x41); // send Slave address and R/W=1
I2CRD1 = i2cReceiveByte(i2cREG1); // Read 1st byte - i2cReceiveByte() just check and write RXRDY=1, then return I2CDRR value.
I2CRD2 = i2cReceiveByte(i2cREG1); // Read 2nd byte
I2CRD1 = i2cReceiveByte(i2cREG1); // Read 1st byte
keep reading ... until
I2CMDR_bit.NACKMOD = 1; // set NACK to tell Slave to finish data sending
I2CRD1 = i2cReceiveByte(i2cREG1); // Read the last byte, and sent NACK to slave
I2CMDR_bit.STP = 1; // stop the communication.
Thanks,
Yanzhong