我尝试在 stm32f4 上实现 i2c 从接收器中断服务程序。这是我的智能代码。
void I2C2_EV_IRQHandler()
{
switch (I2C_GetLastEvent(I2C2))
{
//The address sent by the master matches the own address of the peripheral
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
//The slave stretches SCL low until ADDR is
//cleared and DR filled with the data to be sent
I2C_ClearFlag(I2C2,I2C_FLAG_ADDR);
break;
//The application is expecting a data byte to be received
case I2C_EVENT_SLAVE_BYTE_RECEIVED:
I2C_ReceiveData(I2C2);
break;
//The application is expecting the end of the communication
//Make sure that both ADDR and STOPF flags are cleared
//if both are found set.
case I2C_EVENT_SLAVE_STOP_DETECTED:
if(I2C_GetFlagStatus(I2C2,I2C_FLAG_ADDR) == SET)
I2C_ClearFlag(I2C2,I2C_FLAG_ADDR);
if(I2C_GetFlagStatus(I2C2,I2C_FLAG_STOPF) == SET)
I2C_ClearFlag(I2C2,I2C_FLAG_STOPF);
}
}
中断被调用并进入 I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED 情况。SCL 现在很低。参考手册说,如果我清除地址标志,时钟将继续并发送数据(第 579 页 - 从属接收器)。在我看来,如果有任何数据到达并且下一个状态将是 I2C_EVENT_SLAVE_BYTE_RECEIVED,则总是会调用中断。
我无法从 stm 或通过 google 找到任何示例。任何人都可以帮助我或给我一个例子。