郭天祥MSP430教程源码-FLASH.zip
时间:10-27 14:48
查看:1385次
下载:162次
简介:
有关郭天祥MSP430教程源码-FLASH实验程序设计。
#include <msp430x14x.h>
#include "BoardConfig.h"
uchar value; // 8-bit value to write to segment A
uchar DataBuffer[128];
// Function prototypes
void write_SegA (uchar value);
void copy_A2B (void);
void main(void)
{
BoardConfig(0xb8);
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
FCTL2 = FWKEY + FSSEL0 + FN0; // MCLK/2 for Flash Timing Generator
value = 0; // Initialize value
while(1) // Repeat forever
{
write_SegA(value++); // Write segment A, increment value
copy_A2B(); // Copy segment A to B
_NOP(); // SET BREAKPOINT HERE
}
}
void write_SegA (uchar value)
{
uchar *Flash_ptr; // Flash pointer
uint i;
Flash_ptr = (uchar *) 0x1080; // 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<128; i++)
{
*Flash_ptr++ = value; // Write value to flash
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
void copy_A2B (void)
{
uchar *Flash_ptrA; // Segment A pointer
uchar *Flash_ptrB; // Segment B pointer
uint i;
Flash_ptrA = (uchar *) 0x1080; // Initialize Flash segment A pointer
Flash_ptrB = (uchar *) 0x1000; // Initialize Flash segment B pointer
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptrB = 0; // Dummy write to erase Flash segment B
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
for (i=0; i<128; i++)
{
DataBuffer[i] = *Flash_ptrA++;
*Flash_ptrB++ = DataBuffer[i]; // Copy value segment A to segment B
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}