2018-10-03 02:05 AM
Hi,
We are using stm32f446zet for a new project . We want to read 3 bytes of data using i2c and the read function is as follows . The 1st two bytes are read correctly but the 3rd byte is always zero . Please help us to solve these issues
int I2C3_ReadShort (char Adr)
{
I2C3->CR1 |= (I2C_CR1_START); // send START bit
while (!(I2C3->SR1 & I2C_SR1_SB)) {}; // wait for START condition (SB=1)
I2C3->DR = SLAVE_ADDRESS_WRITE; // slave address -> DR (LSB=1)
while (!(I2C3->SR1 & I2C_SR1_ADDR)) {}; // wait for ADDRESS sent (ADDR=1)
int Status2 = I2C3->SR2; // read SR2 to clear flag
I2C3->DR = Adr; // register in chip -> DR
while (!(I2C3->SR1 & I2C_SR1_TXE)) {}; // wait for DR empty (TxE=1)
while (!(I2C3->SR1 & I2C_SR1_BTF)) {}; // wait for ByteTransferred (BTF=1)
I2C3->CR1 |=(I2C_CR1_START); // send START bit
while (!(I2C3->SR1 & I2C_SR1_SB)) {}; // wait for START condition (SB=1)
I2C3->DR = SLAVE_ADDRESS_READ; // slave address -> DR (LSB=0)
while (!(I2C3->SR1 & I2C_SR1_ADDR)) {}; // wait for ADDRESS sent (ADDR=1)
I2C3->CR1 |= I2C_CR1_ACK;
int Status4 = I2C3->SR2; // read status to clear flag
while (!(I2C3->SR1 & I2C_SR1_BTF)) {}; // wait for ByteReceived (BTF=1)
I2C3->CR1 &= ~I2C_CR1_ACK;
int x1 = I2C3->DR; // safe place
while (!(I2C3->SR1 & I2C_SR1_BTF)) {}; // wait for ByteReceived (BTF=1)
I2C3->CR1 |= (I2C_CR1_STOP); // send STOP bit
int x2 = I2C3->DR << 8; // safe place
int x3 = I2C3->DR << 16; // safe place
return ((int)(x1 + x2 + x3)); // return combined bytes
}
2018-10-03 03:31 AM
I2C bus sequences might differ.
Does the waveform on the bus match the one specified in the datasheet of your slave device ?
Which you could name here, BTW.