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

📄 flash_eeprom_api.c

📁 usb sample progeamme forAT89c5131
💻 C
字号:
/*C***********************************************************************
* NAME:         flash_eeprom_api.c
*-------------------------------------------------------------------------
* Copyright (c) 2004 Atmel.
*-------------------------------------------------------------------------
* RELEASE:            
* REVISION:          
*-------------------------------------------------------------------------
* PURPOSE: 
* This file contains whole of functions to access AT89C5131 Flash and
* EEPROM and AT89C51SND1.
* CAUTTION : add #define ONCHIP_EEPROM for on-chip eeprom products
*            (defined by default in the standard delivery)
**************************************************************************/

/*_____ I N C L U D E S _________________________________________________*/
#include "reg_C51.h"
/*_____ M A C R O S _____________________________________________________*/

/*_____ D E F I N I T I O N _____________________________________________*/
#define ONCHIP_EEPROM //define it only for build-in eeprom chips

unsigned char  data api_command     _at_ 0x1C;
unsigned char  data api_value       _at_ 0x1D;
 
#define MSK_AUXR1_ENBOOT    0x20
#define MAP_BOOT            AUXR1 |= MSK_AUXR1_ENBOOT;
#define UNMAP_BOOT          AUXR1 &= ~MSK_AUXR1_ENBOOT;

#define __API_FLASH_ENTRY_POINT    (*((const void(code*)(void)) 0xFFC0 ))

/*_____ D E C L A R A T I O N ___________________________________________*/
/*---- API for FLASH access  --------------------------------------------*/
/*************************************************************************/
unsigned char __api_rd_code_byte (unsigned char code * pt_address);
unsigned char __api_wr_code_byte (unsigned char xdata* , unsigned char);
unsigned char __api_wr_code_page (unsigned char xdata* pt_code, 
                                  unsigned char xdata* pt_xram, 
                                  unsigned char nb_data);
/*---- API for EEPROM access  -------------------------------------------*/
/*************************************************************************/
#ifdef ONCHIP_EEPROM
  unsigned char __api_rd_eeprom_byte(unsigned char xdata *);
  unsigned char __api_wr_eeprom_byte(unsigned char xdata *, unsigned char);
#endif

/*F***********************************************************************
* NAME: __api_rd_code_byte 
*-------------------------------------------------------------------------
* PARAMS:
* unsigned int address : address in flash memory to read    
* return: 
* unsigned char device : read value
*-------------------------------------------------------------------------
* PURPOSE: 
* This function allows to read a flash memory byte.
*-------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------
* NOTE:
*-------------------------------------------------------------------------
* REQUIREMENTS: 
**************************************************************************/
unsigned char __api_rd_code_byte (unsigned char code * pt_address)
{
  return(*pt_address);
}

/*F***********************************************************************
* NAME: __api_wr_code_byte 
*-------------------------------------------------------------------------
* PARAMS:
* unsigned int address : address to program
* unsigned char value : data to write   
* return: 
* unsigned char return : 
*       return  = 0x00 -> pass                            
*       return != 0x00 -> fail
*-------------------------------------------------------------------------
* PURPOSE: 
* This function allows to program data byte in Flash memory.
*-------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------
* NOTE:
*-------------------------------------------------------------------------
* REQUIREMENTS: 
**************************************************************************/
unsigned char __api_wr_code_byte (unsigned char xdata * pt_address, 
                                  unsigned char value)
{
  bit ea_save;

  ea_save = EA;
  EA = 0;
  api_command = 0x0D; //_COMMAND_WR_CODE_BYTE;
  FCON = 0x08;

  *pt_address = value;

  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
  EA = ea_save;        // restore interrupt state

  return(api_value);
}


/*F***********************************************************************
* NAME: __api_wr_code_page 
*-------------------------------------------------------------------------
* PARAMS:
* unsigned int add_flash : address of the first byte to program 
*                          in the Flash
* unsigned int add_xram  : address in XRAM of the first data to program
* unsigned char nb_data : number of bytes to program
* return: 
* unsigned char return : 
*       return  = 0x00 -> pass                            
*       return != 0x00 -> fail
*-------------------------------------------------------------------------
* PURPOSE: 
* This function allows to program until 128 Datas in Flash memory.
* Number of bytes to program is limited such as the Flash write remains 
* in a single 128 bytes page. 
*-------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------
* NOTE:
* This function used Dual Data Pointer DPTR0&1. At the end of this 
* function. 
* DPTR = DPTR0.
*-------------------------------------------------------------------------
* REQUIREMENTS: 
**************************************************************************/
unsigned char __api_wr_code_page (unsigned char xdata * pt_code, 
                                  unsigned char xdata * pt_xram,
                                  unsigned char nb_data)
{
  unsigned char data i, temp, temp_nb_data;
  bit ea_save;
  unsigned int  data add_pt_code, add_pt_xram;

  add_pt_xram = pt_xram;
  add_pt_code = pt_code;
  temp_nb_data = nb_data;
  ea_save = EA;
  EA = 0;
  api_command = 0x0D;
  for (i=0 ; i< temp_nb_data; i++,add_pt_xram++,add_pt_code++)
     {
        temp = *(unsigned char xdata *)add_pt_xram;
        FCON = 0x08;
        *(unsigned char xdata *)add_pt_code = temp;
        FCON = 0x00;
     }

  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
  EA = ea_save;        // restore interrupt state
    
  return(api_value);
}

#ifdef ONCHIP_EEPROM
/*F***********************************************************************
* NAME: api_rd_eeprom 
*-------------------------------------------------------------------------
* PARAMS:
* unsigned char xdata *address : address to read
* return: 
*-------------------------------------------------------------------------
* PURPOSE: 
* This function allows to read a byte in Eeprom.
*-------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------
* NOTE:
*-------------------------------------------------------------------------
* REQUIREMENTS: The EEPROM mustn't be busy to perform the read access.
*               eeprom status :(EECON & 0x01)=1 busy, =0 free
**************************************************************************/
unsigned char __api_rd_eeprom_byte(unsigned char xdata *address)
{  
  unsigned char val;
  bit ea_save;

  ea_save = EA;
  EA = 0;
  EECON = 0x02;
  val   = *address;  
  EECON = 0x00;
  EA = ea_save;
  return (val);
}


/*F***********************************************************************
* NAME: api_wr_eeprom_byte 
*-------------------------------------------------------------------------
* PARAMS:
* unsigned char xdata* address : address to read
* unsigned char value : data to write
* return: 
*-------------------------------------------------------------------------
* PURPOSE: 
* This function allows to program a byte in Eeprom.
*-------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------
* NOTE:
*-------------------------------------------------------------------------
* REQUIREMENTS: The EEPROM mustn't be busy to perform the read access.
*               eeprom status :(EECON & 0x01)=1 busy, =0 free
**************************************************************************/
unsigned char __api_wr_eeprom_byte (unsigned char xdata *address, 
                                    unsigned char value)
{
  bit ea_save;
  while(EECON & 0x01);// wait bit busy
  ea_save = EA;
  EA = 0;
  EECON = 0x02;
  *address = value;/* addr is a pointer to external data mem */
  EECON = 0x50;          
  EECON = 0xA0;
  EA = ea_save;
          
    return (1);
}
#endif


⌨️ 快捷键说明

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