#include "N76E003.h"
#include "Flash.h"
static bit EA_Save_bit;
void Enable_IAP_Mode(void)
{
EA_Save_bit=EA;
EA=0;
TA=0xAA;
TA=0x55;
CHPCON|=0x01 ;
TA=0xAA;
TA=0x55;
IAPUEN|=0x01;
EA=EA_Save_bit;
}
//-----------------------------------------------------------------------------------------------------------
void Disable_IAP_Mode(void)
{
EA_Save_bit=EA;
EA=0;
TA=0xAA;
TA=0x55;
IAPUEN&=~0x01;
TA=0xAA;
TA=0x55;
CHPCON&=~0x01;
EA=EA_Save_bit;
}
//-----------------------------------------------------------------------------------------------------------
void Trigger_IAP(void)
{
EA_Save_bit=EA;
EA=0;
TA=0xAA;
TA=0x55;
IAPTRG|=0x01;
EA=EA_Save_bit;
}
uint8_t WriteDataToOnePage(uint16_t u16_addr,const uint8_t *pDat,uint8_t num)
{
uint8_t i,offset;
uint8_t code *pCode;
uint8_t xdata xd_tmp[128];
Enable_IAP_Mode();
offset=u16_addr&0x007F;
i = PAGE_SIZE - offset;
if(num>i)num=i;
pCode = (uint8_t code *)u16_addr;
for(i=0;i<num;i++)
{
if(pCode[i]!=0xFF)break;
}
if(i==num)
{
IAPCN =BYTE_PROGRAM_AP;
IAPAL = u16_addr;
IAPAH = u16_addr>>8;
for(i=0;i<num;i++)
{
IAPFD = pDat[i];
Trigger_IAP();
IAPAL++;
}
for(i=0;i<num;i++)
{
if(pCode[i]!=pDat[i])break;
}
if(i!=num) goto WriteDataToPage20;
}
else
{
WriteDataToPage20:
pCode = (uint8_t code *)(u16_addr&0xff80);
for(i=0;i<128;i++)
{
xd_tmp[i] = pCode[i];
}
for(i=0;i<num;i++)
{
xd_tmp[offset+i] = pDat[i];
}
do
{
IAPAL = (u16_addr&0xff80);
IAPAH = u16_addr>>8;
IAPCN = PAGE_ERASE_AP;
IAPFD = 0xFF;
Trigger_IAP();
IAPCN =BYTE_PROGRAM_AP;
for(i=0;i<128;i++)
{
IAPFD = xd_tmp[i];
Trigger_IAP();
IAPAL++;
}
for(i=0;i<128;i++)
{
if(pCode[i]!=xd_tmp[i])break;
}
}while(i!=128);
}
Disable_IAP_Mode();
return num;
}
//-------------------------------------------------------------------------
void WriteDataFlash(uint16_t u16_addr,uint8_t *pDat,uint16_t num)
{
uint8_t CPageAddr,EPageAddr,cnt;
CPageAddr=u16_addr>>7;
EPageAddr=(u16_addr+num)>>7;
while(CPageAddr!=EPageAddr)
{
cnt=WriteDataToOnePage(u16_addr,pDat,128);
u16_addr+=cnt;
pDat+=cnt;
num-=cnt;
CPageAddr=u16_addr>>7;
}
if(num)
{
WriteDataToOnePage(u16_addr,pDat,num);
}
}
//-------------------------------------------------------------------------
void ReadDataFlash(uint16_t u16_addr,uint8_t *pDat,uint16_t num)
{
uint16_t i;
for(i=0;i<num;i++)
pDat[i] = *(uint8_t code *)(u16_addr+i);
}
//-----------------------------------------------------------------------------------------------------------
问题描述:
unsigned char eepbuf[24];
利用 ReadDataFlash(0x4700,&eepbuf,24); 去读取对应的数据,发现与**作写入的数据不一样(读出来有数据,但是错误的)
我是利用ICP烧录工具读取单片机片上APROM数据查看到0x4700-0x4717 数据是和写入的数据是一致的。
会有什么原因导致呢?会不会读取地址数据前需要操作什么指令之类?或者void ReadDataFlash(uint16_t u16_addr,uint8_t *pDat,uint16_t num)这个函数写法有错?
|