//============================================================================
//该程序执行片内FLASH的读写操作,将0-9十个数写入到起始地址为0X1000的FLASH空间中.
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f410.h> // Header file for SiLabs c8051f320
// Header file for USB_API.lib
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F32x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 Capture/Compare
sfr16 PCA0CP1 = 0xe9; // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP2 = 0xeb; // PCA0 Module 2 Capture/Compare
sfr16 PCA0CP3 = 0xed; // PCA0 Module 3 Capture/Compare
sfr16 PCA0CP4 = 0xfd; // PCA0 Module 4 Capture/Compare
sfr16 PCA0 = 0xf9; // PCA0 counter
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
unsigned char xdata *pwrite; // pointer to FLASH used for writes
// NOTE: this pointer must be located
// in <data> or <idata> space!
unsigned char code *pread; // pointer to FLASH used for reads
char EA_save; // saves the current state of the
// interrupt enable bit.
// test string
unsigned char code test_string[] = "012345678901111111111111111111111111111111111111111111122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222223333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333344444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555666666666666666666666666666666666666666666666666666666666666666666666666677777777777777777777777777777777777777777777777777777777777777777777777777777777777777778888888888888888888888888888888888888888888888888888888888888888888888888899999999999999999999999999999999999999999999!";
PCA0MD &= ~0x40;//禁止看门狗定时器
OSCICN = 0x86;//时钟分24.5M的2分频12.25M
// Disable Watchdog timer
RSTSRC |= 0x02; // enable the VDD monitor
// Disable Watchdog timer // WDTE = 0 (clear watchdog timer
// enable)
// erase the FLASH page at 0x1000
EA_save = EA;
EA = 0; // disable interrupts (precautionary)
// initialize write/erase pointer
pwrite = (unsigned char xdata *) 0x1000;
PSCTL = 0x03; // MOVX writes erase FLASH page
FLKEY = 0xA5; // FLASH lock and key sequence 1
FLKEY = 0xF1; // FLASH lock and key sequence 2
*pwrite = 0; // initiate page erase
PSCTL = 0; // MOVX writes target XRAM
EA = EA_save; // re-enable interrupts
// copy a string to FLASH memory at address 0x1000
// initialize FLASH read pointer
pread = (unsigned char code *) test_string;
EA_save = EA;
EA = 0; // disable interrupts (precautionary)
pwrite = 0x1000; // initialize FLASH write pointer
PSCTL = 0x01; // MOVX writes target FLASH memory
while (*pread != ' ') // copy until NULL is detected
{
FLKEY=0xA5;//FLASHlockandkeysequence1
FLKEY=0xF1;//FLASHlockandkeysequence2
*pwrite=*pread;//copybyte
pread++;//advancepointers
pwrite++;
}
FLKEY = 0xA5; // FLASH lock and key sequence 1
FLKEY = 0xF1; // FLASH lock and key sequence 2
*pwrite = ' '; // NULL-terminate string
PSCTL = 0x00; // MOVX writes target XRAM
EA = EA_save; // re-enable interrupts
while (1)
{ // spin forever
}
}