flash无法按照例程写入,必须先擦除数据才能写入,求指导

thebestfei 2014-03-13 04:03:31
问题如题,使用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
}
...全文
716 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
thebestfei 2014-03-13
  • 打赏
  • 举报
回复
是我脑残了……
falloutmx 2014-03-13
  • 打赏
  • 举报
回复 1
写FLASH肯定要先擦除,这是flash机制决定的。flash只写0不写1,擦除后全部变成0xff,这时才能写入正确的值

27,370

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧