MSP430系列單片機(jī)-讀RAM數(shù)據(jù)燒寫(xiě)到Flash中
題目一
從RAM地址0x0200中讀取一個(gè)long數(shù)據(jù),并燒寫(xiě)至地址為0x1800的Flash中。
代碼:
#include"io430.h"
void main(void)
{
//初始化RAM信息段的指針
unsigned long * RAM_ptr = (unsigned long *) 0x0200;
//初始化Flash信息段的指針
unsigned long * Flash_ptr = (unsigned long *) 0x1800;
//存放數(shù)據(jù)的變量
unsigned long value;
WDTCTL = WDTPW + WDTHOLD; //關(guān)閉看門(mén)狗
P4DIR |= BIT2; //設(shè)置P4.2為輸出端口,連接LED作為指示
while(FCTL3 & BUSY); //判斷是否處于忙碌狀態(tài)
FCTL3 = FWKEY; //清除LOCK標(biāo)志
FCTL1 = FWKEY + ERASE; //選擇段擦除,置位ERASE
*Flash_ptr = 0; //將指針?biāo)赶虻牡刂穼?xiě)0,啟動(dòng)擦除
FCTL1 = FWKEY + BLKWRT; //寫(xiě)允許,長(zhǎng)字
//讀RAM信息到value變量
value = *RAM_ptr;
*Flash_ptr = value; //寫(xiě)Flash
while(FCTL3 & BUSY); //判斷是否處于忙碌狀態(tài)
FCTL1 = FWKEY; //清楚WRT位
FCTL3 = FWKEY + LOCK; //置位LOCK標(biāo)志
while(1)
{
P4OUT ^= BIT2; //燒寫(xiě)完成,點(diǎn)亮綠指示燈
__delay_cycles(1000000);
}
}
題目二
從RAM地址0x0200開(kāi)始讀取一個(gè)長(zhǎng)度為8的字符數(shù)組,并燒寫(xiě)至地址為0x1800的Flash中。
#include"io430.h"
# define NumToWrite 8
void main(void)
{
//初始化RAM信息段的指針
unsigned char * RAM_ptr = (uint8_t *) 0x0200;
//初始化Flash信息段的指針
unsigned char * Flash_ptr = (uint8_t *) 0x1800;
//存放數(shù)據(jù)的變量
unsigned char value[NumToWrite];
int i;
WDTCTL = WDTPW + WDTHOLD; //關(guān)閉看門(mén)狗
P4DIR |= BIT2; //設(shè)置P4.2為輸出端口,連接LED作為指示
while(FCTL3 & BUSY); //判斷是否處于忙碌狀態(tài)
FCTL3 = FWKEY; //清除LOCK標(biāo)志
FCTL1 = FWKEY + ERASE; //選擇段擦除,置位ERASE
*Flash_ptr = 0; //將指針?biāo)赶虻牡刂穼?xiě)0,啟動(dòng)擦除
FCTL1 = FWKEY + BLKWRT; //寫(xiě)允許,長(zhǎng)字
//讀RAM信息到value變量
for (i=0;i value[i] = *RAM_ptr++; //將數(shù)組寫(xiě)入Flash for (i=0;i *Flash_ptr++ = value[i]; while(FCTL3 & BUSY); //判斷是否處于忙碌狀態(tài) FCTL1 = FWKEY; //清楚WRT位 FCTL3 = FWKEY + LOCK; //置位LOCK標(biāo)志 while(1) { P4OUT ^= BIT2; //燒寫(xiě)完成,點(diǎn)亮綠指示燈 __delay_cycles(1000000); } } 仿真圖: