flash无法按照例程写入,必须先擦除数据才能写入,求指导
问题如题,使用flash标准单字节写程序,无法正确更新flash信息,原有数据均被字符‘@’取代,
改用首先擦除原有数据之后,在写入则正确,求解!
单片机型号为:C8051F410
网上荡的代码如下:
void FLASH_ByteWrite (FLADDR addr, char byte)
{
bit EA_SAVE = EA; // Preserve EA
char xdata * data pwrite; // FLASH write pointer
EA = 0; // Disable interrupts
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
pwrite = (char xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x01; // PSWE = 1 which enables writes
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
*pwrite = byte; // Write the byte
PSCTL &= ~0x01; // PSWE = 0 which disable writes
EA = EA_SAVE; // Restore interrupts
}
测试用先擦后写代码,相关初始化已在外部完成,使用下面的代码正确实现flash写入
void FLASH_Byte_Write (unsigned int addr, char byte)
{
bit EA_SAVE = EA; // Preserve EA
char xdata * data pwrite; // FLASH write pointer
EA = 0; // Disable interrupts
pwrite = (char xdata *)addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL = 0x03; // PSWE = 1 which enables writes
*pwrite = 0x0;
PFE0CN &= 0xFE; //单字节写入
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL = 0x01; // PSWE = 1 which enables writes
*pwrite = byte; // Write the byte
PSCTL = 0x0; // PSWE = 0 ,which disable writes
EA = EA_SAVE; // Restore interrupts
}