一、原理图
二、CubeMX配置
设置PB6,PB7为输出模式即可
三、代码
将这两个官方给好的.c.h文件放入bsp文件夹中,管理加入.c文件,在.c文件加入下列代码
unsigned char eeprom_read(unsigned char addr)
{
unsigned char dat;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
dat=I2CReceiveByte();
I2CWaitAck();
I2CStop();
return dat;
}
void eeprom_write(unsigned char addr,unsigned char dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
最后记得在写入的时候延时5ms
eeprom一个位置只能存储一个8位的数
如果数据位数大于8位,记得进行分割。
eeprom初始化
I2CInit();
判断是否第一次运行的eeprom代码
void eeprom_start()
{
eeprom_first1=eeprom_read(1);
eeprom_first2=eeprom_read(2);
if( eeprom_first1==234&&eeprom_first2==188 ) //不是第一次
{
num=eeprom_read(3);
}
else //第一次烧录运行
{
eeprom_write(1,234);
HAL_Delay(5);
eeprom_write(2,188);
HAL_Delay(5);
}
}