两个stm32单片机之间的iic通信例程
时间: 2024-01-03 13:05:01 浏览: 153
下面是一个示例代码,用于两个STM32单片机之间的IIC通信。这个例程中,其中一个STM32作为主设备(Master),另一个STM32作为从设备(Slave)。
主设备发送数据给从设备:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#define I2C_ADDRESS 0x08 // 从设备地址
I2C_InitTypeDef I2C_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
void I2C_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
I2C_DeInit(I2C1);
I2C_InitStruct.I2C_ClockSpeed = 100000;
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0x07; // 主设备地址
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStruct);
I2C_Cmd(I2C1, ENABLE);
}
void I2C_WriteData(uint8_t slaveAddress, uint8_t* data, uint8_t dataLength)
{
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, slaveAddress, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
for (uint8_t i = 0; i < dataLength; i++)
{
I2C_SendData(I2C1, data[i]);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
I2C_GenerateSTOP(I2C1, ENABLE);
}
int main(void)
{
uint8_t data[] = "Hello";
I2C_Configuration();
while (1)
{
I2C_WriteData(I2C_ADDRESS, data, sizeof(data));
delay(500);
}
}
```
从设备接收主设备发送的数据:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#define I2C_ADDRESS 0x07 // 从设备地址
I2C_InitTypeDef I2C_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
void I2C_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
I2C_DeInit(I2C1);
I2C_InitStruct.I2C_ClockSpeed = 100000;
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0x08; // 从设备地址
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStruct);
I2C_Cmd(I2C1, ENABLE);
}
void I2C_ReadData(void)
{
uint8_t receivedData;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_AcknowledgeConfig(I2C1, ENABLE);
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, I2C_ADDRESS, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
receivedData = I2C_ReceiveData(I2C1);
// 处理接收到的数据
}
int main(void)
{
I2C_Configuration();
while (1)
{
I2C_ReadData();
delay(500);
}
}
```
在主设备中,我们使用`I2C_WriteData()`函数发送数据给从设备。首先,我们等待IIC总线空闲,然后生成START信号,接着发送从设备地址和写入位,然后通过循环发送数据字节。最后,我们生成STOP信号结束传输。
在从设备中,我们使用`I2C_ReadData()`函数接收主设备发送的数据。首先,我们等待IIC总线空闲,然后生成START信号,接着发送从设备地址和读取位。然后,我们等待接收到数据字节,并进行处理。
请注意:以上示例代码是基于STM32F4系列的,使用了STM32标准外设库(Standard Peripherals Library)。如果你使用其他型号的STM32单片机或者使用了不同的开发库,可能需要进行相应的调整。
希望这个例程对你有所帮助!如果还有其他问题,请随时提问。
阅读全文