试着用IIC让两片STM8通信,一片做主机,一片做从机。主机发送地址后,就停留在发送第一个字节字符后,设备一直停在while(!I2C_GetFlagStatus(I2C_FLAG_TXEMPTY));
搞了好久没找到解决办法undefined求组。。。
主机发送信息的代码:
- <blockquote>void Write2Byte(void)
[color=rgb(51, 102, 153) !important]复制代码
从机初始化IIC,设置了中断:
- INTERRUPT_HANDLER(I2C_IRQHandler, 19)
- {
- uint8_t data;
- /* Read SR2 register to get I2C error */
- if ((I2C->SR2) != 0)
- {
- /* Clears SR2 register */
- I2C->SR2 = 0;
- }
- Event = I2C_GetLastEvent();
- switch (Event)
- {
- /******* Slave transmitter ******/
- /* check on EV1 */
- case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
- Tx_Idx = 0;
- UART1_Printf("I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED");
- break;
- /* check on EV3 */
- case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:
- /* Transmit data */
- I2C_SendData(Slave_Buffer_Rx[Tx_Idx++]);
- UART1_Printf("I2C_EVENT_SLAVE_BYTE_TRANSMITTING");
- break;
- /******* Slave receiver **********/
- /* check on EV1*/
- case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
- UART1_Printf("I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED");
- break;
- /* Check on EV2*/
- case I2C_EVENT_SLAVE_BYTE_RECEIVED:
- Slave_Buffer_Rx[Rx_Idx++] = I2C_ReceiveData();
- UART1_Printf("I2C_EVENT_SLAVE_BYTE_RECEIVED ");
- break;
- /* Check on EV4 */
- case (I2C_EVENT_SLAVE_STOP_DETECTED):
- /* write to CR2 to clear STOPF flag */
- I2C->CR2 |= I2C_CR2_ACK;
- UART1_Printf("I2C_EVENT_SLAVE_STOP_DETECTED ");
- break;
- default:
- break;
- }}
[color=rgb(51, 102, 153) !important]复制代码
|