最近使用PIC的FLASH来保存一些设置参数,从而省掉外部EEPROM。
在使用过程中有几个疑问:
1、如果出现了擦除块不成功的情况,如何判断?WRERR位只有在写操作异常时才被置1吗?在擦除失败时会不会也被置1?
2、如何判断读操作是成功还是失败?如果读操作不成功,TABLAT将保持上次的数据吗?
3、使用FLASH存储设置参数,相比外部EEPROM来讲,它的优点与劣势?我个人感觉如果程序弄好了,用Flash作存储比外部EEPROM更可靠。但没有真正有到过在产品上,还不了解情况。希望有经验的高手们指点一下~~~
附上我写的代码,已测试通过~~~望大侠们指正~~~
/********************************************
chip:pic18F67J10
compiler:MCC18
********************************************/
uchar Flash_Erase(unsigned short long address)
{
TBLPTR = address;
EECON1bits.WREN = 1;
EECON1bits.FREE = 1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0x0AA;
EECON1bits.WR = 1;
INTCONbits.GIE = 0;
if(EECON1bits.WRERR == 1) //疑问:如何判断擦除不成功
{
return 0;
}
else
{
return 1;
}
}
void Flash_Read(unsigned short long address,unsigned int Length,unsigned char *p)
{
unsigned char i;
TBLPTR = address;
for(i=0;i<Length;i++)
{
_asm
TBLRDPOSTINC
_endasm
*p++ = TABLAT;
}
}
uchar Flash_Write(unsigned short long address,unsigned int Length,unsigned char *p)
{
unsigned char i;
if( Length > 64)
{
Length = 64;
}
Flash_Erase(address);
TBLPTR = address-1;
for(i=0;i<64;i++)
{
if(i<Length)
{
TABLAT = *(p+i);
}
else
{
TABLAT = 0;
}
_asm
TBLWTPREINC
_endasm
}
EECON1bits.WREN = 1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0x0aa;
EECON1bits.WR = 1;
INTCONbits.GIE = 1;
if(EECON1bits.WRERR == 1)
{
return 0;
}
else
{
return 1;
}
} |