器件为MSP430G2553,使用LaunchPad做的验证
以下是代码
#pragma DATA_SECTION (label, ".infoA")
volatile unsigned int label[2]={3};
用CCS5.4编译时实现把名为label的数组存入Flash中Segment A中,第一次编译时好像可以,但往后每次执行编译时,segment A中的数据就不变了,好像没编译一样。
以下是完整程序,主要完成系统第一次加电进入CASE0,以后每次加电都会进入CASE2,以二极管闪烁快慢表示两种状态。
#include <msp430.h>
#pragma DATA_SECTION (label, ".infoA")
volatile unsigned int label={0};
unsigned int *Flash_ptrA;
unsigned int a;
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
unsigned int i;
for (i = 0; i < 0xfffe; i++); // Delay for XTAL stabilization
P1OUT = 0x00; // Clear P1 output latches
P1DIR = 0x11;
Flash_ptrA = (unsigned int *)0x10C0;
a= *Flash_ptrA;
switch(a)
{case 0://第一次加电默认进入
Flash_ptrA = (unsigned int *)0x10C0;
FCTL2 = FWKEY + FSSEL_0 + FN0; // for Flash Timing Generator
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY + LOCKA; // Clear LOCK & LOCKA bits
*Flash_ptrA = 0x00; // Dummy write to erase Flash seg A
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
*Flash_ptrA = 1;//改变存入flash中的数据,下一次重启进入case 1:
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit
while(1)
{
P1OUT ^= 0x01; // Toggle LED 慢闪
for (i = 0; i < 0x4000; i++); // SW Delay
}
case 1://第二次以后默认进入
while(1)
{
P1OUT ^= 0x01; // Toggle LED 快闪
for (i = 0; i < 0x1000; i++); // SW Delay
}
}
}