📄 flash.c
字号:
#include <msp430x13x.h>
unsigned int flash_data_buff[10];
void write_flash ();
void read_flash (void);
void flash_init(void);
extern void clr_wdt(void); //复位看门狗
void flash_init(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
FCTL2 = FWKEY + FSSEL0 + FN0; // MCLK/2 for Flash Timing Generator
}
void write_flash (void)
{ unsigned char temp3;
unsigned int temp;
unsigned char temp2;
unsigned char *Flash_ptr; // Flash pointer
unsigned char i;
Flash_ptr = (unsigned char *) 0x1000; // 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
for (i=0; i<10; i++)
{ clr_wdt();
temp=flash_data_buff[i];
temp3=temp;
temp2=(temp>>8);
*Flash_ptr++ = temp2; // Write value to flash
*Flash_ptr++ = temp3; // Write value to flash
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Reset LOCK bit
}
void read_flash (void)
{
unsigned int temp1,temp2;
char *Flash_ptrA; // Segment A pointer
unsigned int i;
Flash_ptrA = (char *) 0x1000; // Initialize Flash segment A pointer
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
for (i=0; i<10; i++)
{ temp1= *Flash_ptrA++; // copy value segment A to segment B
temp2= *Flash_ptrA++; // copy value segment A to segment B
flash_data_buff[i] = (temp2+(temp1<<8));
clr_wdt();
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Reset LOCK bit
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -