简介
基于 BearPi Std 板从入门到放弃 - 后天篇(1)(I2C1 读取 光照强度), 使用同一个I2C接口访问EEPROM, 同时读取光照亮度
主芯片: STM32L431RCT6
LED : PC13 \ 推挽输出即可 \ 高电平点亮
串口: Usart1
I2C : I2C1
光照强度传感器:BH1750
EEPROM芯片 : AT24C02
芯片说明
-
BearPi 自带的扩展版 E53_SC1_智慧路灯的原理图
-
AT2402C 基本描述, 2Kbit, 32Page, 每个Page 1 Byte, 所以地址是0~255
-
AT2402C 的 设备地址规则
-
结论
a. 结合 原理图,
A0 = 1; A1 = 0; A2 =0;
写的地址 = 10100010 = 0xA2
读的地址 = 10100011 = 0xA3
b. 2Kbit, 32Page, 每个Page 1 Byte, 所以地址是0~255
步骤
- 基于前面文章, 创建工程
- 添加读写代码
#define AT24C02_WRITE_ADDRESS 0xA2
#define AT24C02_READ_ADDRESS 0xA3uint8_t AT24C02_Write_Byte(uint16_t addr, uint8_t *dat)
{return HAL_I2C_Mem_Write(&hi2c1, AT24C02_WRITE_ADDRESS, addr, I2C_MEMADD_SIZE_8BIT, dat, 1, 0xffff);
}uint8_t AT24C02_Read_Byte(uint16_t addr, uint8_t *buf)
{return HAL_I2C_Mem_Read(&hi2c1, AT24C02_READ_ADDRESS, addr, I2C_MEMADD_SIZE_8BIT, buf, 1, 0xffff);
}int main()
{...uint8_t res = 0;uint8_t datTemp = 0;/* 写完整个EEPROM */for (int i = 0; i < 255; ++i){datTemp = i;res = AT24C02_Write_Byte(i, &datTemp);if (res){printf("Faile To Write Address %d!\r\n", i);}HAL_Delay(10); // 写延时, 不然会失败}/* 读完整个EEPROM */for (int i = 0; i < 255; ++i){res = AT24C02_Read_Byte(i, &datTemp);if (res){printf("Faile To ReadAddress %d!\r\n", i);}else{printf("Address: %d, Value: %d\r\n", i, datTemp);}}while(1){...}
}
Note: 标记一下, 此篇还可以再完善 顺序读写, 随机读写
BearPi I2C1 完整项目工程, 包含两份芯片datasheet