用仿真器调试信息段的时候要特别主要将FET-Debugger Download-Flash erase 选择成Erase main memory. 否则在重启FET的时候会擦除信息段里面的内容
//******************************************************************************
//******************************************************************************
#include
#define FLASH_ADDRESS 0x01000 //定义FLASH信息区地址B段
unsigned char write_buff[4]={0x01,0x03,0x04,0x09}; //用于存放被数组
unsigned char checkout[4]; //读出缓冲区,用于验证的
unsigned char num="0";
void flash_erase(unsigned int*);
void read_array(unsigned char *pc_byte, unsigned char *array,unsigned char amount);
void write_array(unsigned char *pc_byte,unsigned char *array,unsigned char amount);
//******************************************************************************
//FLASH段擦除
void flash_erase(unsigned int *pc_word)
{
while(FCTL3 & BUSY); //如果处于忙状态,则等待
FCTL3 = FWKEY ; //清出LOCK标志,解锁
FCTL1 = FWKEY + ERASE ; //允许段擦除
*pc_word = 0; //擦除..擦除..
while(FCTL3 & BUSY);
FCTL3 = FWKEY + LOCK ; //加锁
}
//******************************************************************************
//向FLASH信息区读出指定数量的字节数据
//unsigned int*pc_word :信息区数据指针
//unsigned char *array :读出数据存放数据数组,8位长
//unsigned char amount :读操的数量,范围0-127
void read_array(unsigned char *pc_byte, unsigned char *array,unsigned char amount)
{ unsigned char i;
if(amount<=127)
{
for(i=0;i {
*array = *pc_byte; //读数据,读数据时,flash地址自动加 1
array++; //接收缓冲区地址加 1
}
}
}
//******************************************************************************
//向FLASH信息区写入指定数量的字节数据
//unsigned char *pc_byte 信息区数据指针
//unsigned char *array :读出数据存放数据数组,8位长
//unsigned char amount :读操的数量,范围0-127
void write_array(unsigned char *pc_byte,unsigned char *array, unsigned char amount)
{ unsigned char i;
while(FCTL3 & BUSY); //如果处于忙状态,则等待
FCTL3 = FWKEY ; //清出LOCK标志
FCTL1 = FWKEY + WRT ; //写操作,块编程,+ BLKWRT;
if(amount<=127)
{
for(i=0;i {
*pc_byte = *array;
//*pc_byte = num;
// num +=1;
array++; //发送缓冲区地址加 1
pc_byte++; //写flash时,地址人为加 1
while(!(FCTL3 & WAIT)); //如果处于忙状态,则等待 ,若用软件仿真,去掉 //这语句
}
}
FCTL1 = FWKEY; //写操作完成,清除编程允许位 WRT,BLKWRT
while(FCTL3 & BUSY);
FCTL3 = FWKEY + LOCK;
}
//******************************************************************************
void main(void)
{
unsigned char *pc_flash; //定义字节指针变量
unsigned int *pc_flash_segment; //定义段地址指针变量
WDTCTL = WDTPW + WDTHOLD;
FCTL2 = FWKEY + FSSEL_1 + FN1; //工作频率设为 MCLK/3,333.3KHZ
_DINT();
pc_flash = (unsigned char *) FLASH_ADDRESS; //为指针初始化
pc_flash_segment = (unsigned int *) FLASH_ADDRESS;
//---------------------------------------------------
while(1)
{
flash_erase( pc_flash_segment); //段擦除
write_array(pc_flash, write_buff,4); //写入指定字节数量
read_array(pc_flash,&checkout[0],4); //再读出刚才写的字节
_NOP();
}
/*for (;;)
{
//P1OUT ^= 0x01;
_BIS_SR(LPM3_bits); // Enter LPM3
_NOP(); // Required only for C-spy
}*/
}
// Basic Timer interrupt service routine
#pragma vector="BASICTIMER"_VECTOR
__interrupt void BT_ISR(void)
{
P1OUT ^= 0x01; // Toggle P5.1 using exclusive-OR
}
文章评论(0条评论)
登录后参与讨论