本帖最后由 落笔 于 2019-6-24 21:15 编辑
uchar *ADR = (uchar *)0x38000;//宏定义写入Flash的起始地址,位于bank3
//定义一个结构体
typedef struct
{
uint year;
uint monte;
uint day;
uint hour;
uint minute;
uint tube_num;
uint lock_force;
}Flash_seg;
/*************************** 擦除FLASH ***************************/
void FlashErase(uchar *address)
{
FCTL3 = FWKEY;
while(FCTL3 & BUSY);
FCTL1 = FWKEY + ERASE;
*((uchar *)address) = 0;//将对应地址上的双字节数据擦除
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
while(FCTL3 & BUSY);
}
/*************************** 按字节写FLASH ***************************/
void FlashWrite(uchar *address,Flash_seg *word,uint count)
{
FCTL3 = FWKEY;
while(FCTL3 & BUSY);
FCTL1 = FWKEY + WRT;
uint temp = 0;
for (uint i = 0;i < count;i++)
{
memcpy(&temp,((uchar *)(word))+i,1);
address = temp;
}
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
while(FCTL3 & BUSY);
}
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
lcd_init();
Flash_seg p;
p.year = 2019;
p.monte = 06;
p.day = 24;
p.hour = 10;
p.minute = 25;
p.tube_num = 01;
p.lock_force = 13;
uint count = sizeof( Flash_seg);
Flash_seg *q = &p;
show_int((*q).year);
LCD_CLEAR();
FlashErase(ADR);
for(uint i = 0;i < 128;i+= 14) //存128个字节的结构体中的数据
{
FlashWrite(ADR+ i,q,count);
}
}
|