本帖最后由 Deiki 于 2019-11-9 18:20 编辑
试着用IIC让两片STM8通信,一片做主机,一片做从机。主机发送地址后,就停留在发送第一个字节字符后,设备一直停在while(!I2C_GetFlagStatus(I2C_FLAG_TXEMPTY));
从机有地址匹配的中断产生,但是没有接受到数据。
主机初始化后,写数据
- void Write2Byte(void)
- {
- I2C_GenerateSTART(ENABLE);
- while(!I2C_GetFlagStatus(I2C_FLAG_STARTDETECTION));
- I2C_Send7bitAddress(0xB0, I2C_DIRECTION_TX);
- while(!I2C_GetFlagStatus(I2C_FLAG_ADDRESSSENTMATCHED));
- I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);// must add
- I2C_SendData(0x00);
- while(!I2C_GetFlagStatus(I2C_FLAG_TXEMPTY));
- I2C_SendData(0x10);
- while(!I2C_GetFlagStatus(I2C_FLAG_TXEMPTY));
- I2C_SendData(0x20);
- while(!I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED));
- I2C_GenerateSTOP(ENABLE);
- }
复制代码
从机初始化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;
- }}
复制代码
条件有限
一周热门 更多>