两片STM32之间I2C通信
扫描二维码
随时随地手机看文章
硬件平台:master:stm32f401re slave:stm32f401ce
开发平台:keil 5.18
操作系统:win7
如上篇所讲,将401CE配置为I2C从机,并设置为从发送模式。
主机的I2C与从机相同,同样配置即可。同时增加uart设置,方便将接收到的数据打印出来。
主机的设置为主接收:
printf("nrStart....nr");
/* Put I2C peripheral in reception process */
/* Timeout is set to 10S */
while(HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 10000) != HAL_OK)
{
/* Error_Handler() function is called when Timeout error occurs.When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */
if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF)
{
printf("nrReceive failedn");
}
}
printf("nr %s nr", aRxBuffer);
/* Infinite loop */
while (1)
{
}
注:这里I2C_ADDRESS为与我们设置的从机地址相同
将两篇STM的板子I2C连接起来,我们会在串口调试助手里发现从机发送过来的数据,即上篇的”…hello world…“。