项目里有些信息需要实现掉电不丢数据,所以我把一些数据放入了flash中,但是发现重新上电后数据并没有存入,我的读写flash的代码如下,读写的起始地址是0x1040
#define SegmentStart 0x1040 //0x01080 //Segment = 0x01000~0x010FF
void FlashWriteChar(unsigned int addr, char Data);
char FlashReadChar(unsigned int Addr);
void FlashWriteChar(unsigned int addr, char Data) { char *FlashPtr = (char *)addr; // Segment pointer FCTL3 = FWKEY; // Clear Lock bit FCTL1 = FWKEY + ERASE; // Set Erase bit *FlashPtr = 0; // Dummy write to erase Flash seg //_DINT(); *FlashPtr = Data; // Save Data //_EINT(); FCTL1 = FWKEY; // Clear WRT bit FCTL3 = FWKEY + LOCK; // Set LOCK bit } char FlashReadChar(unsigned int Addr) { char Data; char *FlashPtr = (char *)Addr; Data = *FlashPtr; return (Data); }
mian函数中部分初始化代码如下:
void main( void ) { WDTCTL = WDTPW + WDTHOLD; FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator