//开始保存数据起始页
#define HomePage 50 //作为EEPRO起始页
#define Module_1_SectorAddr 1
#define Module_2_SectorAddr 2
#define Module_3_SectorAddr 3
#define Memory_eSectorAddr 3
#define Module_Addr 4
#define Moto_eSectorAddr 5
//扇区首地址
#define PageSize 0x800
//STM32F103ZERC每页2K
#define SectorHeadAddr(Page) (0x08000000+(0X800*Page))
u16 EEPROM_Read(uint32_t ADDR,uint32_t addr) { //ADDR 扇区地址 addr 数据地址
u32 DataAddr=0;
u32 DataPagAddr=0;
u32 temAddr=0;
DataPagAddr=HomePage+ADDR;
temAddr=SectorHeadAddr(DataPagAddr);//换算出实际扇区首地址
DataAddr=temAddr+(addr*4); //数据的实际地址 整数地址
return (*(__IO uint16_t*)(DataAddr));
}
/*
函数用法:
实际首地址=ADDR+HomePage
数据存入的地址 = 实际首地址+addr*4
*/
u16 EEPROM_Write(uint32_t ADDR,uint32_t addr, u16 data){
int i=0;
// u32 tembuff[PageSize];
u32 tembuff[256];
u32 DataAddr=0;
u32 writeAddr=0;
u32 DataPagAddr=0;
u8 number[8];
DataPagAddr=HomePage+ADDR;
DataAddr=SectorHeadAddr(DataPagAddr);//换算出实际扇区首地址
memset(tembuff,0,sizeof tembuff);//清空缓存
for(i=0;i<(10);i++){
tembuff[i*4]=EEPROM_Read(ADDR,i*4); // 把数据读取出来,寄存在数组中
}
//解锁
FLASH_Unlock();
//清除标志位
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
//察除页的起始地址
FLASH_ErasePage(DataAddr); //檫地址页
tembuff[addr*4]=data&0xff;//把数据存到数组中
for(i=0;i<10;i++){
writeAddr= SectorHeadAddr(DataPagAddr)+(i*4); //数据页地址加偏移地址
sprintf(number,"%08X",writeAddr);//打印地址
OLED_ShowString(0,2,number,8);
FLASH_ProgramHalfWord(writeAddr,tembuff[i*4]);
}
//上锁(锁定)
FLASH_Lock();
// OLED_ShowString(10,2," lock",6);
}
////////////////////////////////////////////////////////////
写到EEPROM的数据
EEPROM_Write(1,1,0xA1);
EEPROM_Write(1,2,0xA2);
EEPROM_Write(1,3,0xA3);
EEPROM_Write(1,4,0xAA);
EEPROM_Write(2,1,0xB1);
EEPROM_Write(2,2,0xB2);
EEPROM_Write(2,3,0xB3);
EEPROM_Write(2,4,0xB4);
EEPROM_Write(3,1,0xC1);
EEPROM_Write(3,2,0xC2);
EEPROM_Write(3,3,0xC3);
EEPROM_Write(3,4,0xC4);
///////////////////////////////////////////////////////////////////////////////////
读取数据
Value_set.Soner_1=EEPROM_Read(1,1);//从EE中的1地址中读取
Value_set.Soner_2=EEPROM_Read(1,2);//从EE中的2地址中读取
Value_set.Soner_3=EEPROM_Read(1,3);//从EE中的3地址中读取
Value_set.Soner_4=EEPROM_Read(1,4);//从EE中的4地址中读取
sprintf(number,"%02X",(Value_set.Soner_1)&0xff);
OLED_ShowString(0,5,number,8);
sprintf(number,"%02X",(Value_set.Soner_2)&0xff);
OLED_ShowString(30,5,number,8);
sprintf(number,"%02X",(Value_set.Soner_3)&0xff);
OLED_ShowString(60,5,number,8);
sprintf(number,"%02X",(Value_set.Soner_4)&0xff);
OLED_ShowString(90,5,number,8);
Value_set.Soner_1=EEPROM_Read(2,1);//从EE中的1地址中读取
Value_set.Soner_2=EEPROM_Read(2,2);//从EE中的2地址中读取
Value_set.Soner_3=EEPROM_Read(2,3);//从EE中的3地址中读取
Value_set.Soner_4=EEPROM_Read(2,4);//从EE中的4地址中读取
sprintf(number,"%02X",(Value_set.Soner_1)&0xff);
OLED_ShowString(0,6,number,8);
sprintf(number,"%02X",(Value_set.Soner_2)&0xff);
OLED_ShowString(30,6,number,8);
sprintf(number,"%02X",(Value_set.Soner_3)&0xff);
OLED_ShowString(60,6,number,8);
sprintf(number,"%02X",(Value_set.Soner_4)&0xff);
OLED_ShowString(90,6,number,8);
Value_set.Soner_1=EEPROM_Read(3,1);//从EE中的1地址中读取
Value_set.Soner_2=EEPROM_Read(3,2);//从EE中的2地址中读取
Value_set.Soner_3=EEPROM_Read(3,3);//从EE中的3地址中读取
Value_set.Soner_4=EEPROM_Read(3,4);//从EE中的4地址中读取
sprintf(number,"%02X",(Value_set.Soner_1)&0xff);
OLED_ShowString(0,7,number,8);
sprintf(number,"%02X",(Value_set.Soner_2)&0xff);
OLED_ShowString(30,7,number,8);
sprintf(number,"%02X",(Value_set.Soner_3)&0xff);
OLED_ShowString(60,7,number,8);
sprintf(number,"%02X",(Value_set.Soner_4)&0xff);
OLED_ShowString(90,7,number,8);
一周热门 更多>