//基于msp430F149單片機(jī)的flash讀寫(xiě),記錄430的運(yùn)行狀態(tài),//實(shí)現(xiàn)在復(fù)位和掉電的情況下,恢復(fù)原始運(yùn)行狀態(tài)。
本文引用地址:http://www.ex-cimer.com/article/201611/320704.htm#include <msp430x14x.h>
#include "function.h"
unsigned char value1=0; // 待寫(xiě)入段A的值
unsigned char *data =(unsigned char *)0x4080;// 段A的頭指針
void write_SegA (unsigned char value1)
{
char *Flash_ptr; // Flash pointer
Flash_ptr = (char *) 0x4080; // Initialize Flash pointer
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
*Flash_ptr = value1; // Write value1 to flash
_NOP(); // SET BREAKPOINT HERE至關(guān)重要?。⌒枰说却?br /> FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P3DIR |= 0x23;
P2DIR |= 0xFF;
XT2_open();
uart1_init((8000000/115200)%6,(8000000/115200)/256);
FCTL2 = FWKEY + FSSEL0 + FN2 + FN4; // MCLK/20 for Flash Timing Generator
while(1){
P2OUT =*data;//led燈指示
yanshi_1ms(1000);//延時(shí)1s
write_SegA(*data+1); // 讀出段A第一個(gè)值,增加1,并賦給value1,寫(xiě)入flash
sendi(i);sends(" : ");
sendi(*data);sends(""); // 串口發(fā)送信息
}
}
評(píng)論