硬件平臺:master:stm32f401re slave:stm32f401ce
開發(fā)平臺:keil 5.18
操作系統(tǒng):win7
如上篇所講,將401CE配置為I2C從機,并設置為從發(fā)送模式。
主機的I2C與從機相同,同樣配置即可。同時增加uart設置,方便將接收到的數(shù)據(jù)打印出來。
主機的設置為主接收:
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連接起來,我們會在串口調試助手里發(fā)現(xiàn)從機發(fā)送過來的數(shù)據(jù),即上篇的”…h(huán)ello world…“。





