📄 rwflash.c
字号:
//============================================================================
//该程序执行片内FLASH的读写操作,将0-9十个数写入到起始地址为0X1000的FLASH空间中.
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f310.h> // SFR declarations
#include <intrins.h>
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F31x
//-----------------------------------------------------------------------------
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[] = "01234567890!";
RSTSRC |= 0x02; // enable the VDD monitor
// Disable Watchdog timer
PCA0MD &= ~0x40; // 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 != '\0') { // copy until NULL is detected
FLKEY = 0xA5; // FLASH lock and key sequence 1
FLKEY = 0xF1; // FLASH lock and key sequence 2
*pwrite = *pread; // copy byte
pread++; // advance pointers
pwrite++;
}
FLKEY = 0xA5; // FLASH lock and key sequence 1
FLKEY = 0xF1; // FLASH lock and key sequence 2
*pwrite = '\0'; // NULL-terminate string
PSCTL = 0x00; // MOVX writes target XRAM
EA = EA_save; // re-enable interrupts
while (1) { // spin forever
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -