flash_f000.c

来自「ad转换的程序」· C语言 代码 · 共 87 行

C
87
字号
//-----------------------------------------------------------------------------
// FLASH_F000.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 'F00x/'F01x/'F2xx device from application code.
//
// Target: C8051F00x/'F01x/'F2xx
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//

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

#include <C8051F000.h>                 // SFR declarations
//#include <C8051F200.h>                 // SFR declarations

//-----------------------------------------------------------------------------
// 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
   unsigned char code test_string[] = "Howdy!";
   
   // disable watchdog timer
   WDTCN = 0xde;                       
   WDTCN = 0xad;

   // erase the FLASH page at 0x1000
   EA_save = EA;
   EA = 0;                             // disable interrupts (precautionary)

   // initialize write/erase pointer
   pwrite = (unsigned char xdata *) 0x1000;
   
   FLSCL = 0x86;                       // set FLASH scale register for
                                       // 2MHz sytem clock
   PSCTL = 0x03;                       // MOVX writes erase FLASH page

   *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

      *pwrite = *pread;                // copy byte

      pread++;                         // advance pointers
      pwrite++;
   }

   *pwrite = '\0';                     // NULL-terminate string

   PSCTL = 0x00;                       // MOVX writes target XRAM
   
   FLSCL = 0x0F;                       // disable FLASH writes/erases 
                                          

   EA = EA_save;                       // re-enable interrupts

   while (1) {                         // spin forever
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?