⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash_f300.c

📁 C8051系列关于flash编程C原代码
💻 C
字号:
//-----------------------------------------------------------------------------
// FLASH_F30x.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: BW / FB
// DATE: 20 MAY 02
//
// This program shows an example of how to read, erase, and write FLASH
// memory on an 'F30x device from application code.
//
// Target: C8051F30x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <C8051F300.h>                 // SFR declarations

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F30x
//-----------------------------------------------------------------------------

sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR2RL   = 0xca;                 // Timer2 reload value
sfr16 TMR2     = 0xcc;                 // Timer2 counter
sfr16 PCA0CP1  = 0xe9;                 // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP2  = 0xeb;                 // PCA0 Module 2 Capture/Compare
sfr16 PCA0     = 0xf9;                 // PCA0 counter
sfr16 PCA0CP0  = 0xfb;                 // PCA0 Module 0 Capture/Compare

//-----------------------------------------------------------------------------
// 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[] = "Howdy!";

   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 + -