📄 eprom.c
字号:
#include "default.h"
#include "Stdafx.h"
#include "Eprom.h"
/* 定义命令 */
#define READ_AP_and_Data_Memory_Command 0x01 /* 字节读数据存储区 */
#define PROGRAM_AP_and_Data_Memory_Command 0x02 /* 字节编程数据存储区 */
#define SECTOR_ERASE_AP_and_Data_Memory_Command 0x03 /* 扇区擦除数据存储区 */
//20MHZ
#define WAIT_TIME 0x01
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0x7e00
#define DEBUG_AP_Memory_End_Byte_addr 0x7fff
#define DEBUG_Data_Memory_Begin_Sector_addr 0x8000
/* 打开 ISP,IAP 功能 */
void ISP_IAP_enable(void)
{
EA = 0; /* 关中断 */
ISP_CONTR = ISP_CONTR & 0x18; /* 0001,1000 */
ISP_CONTR = ISP_CONTR | WAIT_TIME;
ISP_CONTR = ISP_CONTR | 0x80; /* 1000,0000 */
}
/* 关闭 ISP,IAP 功能 */
void ISP_IAP_disable(void)
{
ISP_CONTR = ISP_CONTR & 0x7f; /* 0111,1111 */
ISP_TRIG = 0x00;
EA = 1; /* 开中断 */
}
void ispcommand(uchar uCommand)
{
ISP_CMD &= 0xF8; /* 1111,1000 */
ISP_CMD |= uCommand;
}
uchar readchar(uint byte_addr)
{
ISP_DATA = 0;
ispcommand(READ_AP_and_Data_Memory_Command); /* 0000,0001 */
ISP_IAP_enable();
ISP_ADDRH = (uchar)(byte_addr >> 8);
ISP_ADDRL = (uchar)(byte_addr & 0x00ff);
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9;
_nop_();
ISP_IAP_disable();
return (ISP_DATA);
}
void writechar(uint byte_addr,uchar uWrite)
{
ispcommand(PROGRAM_AP_and_Data_Memory_Command);/* 0000,0010 */
ISP_IAP_enable();
ISP_ADDRH = (uchar)(byte_addr >> 8);
ISP_ADDRL = (uchar)(byte_addr & 0x00ff);
ISP_DATA = uWrite;
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9;
_nop_();
ISP_IAP_disable();
}
/* 扇区擦除 */
uchar sector_erase(uint uSector)
{
uint xdata sector_addr = DEBUG_Data_Memory_Begin_Sector_addr + uSector*512;
uint xdata get_sector_addr = 0;
get_sector_addr = (sector_addr & 0xfe00); /* 1111,1110,0000,0000; 取扇区地址 */
ISP_ADDRH = (uchar)(get_sector_addr >> 8);
ISP_ADDRL = 0x00;
ispcommand(SECTOR_ERASE_AP_and_Data_Memory_Command); /* 0000,0011 */
ISP_IAP_enable();
ISP_TRIG = 0x46; /* 触发ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 触发ISP_IAP命令 */
ISP_IAP_disable();
_nop_();
return OK;
}
/* 字节编程并校验 */
//uSector 第几扇区,pWrite,写入数据指针, uint uCount,多少个字节
bit WriteEprom(uchar uSector, uchar *pWrite, uint uCount)
{
uint xdata i = 0;
uchar xdata ii;
uint xdata byte_addr = DEBUG_Data_Memory_Begin_Sector_addr + uSector*512;
uint xdata uMove = 0;
bit bBreak = 0;
if(uCount > 256)
{
sector_erase(uSector);// 扇区擦除
}else
{
uMove = (510 - 510/(8*uCount))/(8*uCount) + 1;
if(readchar(byte_addr) != (uchar)uCount)
{
sector_erase(uSector);// 扇区擦除
writechar(byte_addr, (uchar)uCount);
}else
{
for(i = 0; i < (510 - 510/(8*uCount))/(8*uCount);i++)
{
uchar xdata tChar = readchar(byte_addr + i + 1);
for(ii = 0; ii < 8; ii++)
{
if((tChar &(0x01 << ii)) == 0)
{
uMove += uCount;
}else
{
uchar xdata ij;
//对比新的参数是否和旧的一样
for(ij = 0; ij < uCount; ij++)
{
if(readchar(byte_addr + ij + uMove) != pWrite[ij])
{
bBreak = 1;
break;
}
}
if(bBreak)
{
tChar &= (0xFE << ii);
writechar(byte_addr + i + 1,tChar);
}else return OK;
break;
}
}
if(bBreak)
{
uMove += uCount;
}
if((tChar == 0)&&(i >= ((510 - 510/(8*uCount))/(8*uCount) - 1)))
{
sector_erase(uSector);// 扇区擦除
writechar(byte_addr, (uchar)uCount);
uMove = (510 - 510/(8*uCount))/(8*uCount) + 1;
}
if(bBreak)
{
break;
}
}
}
}
//ConvetLed2((long)uMove + 1000);
//delay(60000);delay(60000);
for(i = 0; i < uCount; i++)
{
writechar(byte_addr + i + uMove, pWrite[i]);
}
for(i = 0; i < uCount; i++)
{
if(readchar(byte_addr + i + uMove) != pWrite[i])
{
P4_3 = 0;
delay(5000);
P4_3 = 1;
ConvetLed2(i + uMove);
delay(60000);delay(60000);delay(60000);delay(60000);
return ERROR;
}
}
return OK;
}
/* 读Eprom*/
//uSector 第几扇区,pWrite,读出数据指针, uint uCount,多少个字节
bit ReadEprom(uchar uSector, uchar *pWrite, uint uCount)
{
uint xdata i = 0;
uchar xdata ii;
uint xdata byte_addr = DEBUG_Data_Memory_Begin_Sector_addr + uSector*512;
uint xdata uMove = 0;
bit bBreak = 0;
if(uCount > 256)
{
}else
{
uMove = (510 - 510/(8*uCount))/(8*uCount) + 1;
if(readchar(byte_addr) != (uchar)uCount)
{
P4_3 = 0;
delay(5000);
P4_3 = 1;
delay(5000);
P4_3 = 0;
delay(5000);
P4_3 = 1;
delay(5000);
P4_3 = 0;
delay(5000);
P4_3 = 1;
}
for(i = 0; i < (510 - 510/(8*uCount))/(8*uCount);i++)
{
uchar xdata tChar = readchar(byte_addr + i + 1);
for(ii = 0; ii < 8; ii++)
{
if((tChar &(0x01 << ii)) == 0)
{
uMove += uCount;
}else
{
bBreak = 1;
break;
}
}
if(bBreak)
{
break;
}
}
}
for(i = 0; i < uCount; i++)
{
pWrite[i] = readchar(byte_addr + i + uMove);
}
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -