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

📄 flash_eeprom_api.c

📁 C51 API 例子源码
💻 C
字号:
/*C***********************************************************************
* NAME:         flash_eeprom_api.c
*-------------------------------------------------------------------------
* Copyright (c) 2004 Atmel.
*-------------------------------------------------------------------------
* RELEASE:            
* REVISION:     1.0     
*-------------------------------------------------------------------------
* PURPOSE: 
* Read/Write flash
* CAUTTION : add #define ONCHIP_EEPROM for on-chip eeprom products
*            (defined by default in the standard delivery)
**************************************************************************/

/*_____ I N C L U D E - F I L E S _______________________________________*/
#include "reg_C51.h"

/*_____ D E C L A R A T I O N ___________________________________________*/
#define ONCHIP_EEPROM //define it only for on-chip eeprom products
/*---- API for FLASH access  --------------------------------------------*/
/*************************************************************************/
#define __api_rd_code_byte(address)  (*((unsigned char code*) (address)))
unsigned char	__api_wr_code_byte	(int , unsigned char)small;
unsigned char	__api_wr_code_page	(int , int, unsigned char)small;
/*---- API for EEPROM access  -------------------------------------------*/
/*************************************************************************/
#ifdef ONCHIP_EEPROM
   void __api_wr_eeprom_byte(unsigned int adr, unsigned char value);
   unsigned char __api_rd_eeprom_byte(unsigned int adr);
#endif

/*_____ G L O B A L S ___________________________________________________*/
sfr16 DPTR   =   0x82;

/*_____ L O C A L S _____________________________________________________*/
#define MSK_AUXR1_ENBOOT	0x20
#define MSK_AUXR_M0				0x20
#define MAP_BOOT 			AUXR1 |= MSK_AUXR1_ENBOOT;
#define UNMAP_BOOT		AUXR1 &= ~MSK_AUXR1_ENBOOT;

/*_____ EXTERNAL - F U N C T I O N S - D E C L A R A T I O N ____________*/
extern void ASM_MOV_R1_A(void);
extern void __API_FLASH_ENTRY_POINT(void);

/*F***********************************************************************
* NAME: __api_wr_code_byte 
*-------------------------------------------------------------------------
* PARAMS:  
* 		int address : address to program   
* 		unsigned char value : data to write
* 		unsigned char return : 
*             return  = 0x00 -> pass                            
*             return != 0x00 -> fail
*-------------------------------------------------------------------------
* PURPOSE: 
*  Program data byte in Flash memory 
**************************************************************************/
unsigned char __api_wr_code_byte (int address, unsigned char value) small
{
  bit ea_save;
  ea_save = EA;
  EA = 0;
  DPTR = address;    
  ACC = 0x02;
  ASM_MOV_R1_A();
  ACC = value;  
	MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
	UNMAP_BOOT;
  EA = ea_save;    	// restore interrupt state
  return (ACC); 
}

/*F***********************************************************************
* NAME: __api_wr_code_page 
*-------------------------------------------------------------------------
* PARAMS:  
* 		int add_flash : address of the first byte to program in the Flash
* 		int add_xram  : address in XRAM of the first data to program
* 		unsigned char nb_data : number of bytes to program
*			unsigned char return : 
*           return = 0x00 -> pass                           
*           return != 0x00 -> fail 
*-------------------------------------------------------------------------
* PURPOSE: 
*  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.               
**************************************************************************/
unsigned char __api_wr_code_page (int add_flash, int add_xram, unsigned char nb_data) small
{
  unsigned char save_auxr1;
  bit ea_save;
  ea_save = EA;
  EA = 0;
  save_auxr1 = AUXR1;
  AUXR1 &= ~0x01;        	// Set DPTR	=	DPTR0
  DPTR = add_flash;
  AUXR1++;        				// DPTR = DPTR1
  DPTR = add_xram;
  ACC = 0x09;
  ASM_MOV_R1_A();
  ACC = nb_data;
  AUXR1 &= ~0x01;        	// Set DPTR = DPTR0
  MAP_BOOT
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
  AUXR1 = save_auxr1;        		
  EA = ea_save;    	// restore interrupt state
return (ACC);
}

/*F***********************************************************************
* NAME: __api_rd_eeprom_byte
*-------------------------------------------------------------------------
* PARAMS:
* unsigned int adr: The EEDATA memory location to read
* return: value 
*-------------------------------------------------------------------------
* PURPOSE: 
* This function reads one byte in the on-chip EEPROM data.
*-------------------------------------------------------------------------
* EXAMPLE:
* val=__api_rd_eeprom_byte(128);
*-------------------------------------------------------------------------
* NOTE: 
*-------------------------------------------------------------------------
* REQUIREMENTS: 
**************************************************************************/
unsigned char __api_rd_eeprom_byte(unsigned int adr)
{
  unsigned char val;
  bit ea_save;
  while (EECON&1);//Eeprom_busy()
  ea_save=EA;
  EA=0;
  EECON |= 0x02;//Enable eeprom data;
  val=*(unsigned char xdata*)adr;
  EECON &= ~0x02;//Disable eeprom data;
  EA=ea_save;
  return  val;
}

/*F***********************************************************************
* NAME: __api_wr_eeprom_byte
*-------------------------------------------------------------------------
* PARAMS:
* unsigned int adr: The EEDATA memory location to read
* unsigned char value: The data byte to write 
* return:   none
*-------------------------------------------------------------------------
* PURPOSE: 
*  This function writes one byte in the on-chip EEPROM data.
*-------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------
* NOTE: 
*-------------------------------------------------------------------------
* REQUIREMENTS: 
**************************************************************************/
void __api_wr_eeprom_byte(unsigned int adr, unsigned char value)
{
  bit ea_save;
  while(EECON & 0x01);// wait bit busy
  ea_save=EA;
  EA=0;
  EECON |= 0x02;//Enable eeprom data
  *(unsigned char xdata*)adr=value;
  EECON &= ~0x02;//Disable eeprom data
  EA=ea_save;
}

⌨️ 快捷键说明

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