📄 flash_f020.c
字号:
//-----------------------------------------------------------------------------
// FLASH_F020.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: BW / FB
// DATE: 3 JUNE 02
//
// This program shows an example of how to read, erase, and write FLASH
// memory on an 'F02x device from application code.
//
// Target: C8051F02x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F020.h> // SFR declarations
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
unsigned char xdata * idata 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 stored in FLASH
unsigned char code test_string[] = "Howdy!";
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
// PART A -- erase the FLASH page at 0x1000
// initialize write/erase pointer
pwrite = (unsigned char xdata *) 0x1000;
EA_save = EA; // save interrupt status
EA = 0; // disable interrupts (precautionary)
FLSCL |= 0x01; // enable FLASH writes/erases from
// user software
PSCTL = 0x03; // MOVX writes erase FLASH page
*pwrite = 0; // initiate page erase
PSCTL = 0; // MOVX writes target XRAM
FLSCL &= ~0x01; // disable FLASH writes/erases from
// user software
EA = EA_save; // re-enable interrupts
// PART B -- copy test string to FLASH memory at address 0x1000
// initialize FLASH read pointer
pread = (unsigned char code *) test_string;
EA_save = EA; // save interrupt status
EA = 0; // disable interrupts (precautionary)
pwrite = 0x1000; // initialize FLASH write pointer
FLSCL |= 0x01; // enable FLASH writes/erases from
// user software
PSCTL = 0x01; // MOVX writes target FLASH memory
while (*pread != '\0') { // copy until NULL is detected
*pwrite = *pread; // copy byte
pread++; // advance pointers
pwrite++;
}
*pwrite = '\0'; // NULL-terminate string
PSCTL = 0x00; // MOVX writes target XRAM
FLSCL &= ~0x01; // disable FLASH writes/erases from
// user software
EA = EA_save; // re-enable interrupts
// PART C -- erase the scratchpad area of FLASH
// initialize write/erase pointer to any location in the scratchpad
pwrite = (unsigned char xdata *) 0x0000;
EA_save = EA; // save interrupt status
EA = 0; // disable interrupts (precautionary)
FLSCL |= 0x01; // enable FLASH writes/erases from
// user software
PSCTL = 0x07; // MOVX writes erase FLASH page
// (SFLE set directing FLASH
// reads/writes/erases to the
// scratchpad memory)
*pwrite = 0; // initiate page erase
PSCTL = 0; // MOVX writes target XRAM
// (SFLE is cleared)
FLSCL &= ~0x01; // disable FLASH writes/erases from
// user software
EA = EA_save; // re-enable interrupts
// PART D -- copy test string to the scratchpad page in FLASH
// initialize FLASH read pointer
pread = (unsigned char code *) test_string;
// initialize FLASH write pointer to the beginning of the scratchpad
pwrite = 0x0000;
EA_save = EA; // save interrupt status
EA = 0; // disable interrupts (precautionary)
FLSCL |= 0x01; // enable FLASH writes/erases from
// user software
PSCTL = 0x01; // MOVX writes target FLASH memory
while (*pread != '\0') { // copy until NULL is detected
PSCTL |= 0x04; // set SFLE
*pwrite = *pread; // copy byte
PSCTL &= ~0x04; // clear SFLE
pread++; // advance pointers
pwrite++;
}
PSCTL |= 0x04; // set SFLE
*pwrite = '\0'; // NULL-terminate string
PSCTL &= ~0x04; // clear SFLE
PSCTL = 0x00; // MOVX writes target XRAM
FLSCL &= ~0x01; // disable FLASH writes/erases from
// user software
EA = EA_save; // re-enable interrupts
while (1); // spin forever
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -