f103硬件iic读写24cxx
时间: 2023-07-11 13:45:49 浏览: 118
首先,你需要连接你的f103芯片和24cxx芯片。24cxx是一个I2C EEPROM,所以你需要连接SDA和SCL引脚到f103的相应引脚。
接下来,你需要在f103上配置I2C总线。你可以使用STM32的I2C库来实现。
以下是一个简单的例子:
1. 首先,你需要初始化I2C总线。
```c
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// 配置I2C总线的GPIO引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 配置I2C总线
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
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);
```
2. 读取24cxx芯片的数据
```c
uint8_t read_data[10];
uint16_t read_address = 0x0000; // 24cxx芯片的起始地址
uint8_t device_address = 0xA0; // 24cxx芯片的设备地址
// 发送读取请求
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// 发送24cxx芯片的设备地址和读取请求
I2C_Send7bitAddress(I2C1, device_address, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// 发送24cxx芯片的起始地址
I2C_SendData(I2C1, (read_address >> 8) & 0xFF);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, read_address & 0xFF);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// 重新发送开始信号
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// 发送24cxx芯片的设备地址和读取请求
I2C_Send7bitAddress(I2C1, device_address, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
// 读取数据
for (int i = 0; i < 5; i++) {
if (i == 4) {
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
read_data[i] = I2C_ReceiveData(I2C1);
}
I2C_AcknowledgeConfig(I2C1, ENABLE);
// 停止I2C总线
I2C_GenerateSTOP(I2C1, ENABLE);
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));
```
3. 写入24cxx芯片的数据
```c
uint16_t write_address = 0x0000; // 24cxx芯片的起始地址
uint8_t write_data[5] = {0x01, 0x02, 0x03, 0x04, 0x05}; // 写入的数据
uint8_t device_address = 0xA0; // 24cxx芯片的设备地址
// 发送写入请求
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// 发送24cxx芯片的设备地址和写入请求
I2C_Send7bitAddress(I2C1, device_address, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// 发送24cxx芯片的起始地址
I2C_SendData(I2C1, (write_address >> 8) & 0xFF);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, write_address & 0xFF);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// 发送数据
for (int i = 0; i < 5; i++) {
I2C_SendData(I2C1, write_data[i]);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
// 停止I2C总线
I2C_GenerateSTOP(I2C1, ENABLE);
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));
```
这些代码可以帮助你开始读写24cxx芯片。需要注意的是,这些代码可能需要根据你的具体硬件和软件环境进行修改。
阅读全文