擦除Flash
static void FlashErase(DWORD Addr)
{
unsigned int tmp;
NVMCON = 0x4042;
TBLPAG = (Addr >> 16);
WREG0 = Addr;
tmp = SR ;
SR |= 0xE0;
NVMCONbits.WREN = 1;
NVMKEY=0x55;
NVMKEY=0xAA;
NVMCONbits.WR = 1;
while(NVMCONbits.WR);
NVMCONbits.WREN = 0;
Nop();
Nop();
SR = tmp;
}
读Flash
static BYTE FlashRead(DWORD Addr)
{
BYTE dat;
TBLPAG = (Addr >> 16);
WREG0 = Addr;
asm("TBLRDH.B [W0],W3");
asm("TBLRDL.B [W0++],W4");
asm("TBLRDL.B [W0++],W5");
dat = WREG5;
return dat;
}
写Flash
static void FlashWrite(BYTE* pBuffer, BYTE Len, DWORD Addr)
{
unsigned int tmp;
register unsigned int wr0 asm("w0");
register unsigned int in1 asm("w2");
register unsigned int in2 asm("w3");
register unsigned int in3 asm("w4");
DWORD *Addr_tmp;
Addr_tmp = Addr;
NVMCON = 0x4001;
TBLPAG = (Addr >> 16);
WREG0 = Addr;
while(Len--)
{
in1 = *pBuffer;
in2 = *pBuffer++;
asm("TBLWTL.B W2, [W0]");
asm("TBLWTH.B W3, [W0++]");
}
TBLPAG = (Addr >> 16);
WREG0 = Addr;
tmp = SR ;
SR |= 0xE0;
NVMCONbits.WREN = 1;
NVMKEY=0x55;
NVMKEY=0xAA;
NVMCONbits.WR = 1;
while(NVMCONbits.WR);
NVMCONbits.WREN = 0;
Nop();
Nop();
SR = tmp;
}
读Flash已经没有问题,写Flash并不能正常实现功能,擦除Flash目前不知道是否正常,求大侠指点,谢谢了!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>