HAL_I2C_Mem_Write函数详解
时间: 2023-11-20 11:34:29 浏览: 2777
HAL_I2C_Mem_Write函数是STM32 HAL库中的一个函数,用于向I2C设备的指定寄存器地址中写入数据。该函数的原型为:
HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
参数说明:
1. hi2c:指向I2C_HandleTypeDef结构体的指针,表示I2C外设的句柄。
2. DevAddress:I2C设备的地址。
3. MemAddress:I2C设备的寄存器地址。
4. MemAddSize:I2C设备寄存器地址的大小,可取值为I2C_MEMADD_SIZE_8BIT或I2C_MEMADD_SIZE_16BIT。
5. pData:指向要写入的数据缓冲区的指针。
6. Size:要写入的数据的字节数。
7. Timeout:操作超时时间。
函数返回值为HAL_StatusTypeDef类型,表示函数执行状态。如果函数执行成功,则返回HAL_OK,否则返回其他错误代码。
函数的详细作用是:向I2C设备的指定寄存器地址中写入指定数量的数据。在执行该函数之前,需要先调用HAL_I2C_Master_Transmit函数向I2C设备发送寄存器地址,以告诉设备写入数据的位置。然后再调用HAL_I2C_Mem_Write函数将数据写入设备。
相关问题
HAL_I2C_Mem_Write HAL_I2C_Mem_Read
### 使用STM32 HAL I2C Mem Write 和 Read 函数
#### 初始化I2C接口
为了使用`HAL_I2C_Mem_Write`和`HAL_I2C_Mem_Read`函数,首先需要通过调用`HAL_I2C_Init`完成I2C接口的初始化[^2]。
```c
// 假定已经在STM32CubeMX中配置好并生成了相应的初始化代码
HAL_StatusTypeDef status;
status = HAL_I2C_Init(&hi2c1);
if (status != HAL_OK) {
// 错误处理...
}
```
#### 写入数据到外部设备内存地址
当向具有内部存储器映射结构(如EEPROM)的器件发送数据时,可以利用`HAL_I2C_Mem_Write`函数。此函数允许指定目标设备地址、要访问的内存地址以及待传输的数据缓冲区长度。
```c
uint8_t DevAddress = 0xA0; // 设备地址, 对于AT24C02来说通常是0xA0或0xA2等
uint16_t MemAddress = 0x00AA; // 起始写入位置(内存地址)
uint8_t *pData = "Hello"; // 待写入的数据指针
uint16_t Size = strlen((char*)pData); // 数据大小
uint16_t MemAddSize = I2C_MEMADD_SIZE_8BIT;
status = HAL_I2C_Mem_Write(&hi2c1, DevAddress, MemAddress, MemAddSize, pData, Size, 1000);
if(status != HAL_OK){
// 处理错误情况
}
```
上述代码片段展示了如何将字符串"Hello"写入至从地址`MemAddress`开始的位置上,其中`DevAddress`代表的是连接在外围总线上具体芯片的选择地址;而`MemAddSize`参数则用于指示所使用的内存寻址宽度——这里选择了8位宽[^1]。
#### 从外部设备内存地址读取数据
对于从拥有内部RAM/Flash架构的外围模块获取信息而言,则会涉及到`HAL_I2C_Mem_Read`方法的应用。它同样接收一系列类似的参数来定位源端口及其偏移量,并最终把接收到的信息存放到预先分配好的数组里去。
```c
uint8_t readData[5]; // 存储读回的数据
size_t sizeToRead = sizeof(readData)/sizeof(uint8_t);
status = HAL_I2C_Mem_Read(&hi2c1, DevAddress, MemAddress, MemAddSize, readData, sizeToRead, 1000);
if(status != HAL_OK){
// 处理错误情况
}
for(int i=0;i<sizeToRead;i++){
printf("%c",readData[i]); // 输出读取的内容
}
```
这段示例说明了怎样从之前设定过的相同起始点处提取五个字节的数据出来,并逐个打印字符形式的结果给用户查看。
hal_i2c_mem_write和hal_i2c_mem_read库函数写一个写入并读取m24256存储芯片的程序
在嵌入式系统中,使用HAL (Hardware Abstraction Layer) 函数如`hal_i2c_mem_write` 和 `hal_i2c_mem_read` 来操作I2C总线通信是很常见的。这里是一个简单的示例,假设我们正在使用Linux下的mbed HAL库来控制M24256(一种支持I2C的闪存芯片),如何通过这两个函数来完成数据的写入和读取:
```c
#include "mbed.h"
#include "mbed_i2c.h"
// 定义I2C设备实例
I2C i2c(0); // 如果使用P0口的I2C,替换为对应的I2C实例
// M24256地址和起始地址
const int8_t M24256_ADDR = 0x50; // 检查实际地址是否正确
const int8_t WRITE_ADDR = 0x00; // 写入的数据起始位置
const int8_t READ_ADDR = 0x01; // 阅读的数据起始位置
void write_data(const uint8_t* data, size_t length) {
hal_i2c_mem_write(&i2c, M24256_ADDR, WRITE_ADDR, data, length);
MBED_LOG_INFO("Data written successfully");
}
uint8_t* read_data(size_t *length) {
uint8_t buffer[100]; // 假设最大读取长度为100字节
size_t actual_length;
if (hal_i2c_mem_read(&i2c, M24256_ADDR, READ_ADDR, buffer, sizeof(buffer), &actual_length) == I2C_OK) {
MBED_LOG_INFO("Read %zd bytes", actual_length);
*length = actual_length;
return buffer;
} else {
MBED_LOG_ERROR("Failed to read data");
return NULL;
}
}
int main() {
const uint8_t test_data[] = {0x01, 0x02, 0x03, 0x04}; // 要写入的数据
size_t write_size = sizeof(test_data);
write_data(test_data, write_size); // 写入数据
uint8_t* read_buffer = read_data(&write_size); // 读取刚刚写入的数据
// 检查读取结果
for (size_t i = 0; i < write_size; ++i) {
MBED_LOG_DEBUG("Read data: 0x%02X", read_buffer[i]);
}
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)