|
PIC16f887 flash读写函数
void write_flash(unsigned char H_addr,unsigned char L_addr,unsigned char W_dataH,unsigned char W_dataL)
{
//PIC16F887的flash的操作是以8个字为1个块的写操作,擦除是以16个字为一个1块的操作.
unsigned char i;
for(i=0;i<16;++i)
{
if(i==(L_addr&0x07)) //指定的单元进行写操作.
{
EEADRH=H_addr;
EEADR=L_addr;
EEDATA=W_dataL;
EEDATH=W_dataH;
EEPGD=1;
}
else //非指定的单元进行读操作.(flash中将原有内容读取到EEDATH,EEDATA中,再进行写操作时才不会被破坏).
{
EEADRH=H_addr;
EEADR=(L_addr&0xf0)+i;
EEPGD=1;
RD=1;
NOP();
NOP();
while(RD); //等待读取结束.
}
WREN=1;
//GIE=0;
EECON2=0x55;
EECON2=0xAA;
WR=1;
NOP();
NOP();
while(WR);
WREN=0;
//GIE=1;
}
return(1);
}
unsigned int read_flash(unsigned int address)
{
EEADRH=address>>8; //地址高5位.
EEADR=address&0xff; //地址低8位.
EEPGD=1; //目标存储器为program flash.
RD=1; //启动读操作.
NOP(); //必须放置两nop.
NOP();
return((((unsigned int)EEDATH)<<8) + EEDATA); //返回数据.
}
|
|