//基于
msp430F149单片机的flash读写,记录
430的运行状态,
//实现在复位和掉电的情况下,恢复原始运行状态。
#include msp430x14x.h>
#include "function.h"
unsigned char value1=0; // 待写入段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至关重要!!需要此等待
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);//延时1s
write_SegA(*data+1); // 读出段A第一个值,增加1,并赋给value1,写入flash
sendi(i);sends(" : ");
sendi(*data);sends(""); // 串口发送信息
}
}