我正在使用基于STM32F10x的微控制器,并且正在与从设备通信。直到我向从属设备发送读取数据的命令为止,一切似乎都在进行,但是在while循环中它会卡住,因为我的BTF标志永远不会变高。如果我一起删除while循环,我将得到0x81而没有任何实际数据。
I2C1->CR1 |= I2C_CR1_START; // send start bit
delay(30000);
//while((I2C1->SR1 & I2C_SR1_TXE)); //read SR1
(void)I2C1->SR1;
I2C1->DR = 0x80; // slave address writing to DR 1000 0000 0x40 with R/W bit as
delay(30000);
while(!(I2C1->SR1 & I2C_SR1_TXE))// make sure transmit is empty.
{
}
(void)I2C1->SR1;
(void)I2C1->SR2;
I2C1->DR = 0xE3; // command for slave to send data, hold master mode on, clock stretching enabled
delay(30000);
while(!(I2C1->SR1 & I2C_SR1_TXE))
{
}
delay(80000);
I2C1->CR1 |= I2C_CR1_START; // send start bit
(void)I2C1->SR1;
I2C1->DR = 0x81;
delay(1000);
while(!(I2C1->SR1 & I2C_SR1_BTF)) // waiting for byte to finish transfer
{
}
dataaa[0] = I2C1->DR;
I2C1->CR1 &= ~I2C_CR1_ACK;// acknowledgement disabled
dataaa[1] = I2C1->DR;
I2C1->CR1 |= I2C_CR1_STOP;// acknowledgement disabled
temperature = temperature + dataaa[0];
temperature = temperature << 8;
temperature = temperature + dataaa[1];