stm32iic读写eeprom
时间: 2025-01-01 13:18:47 浏览: 19
### STM32 I2C EEPROM 读写 示例代码 教程
#### 初始化I2C外设
为了实现STM32通过I2C接口与EEPROM通信,首先需要初始化I2C外设。这一步骤通常在`i2c.c`文件中完成。
```c
// i2c.c 文件中的部分代码片段
void I2C_Init(void){
GPIO_InitTypeDef GPIO_InitStruct = {0};
I2C_InitTypeDef I2C_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_I2C1_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
I2C_InitStruct.Timing = 0x20909CEC;
I2C_InitStruct.OwnAddress1 = 0;
I2C_InitStruct.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2C_InitStruct.DualAddressMode = I2C_DUALADDRESS_DISABLE;
I2C_InitStruct.OwnAddress2 = 0;
I2C_InitStruct.GeneralCallMode = I2C_GENERALCALL_DISABLE;
I2C_InitStruct.NoStretchMode = I2C_NOSTRETCH_DISABLE;
HAL_I2C_Init(&hi2c1);
}
```
这段代码展示了如何设置SCL和SDA引脚以及配置I2C参数[^1]。
#### 定义头文件
接着,在`i2c.h`文件里定义必要的宏定义、结构体声明和其他函数原型:
```c
#ifndef __I2C_H__
#define __I2C_H__
#include "stm32f1xx_hal.h"
extern I2C_HandleTypeDef hi2c1;
void I2C_Init(void);
uint8_t I2C_EEPROM_ReadByte(uint16_t DevAddress,uint16_t MemAddress);
void I2C_EEPROM_WriteByte(uint16_t DevAddress,uint16_t MemAddress,uint8_t Data);
#endif /* __I2C_H__ */
```
这里包含了对外部变量`hi2c1`的声明,并提供了两个用于操作EEPROM的方法——读取字节(`ReadByte`)和写入字节(`WriteByte`)。
#### 主程序逻辑
最后,在`main.c`文件内编写主循环来测试上述功能:
```c
int main(void)
{
HAL_Init();
SystemClock_Config(); // 配置系统时钟
I2C_Init(); // 初始化I2C外设
while (1)
{
uint8_t data_to_write = 'H';
uint8_t read_data;
// 向指定地址写入单个字节的数据
I2C_EEPROM_WriteByte(DEVICE_ADDR, MEMORY_ADDR, data_to_write);
// 延迟一段时间等待写入完成
HAL_Delay(WRITE_DELAY_MS);
// 从相同位置读回该字节并显示其值
read_data = I2C_EEPROM_ReadByte(DEVICE_ADDR, MEMORY_ADDR);
if(read_data != data_to_write){
Error_Handler(__FILE__, __LINE__);
}
}
}
/**
* @brief Write a byte to the specified address of an EEPROM device.
*
* @param DevAddress The slave address of the target EEPROM device on the bus.
* @param MemAddress Internal memory address within the EEPROM where writing starts from.
* @param Data Byte value that will be written into the EEPROM cell at given location.
*/
void I2C_EEPROM_WriteByte(uint16_t DevAddress,uint16_t MemAddress,uint8_t Data){
HAL_StatusTypeDef status;
status = HAL_I2C_Mem_Write(&hi2c1, DevAddress, MemAddress, I2C_MEMADD_SIZE_8BIT, &Data, sizeof(Data), WRITE_TIMEOUT);
if(status != HAL_OK){
Error_Handler(__FILE__,__LINE__);
}
}
/**
* @brief Read one byte from specific address inside an EEPROM chip connected via I2C interface.
*
* @param DevAddress Targeted EEPROM's hardware address over I2C communication line.
* @param MemAddress Location offset inside selected EEPROM which user wants to access.
* @return Value stored in requested position as single-byte integer type.
*/
uint8_t I2C_EEPROM_ReadByte(uint16_t DevAddress,uint16_t MemAddress){
uint8_t buffer[1];
HAL_StatusTypeDef status;
status = HAL_I2C_Mem_Read(&hi2c1, DevAddress, MemAddress, I2C_MEMADD_SIZE_8BIT, buffer, sizeof(buffer), READ_TIMEOUT);
if(status != HAL_OK){
Error_Handler(__FILE__,__LINE__);
}
return buffer[0];
}
```
此段代码实现了向特定内存地址写入字符'H'的操作,并随后尝试从中读取相同的数值以验证成功与否。如果两者不符,则调用错误处理函数报告异常情况。
阅读全文