void test() { WORD i; P1 = 0xfe; //1111,1110 System Reset OK Delay(10); //Delay IapEraseSector(IAP_ADDRESS); //Erase current sector for (i=0; i<512; i++) //Check whether all sector data is FF { if (IapReadByte(IAP_ADDRESS+i) != 0xff) goto Error; //If error, break } P1 = 0xfc; //1111,1100 Erase successful Delay(10); //Delay for (i=0; i<512; i++) //Program 512 bytes data into data flash { IapProgramByte(IAP_ADDRESS+i, (BYTE)i); } P1 = 0xf8; //1111,1000 Program successful Delay(10); //Delay for (i=0; i<512; i++) //Verify 512 bytes data { if (IapReadByte(IAP_ADDRESS+i) != (BYTE)i) goto Error; //If error, break } P1 = 0xf0; //1111,0000 Verify successful while (1); Error: P1 &= 0x7f; //0xxx,xxxx IAP operation fail while (1); }
/* begin_addr,被写数据Flash开始地址;counter,连续写多少个字节; array[],数据来源 */ BYTE sequential_write_flash_in_one_sector(WORD begin_addr, WORD counter, BYTE array[]) { WORD i = 0; WORD in_sector_begin_addr = 0; WORD sector_addr = 0; /* 判是否是有效范围,此函数不允许跨扇区操作 */ if(counter > USED_BYTE_QTY_IN_ONE_SECTOR) return ERROR; in_sector_begin_addr = begin_addr & 0x01ff; if((in_sector_begin_addr + counter) > USED_BYTE_QTY_IN_ONE_SECTOR) return ERROR; /* 擦除 要修改/写入 的扇区 */ IapEraseSector(begin_addr); for(i=0; i<counter; i++) { /* 写一个字节 */ IapProgramByte(begin_addr, array[i]); /* 比较对错 */ if (IapReadByte(begin_addr) != array[i]) { IapIdle(); return ERROR; } begin_addr++; } IapIdle(); return OK; }
unsigned char EEPROM_Save_Water_Bound(unsigned char bound) { unsigned int i; //P1 = 0xfe; //1111,1110 系统OK EEPDelay(10); //延时 IapEraseSector(IAP_ADDRESS); //扇区擦除 for (i=0; i<512; i++) //检测是否擦除成功(全FF检测) { if (IapReadByte(IAP_ADDRESS+i) != 0xff) goto Error; //如果出错,则退出 } //P1 = 0xfc; //1111,1100 擦除成功 EEPDelay(10); //延时 //for (i=0; i<512; i++) //编程512字节 //{ IapProgramByte(IAP_ADDRESS, (unsigned char)bound); //} //P1 = 0xf8; //1111,1000 编程完成 EEPDelay(10); //延时 //for (i=0; i<512; i++) //校验512字节 //{ if (IapReadByte(IAP_ADDRESS) != (unsigned char)bound) goto Error; //如果校验错误,则退出 //} //P1 = 0xf0; //1111,0000 测试完成 //while (1); return 0; Error: //P1 &= 0x7f; //0xxx,xxxx IAP操作失败 //while (1); return 1; }
unsigned char parameter_save() { WORD i, result = 1; Delay(10); IapEraseSector(IAP_ADDRESS); //拆除EEPROM for (i=0; i<512; i++) //检测是否擦除成功 { if (IapReadByte(IAP_ADDRESS+i) != 0xff) result = 0; } Delay(10); //Delay for(i=0; i<8; i++) //写入EEPROM { IapProgramByte(IAP_ADDRESS+(i*2), (BYTE)(cistern[i].perSettingTime>>8)); IapProgramByte(IAP_ADDRESS+(i*2+1), (BYTE)cistern[i].perSettingTime); } IapProgramByte(IAP_ADDRESS+256, 0xEE); //写入标志位 Delay(10); for(i=0; i<8; i++) //校验保存值 { if(IapReadByte(IAP_ADDRESS+(i*2)) != (BYTE)(cistern[i].perSettingTime>>8)) { result = 0; } if(IapReadByte(IAP_ADDRESS+(i*2+1)) != (BYTE)cistern[i].perSettingTime) { result = 0; } } for(i=0; i<8; i++) //设置值为界面值 { cistern[i].settingTime = cistern[i].perSettingTime; } return result; }
void IAP_Angle() { IapEraseSector(0xE400); //加速度计数据存放至115扇区 //a_x=(char)Angle; //a_y=(char)Angley; IapProgramByte(0xE400,1); IapProgramByte(0xE401,g_fOffsetx); IapProgramByte(0xE402,g_fOffsety); }
/* begin_addr,被写数据Flash开始地址;counter,连续写多少个字节; array[],数据来源 */ BYTE write_flash_with_protect_in_one_sector(WORD begin_addr, WORD counter, BYTE array[]) { WORD i = 0; WORD in_sector_begin_addr = 0; WORD sector_addr = 0; WORD byte_addr = 0; /* 判是否是有效范围,此函数不允许跨扇区操作 */ if(counter > USED_BYTE_QTY_IN_ONE_SECTOR) return ERROR; in_sector_begin_addr = begin_addr & 0x01ff; /* 假定从扇区的第0个字节开始,到USED_BYTE_QTY_IN_ONE_SECTOR-1个字节结束,后面部分不用,程序易编写 */ if((in_sector_begin_addr + counter) > USED_BYTE_QTY_IN_ONE_SECTOR) return ERROR; /* 将该扇区数据 0 - (USED_BYTE_QTY_IN_ONE_SECTOR-1) 字节数据读入缓冲区保护 */ sector_addr = (begin_addr & 0xfe00); byte_addr = sector_addr; for(i = 0; i < USED_BYTE_QTY_IN_ONE_SECTOR; i++) { protect_buffer[i] = IapReadByte(byte_addr++); } /* 将要写入的数据写入保护缓冲区的相应区域,其余部分保留 */ for(i = 0; i < counter; i++) { protect_buffer[in_sector_begin_addr++] = array[i]; } /* 擦除 要修改/写入 的扇区 */ IapEraseSector(sector_addr); /* 将保护缓冲区的数据写入 Data Flash, EEPROM */ byte_addr = sector_addr; for(i = 0; i< USED_BYTE_QTY_IN_ONE_SECTOR; i++) { /* 写一个字节 */ IapProgramByte(byte_addr, protect_buffer[i]); /* 比较对错 */ if (IapReadByte(begin_addr) != protect_buffer[i]) { IapIdle(); return ERROR; } byte_addr++; } IapIdle(); return OK; }
void IAP_Gyro() //陀螺仪校准 { IapEraseSector(0xE200); //扇区擦除 g_fGyroYOffset = DataSynthesis(GYRO_YOUT_H); //读取陀螺仪数据 g_fGyroXOffset = DataSynthesis(GYRO_XOUT_H); g_fGyroZOffset = DataSynthesis(GYRO_ZOUT_H); IapProgramByte(0xE200,g_fGyroYOffset>>8); //写入数据至EEPROM IapProgramByte(0xE201,g_fGyroYOffset); IapProgramByte(0xE202,g_fGyroXOffset>>8); //陀螺仪数据存放至114扇区 IapProgramByte(0xE203,g_fGyroXOffset); IapProgramByte(0xE204,g_fGyroZOffset>>8); IapProgramByte(0xE205,g_fGyroZOffset); IapProgramByte(0xE206,1); }
void storageBaseInfo(){ uchar m,n; IapEraseSector(IAP_ADDRESS); IapProgramByte(IAP_ADDRESS+LOCAL_ADDRESS_ADDR, localAddress); IapProgramByte(IAP_ADDRESS+BRIGHT_THRESHOLD_ADDR, brightThreshold); if(isInfoSetted==1){ isSetted=1; } IapProgramByte(IAP_ADDRESS+IS_SETTED_ADDR, isSetted); for (m = 0; m < FIELD_COUNT; m++) { for (n = 0; n < STAFF_INFO_FIELD_LENGTN; n++) { IapProgramByte(IAP_ADDRESS+STAFF_INFO_ADDR+m*STAFF_INFO_FIELD_LENGTN+n, staffInfoBuf[m][n]); } } }
void test_eeprom(void) { unsigned int i; unsigned char dat; //清除数据 IapEraseSector(IAP_ADDRESS); for(i = 0; i < 512; i++) { if(IapReadByte(IAP_ADDRESS + i) != 0xff) { goto err; } } _nop_(); _nop_(); _nop_(); _nop_(); //写入数据 for(i = 0; i < 512; i++) { IapProgramByte(IAP_ADDRESS + i, (unsigned char)i); } _nop_(); _nop_(); _nop_(); _nop_(); //verify for(i = 0; i < 512; i++) { if((dat = IapReadByte(IAP_ADDRESS+i)) != (unsigned char)i) { goto err; } UART_Send_Char(dat); } err: while(1) { } }
/********************************************************************************************* 函数名:writeEpdat 参 数:无 返回值:无 备 注:EEPROM 读数据 **********************************************************************************************/ void writeEpdat(unsigned int addr, unsigned char* dat, unsigned char len) { unsigned char i; //清除数据 IapEraseSector(addr); for(i = 0; i < len; i++) { if(IapReadByte(addr + i) != 0xff) { UART_Send_String("ERASE ERR!"); } } //写入数据 for(i = 0; i < len; i++) { IapProgramByte(addr + i, (unsigned char)dat[i]); } }