基于STM32的三路IIC控制代码
时间: 2024-05-16 17:16:44 浏览: 82
以下是基于STM32的三路IIC控制代码示例:
```c
#include "stm32f10x.h"
#define I2C1_SLAVE_ADDRESS 0x50
#define I2C2_SLAVE_ADDRESS 0x51
#define I2C3_SLAVE_ADDRESS 0x52
void I2C1_Configuration(void);
void I2C2_Configuration(void);
void I2C3_Configuration(void);
int main(void)
{
uint8_t data[4] = {0x01, 0x02, 0x03, 0x04};
I2C1_Configuration();
I2C2_Configuration();
I2C3_Configuration();
while(1)
{
// I2C1 send data
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, I2C1_SLAVE_ADDRESS, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
for(int i=0; i<4; i++)
{
I2C_SendData(I2C1, data[i]);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
I2C_GenerateSTOP(I2C1, ENABLE);
// I2C2 receive data
I2C_GenerateSTART(I2C2, ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C2, I2C2_SLAVE_ADDRESS, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
for(int i=0; i<4; i++)
{
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED));
data[i] = I2C_ReceiveData(I2C2);
}
I2C_GenerateSTOP(I2C2, ENABLE);
// I2C3 send and receive data
I2C_GenerateSTART(I2C3, ENABLE);
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C3, I2C3_SLAVE_ADDRESS, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
for(int i=0; i<4; i++)
{
I2C_SendData(I2C3, data[i]);
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
I2C_GenerateSTOP(I2C3, ENABLE);
I2C_GenerateSTART(I2C3, ENABLE);
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C3, I2C3_SLAVE_ADDRESS, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
for(int i=0; i<4; i++)
{
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_RECEIVED));
data[i] = I2C_ReceiveData(I2C3);
}
I2C_GenerateSTOP(I2C3, ENABLE);
}
}
void I2C1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS << 1;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
void I2C2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C2);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS << 1;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C2, &I2C_InitStructure);
I2C_Cmd(I2C2, ENABLE);
}
void I2C3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
I2C_DeInit(I2C3);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C3_SLAVE_ADDRESS << 1;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C3, &I2C_InitStructure);
I2C_Cmd(I2C3, ENABLE);
}
```
以上代码中,使用了三个I2C总线,分别连接三个从设备。在每次循环中,首先使用I2C1总线发送数据,然后使用I2C2总线接收数据,最后使用I2C3总线同时发送和接收数据。在每个I2C总线的初始化函数中,配置GPIO和I2C参数,并使能I2C总线。对于每个I2C总线,需要使用不同的GPIO引脚和从设备地址。在每个数据传输过程中,需要检查I2C事件是否发生,并根据事件类型进行相应的操作。例如,在发送数据时,需要等待主模式选择事件、主模式发送器已选择事件和字节传输完成事件。在接收数据时,需要等待主模式选择事件、主模式接收器已选择事件和字节接收完成事件。
阅读全文