|
M430 FLASH seg-A seg-B 读写问题
[复制链接]
MSP430F149 写字符到FLASH后,读取出来会有时出错,大部分时候正常,对FLASH不是非常了解,但是偶尔不正常会造成信息出错,很麻烦,请帮忙看看。代码如下:
//*****将字符串写入FLASH,给定首地址
void Write_string_to_flash(char *value,unsigned int addr_flash)
{
unsigned char *Flash_ptr,i; // Flash pointer
_DINT();
Flash_ptr = (unsigned char *) addr_flash;
/*FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptr = 0; // Dummy write to erase Flash segment
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
*/
if((addr_flash < RECORD_FLASH_ADDR))//保存记录时不擦除
{
Flash_erase(addr_flash);
}
while(FCTL3 & BUSY) //如果处于忙状态,则等待
{
;//MY_WATCH_DOG; //看门狗计数器清零(WDTCTL=0x5A08)
}
FCTL3 = FWKEY ; //清出LOCK标志
FCTL1 = FWKEY + WRT ; //写操作,块编程,+ BLKWRT;
if(addr_flash < 0x1100) //在information 区,写128个
{
i=128;
while(i--)
{
*Flash_ptr++ =*value++;
while(!(FCTL3 & WAIT)) //如果处于忙状态,则等待 ,若用软件仿真,去掉 //这语句
{
;//MY_WATCH_DOG; //看门狗计数器清零(WDTCTL=0x5A08)
}
}
}
else
{
while(*value!=0)
{
*Flash_ptr++ =*value++;
while(!(FCTL3 & WAIT)) //如果处于忙状态,则等待 ,若用软件仿真,去掉 //这语句
{
;//MY_WATCH_DOG; //看门狗计数器清零(WDTCTL=0x5A08)
}
}
}
FCTL1 = FWKEY; //写操作完成,清除编程允许位 WRT,BLKWRT
while(FCTL3 & BUSY)
{
;//MY_WATCH_DOG; //看门狗计数器清零(WDTCTL=0x5A08)
}
FCTL3 = FWKEY + LOCK;
_EINT();
}
//***********从给定的FLASH首地值起读固定长度的数据
void Read_data_from_flash(char *goal,unsigned int addr_flash,int length)
{
char i=0;
unsigned char *Flash_ptr; // Flash pointer
_DINT();
Flash_ptr = (unsigned char *) addr_flash;
for(i=0;i
{
*(goal+i)=*(Flash_ptr+i);
}
_EINT();
}
//******************************************************************************
//FLASH段擦除
void Flash_erase(unsigned int addr_flash)
{
unsigned char *pc_word; // Flash pointer
pc_word = (unsigned char *) addr_flash;
//_DINT();
while(FCTL3 & BUSY) //如果处于忙状态,则等待
{
;//MY_WATCH_DOG; //看门狗计数器清零(WDTCTL=0x5A08)
}
FCTL3 = FWKEY ; //清出LOCK标志,解锁
FCTL1 = FWKEY + ERASE ; //允许段擦除
*pc_word = 0; //擦除..擦除..
while(FCTL3 & BUSY)
{
;//MY_WATCH_DOG; //看门狗计数器清零(WDTCTL=0x5A08)
}
FCTL3 = FWKEY + LOCK ; //加锁
//_EINT();
}
|
|