www.pudn.com > EEPROM.rar > EEPROM.c, change:2013-04-25,size:7537b


/****************************************Copyright (c)**************************************************** 
**                                          RFWORLD 
** 
**                                    http://www.mcurf.com 
** 
**      无线方案提供商,定制无线方案,无线模块,数字FSK无线模块,无线芯片。致力于提供 169M, 315M,433M,868M, 
**      915M,2.4G ,5.8G 的 数字无线模块,透明传输模块(USB,UART,485,232),DMX512 模块,蓝牙4.0模块,WIFI 模块, 
**      GPRS 模块,ZIGBEE 模块,IPHONE IPAD 连接方案,安卓手机连接方案, 基于(TI, Siliconlib,Integration, Nodic,  
**      Chipcon, Ember , Atmel,Axsem,..etc) 芯片的推广和无线方案相关应用。 
**                                                                        ——深圳市阅天信息技有限公司 
** 
**--------------File Info--------------------------------------------------------------------------------- 
** File name:               EEPROM.c 
** Last modified date:      2013-04-23 
** Last version:            V1.0 
** Descriptions:            EEPROM程序 
** 
**-------------------------------------------------------------------------------------------------------- 
** Created by:              Mo ZhenTeng 
** Created date:            2013-04-23 
** Version:                 V1.0 
** Descriptions:            The original version 初始版本 
**-------------------------------------------------------------------------------------------------------- 
** Modified by:         
** Modified date:       
** Version:             
** Descriptions:        
** 
*********************************************************************************************************/ 
 
/********************************************************************* 
 * INCLUDES 
 */ 
#include "SM39R08A5.h" 
#include <absacc.h>    /* Include Macro Definitions */ 
#include "EEPROM.h" 
 
/********************************************************************************************************* 
  宏定义 
*********************************************************************************************************/ 
//ISPFC.ISPF[2:0] 
#define d_Command_Byte_Program	0 
#define d_Command_Page_Erase	2 
 
#define d_DATALEN	64				// idata limit:1~128 
 
/********************************************************************************************************* 
** EEPROM全局变量 
*********************************************************************************************************/ 
unsigned char idata buf[d_DATALEN];	// idata limit:1~128 
 
/********************************************************************************************************* 
** Function name:           EEPROM_Enable 
** Descriptions:            使能EEPROM函数 
** input parameters:        NONE 
** Output parameters:       NONE 
** Returned value:          NONE 
*********************************************************************************************************/ 
void EEPROM_Enable(void) 
{ 
	unsigned char eep_retry=255; 
	while((IFCON & 0x01)==0x00) 
	{ 
		TAKEY  = 0x55; 
		TAKEY  = 0xAA; 
		TAKEY  = 0x5A; 
		IFCON |= 0x01;		// ISPE=1, Enable ISP function 
		 
		eep_retry--; 
		if(eep_retry==0) break; 
	} 
} 
 
/********************************************************************************************************* 
** Function name:           EEPROM_Disable 
** Descriptions:            禁止EEPROM函数 
** input parameters:        NONE 
** Output parameters:       NONE 
** Returned value:          NONE 
*********************************************************************************************************/ 
void EEPROM_Disable(void) 
{ 
	unsigned char eep_retry=255; 
	while((IFCON & 0x01)==0x01) 
	{ 
		TAKEY  = 0x55; 
		TAKEY  = 0xAA; 
		TAKEY  = 0x5A; 
		IFCON &= 0xFE;		// ISPE=0, Disable  ISP function 
		 
		eep_retry--; 
		if(eep_retry==0) break; 
	} 
} 
 
/********************************************************************************************************* 
** Function name:           EEPROM_Byte_Program 
** Descriptions:            写单个字节函数 
** input parameters:        Addr:16bit地址码  	Data:有效字节 
** Output parameters:       NONE 
** Returned value:          NONE 
*********************************************************************************************************/ 
void EEPROM_Byte_Program(unsigned int Addr, unsigned char Data) 
{ 
	EEPROM_Enable(); 
	ISPFD=Data; 
	ISPFAH=(Addr/256); 
	ISPFAL=(Addr%256); 
	ISPFC=d_Command_Byte_Program; 
	EEPROM_Disable(); 
} 
 
/********************************************************************************************************* 
** Function name:           EEPROM_Page_Erase 
** Descriptions:            页擦除函数,一页有128字节 
** input parameters:        Addr:16bit地址码 
** Output parameters:       NONE 
** Returned value:          NONE 
*********************************************************************************************************/ 
void EEPROM_Page_Erase(unsigned int Addr)		// erase 128-Byte 
{ 
	EEPROM_Enable(); 
	ISPFAH=(Addr/256); 
	ISPFAL=(Addr%256);	 
	ISPFC=d_Command_Page_Erase;  
	EEPROM_Disable(); 
} 
 
/********************************************************************************************************* 
** Function name:           EEPROM_Sector_Program 
** Descriptions:            写多字节函数 
** input parameters:        Addr:16bit地址码	  len:长度  Data:数据缓冲区指针 
** Output parameters:       NONE 
** Returned value:          NONE 
*********************************************************************************************************/ 
void EEPROM_Sector_Program(unsigned int Addr, unsigned int len, unsigned char *Data) 
{ 
	unsigned int i; 
	for(i=0; i<=len; i++) 
	{ 
		if((i+Addr) % 128 == 0) 
		{ 
		 	EEPROM_Page_Erase(i+Addr); 
		} 
		EEPROM_Byte_Program(i+Addr, Data[i]); 
	} 
} 
 
/********************************************************************************************************* 
** Function name:           EEPROM_Byte_Read 
** Descriptions:            读取单个字节函数 
** input parameters:        Addr:16bit地址码 
** Output parameters:       NONE 
** Returned value:          CBYTE[Addr] 
*********************************************************************************************************/ 
unsigned char EEPROM_Byte_Read(unsigned int Addr) 
{ 
	return CBYTE[Addr];			//The range of valid index values for this macro is 0-255 
} 
 
/*unsigned int EEPROM_Word_Read(unsigned int Addr) 
{ 
	return CWORD[Addr];			//The range of valid index values for this macro is 0-65535 
}*/ 
 
/********************************************************************************************************* 
** Function name:           EEPROM_Byte_Modify 
** Descriptions:            修改单个字节函数 
** input parameters:        Addr:16bit地址码	 Data:修改数据缓冲区指针 
** Output parameters:       NONE 
** Returned value:          NONE 
*********************************************************************************************************/ 
void EEPROM_Byte_Modify(unsigned int Addr, unsigned char Data) 
	{ 
	unsigned char i; 
	 
	for(i=0; i<d_DATALEN; i++) 
		buf[i] = EEPROM_Byte_Read((Addr&0xFF00)+i);	// page read 
 
	EEPROM_Page_Erase(Addr);						// erase page  
	 
	buf[(unsigned char)Addr] = Data;				// byte modify 
 
	for(i=0; i<d_DATALEN; i++) 
		EEPROM_Byte_Program((Addr&0xFF00)+i, buf[i]);// page program		 
} 
/********************************************************************************************************* 
  END FILE  
*********************************************************************************************************/