本帖最后由 astudent 于 2014-12-27 08:01 编辑
新用PIC24F256GB206,发现执行片内FLASH某个扇区擦除操作后,都需要重新初始化串口配置。不重新初始化,串口通信失效。会是什么原因?
FLASH操作代码是网上找的,如下:
#define FLASH_ERASEPAGE 0x4042
#define FLASH_PROGLINE 0x4001
//写周期函数
void FlashWriteCycle(void)
{
NVMKEY = 0x55;
NVMKEY = 0xaa;
_WR = 1;
Nop();
Nop();
}
//一次擦除8行块,512条指令,1536字节的函数
void FlashErasePage(UINT32 FlashAddr)
{
NVMCON = FLASH_ERASEPAGE;
TBLPAG = ((FlashAddr & 0x7F0000)>>16);
FlashAddr = (FlashAddr & 0x00FFFF);
asm("tblwtl w0,[w0]");
FlashWriteCycle();
}
//一次写1行块,64条指令,192字节的函数
void FlashWriteBlock(UINT32 FlashAddr,UINT8 *Dest)
{
UINT16 i;
NVMCON = FLASH_PROGLINE;
TBLPAG = ((FlashAddr & 0x7F0000)>>16);
FlashAddr = (FlashAddr & 0x00FFFF);
for(i=0; i<64; i++)
{
asm("tblwtl.b %0,[%1]"::"r"(*Dest),"d"(FlashAddr));
Dest++;
asm("tblwtl.b %0,[%1]"::"r"(*Dest),"d"(FlashAddr+1));
Dest++;
asm("tblwth.b %0,[%1]"::"r"(*Dest),"d"(FlashAddr));
Dest++;
// 跳过
Dest++;
FlashAddr += 2;
}
FlashWriteCycle();
}
//一次读1行块,64条指令,192字节的函数
void FlashReadBlock(UINT32 FlashAddr,UINT8 *Dest)
{
UINT16 i;
TBLPAG = ((FlashAddr & 0x7F0000)>>16);
FlashAddr = (FlashAddr & 0x00FFFF);
for(i=0; i<64; i++)
{
asm("tblrdl.b [%1],%0":"=r"(*Dest):"r"(FlashAddr));
Dest++;
asm("tblrdl.b [%1],%0":"=r"(*Dest):"r"(FlashAddr+1));
Dest++;
asm("tblrdh.b [%1],%0":"=r"(*Dest):"r"(FlashAddr));
Dest++;
// 跳过
Dest++;
FlashAddr += 2;
}
}
芯片配置如下:
// These are the same config settings used by the bootloader and must not be changed here.
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & FWDTEN_OFF & ICS_PGx3)
_CONFIG2( IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_NONE & FNOSC_FRCPLL & PLLDIV_NODIV & IOL1WAY_ON)
_CONFIG3( SOSCSEL_EC & WPCFG_WPCFGDIS & WPDIS_WPDIS) //Disable erase/write protect of all memory regions.
|