中断处理里面执行下面这个if判断函数后,能发送数据了。- void I2C_rx_tx(void)
- {
- static u8 sr1,sr2,sr3;;
- // save the I2C registers configuration
- sr1 = I2C1->SR1;
- sr2 = I2C1->SR2;
- sr3 = I2C1->SR3;
- //Communication error? //
- if (sr2 & (I2C_SR2_WUFH | I2C_SR2_OVR |I2C_SR2_ARLO |I2C_SR2_BERR))
- {
- // I2C1->CR2|= I2C_CR2_STOP; // stop communication - release the lines
- I2C1->SR2= 0; // clear all error flags
-
- }
- // More bytes received ?
- if ((sr1 & (I2C_SR1_RXNE | I2C_SR1_BTF)) == (I2C_SR1_RXNE | I2C_SR1_BTF))
- {
- // I2C_byte_received(I2C1->DR);
- }
- // Byte received ? //
- if( (sr1 & I2C_SR1_RXNE)&&(sr3 &I2C_SR3_BUSY))
- {
- GPIOE->ODR^=0X80;
- I2C_byte_received(I2C1->DR);
- }
-
- //NAK? (=end of slave transmit comm)//
- if (sr2 & I2C_SR2_AF)
- {
- I2C1->SR2 &= ~I2C_SR2_AF; // clear AF
- }
- // Stop bit from Master (= end of slave receive comm) //
- if (sr1 & I2C_SR1_STOPF)
- {
- I2C1->CR2 |= I2C_CR2_ACK; // CR2 write to clear STOPF
-
- }
- // Slave address matched (= Start Comm) //
- if( (sr1 & I2C_SR1_ADDR)&&(sr3 &I2C_SR3_BUSY))
- {
- GPIOC->ODR^=0X80;
- }
- // More bytes to transmit ? //
- if ((sr1 & (I2C_SR1_TXE | I2C_SR1_BTF)) == (I2C_SR1_TXE | I2C_SR1_BTF))
- {
- }
- // Byte to transmit ? //
- if (sr1 & I2C_SR1_TXE)
- {
- I2C1->DR =*(data);
- }
-
- }
|