51 EEPROM操作模板

各个型号容量及扇区请查datasheet


 

#include <reg52.h>

#include "intrins.h"



typedef unsigned char byte;

typedef unsigned int word;



/* Declare SFR associated with the IAP */

sfr IAP_DATA  = 0xC2;  //Flash data register

sfr IAP_ADDRH = 0xC3;  //Flash address HIGH

sfr IAP_ADDRL = 0xC4;  //Flash address LOW

sfr IAP_CMD   = 0xC5;  //Flash command register

sfr IAP_TRIG  = 0xC6;  //Flash command trigger

sfr IAP_CONTR = 0xC7;  //Flash control register



/* Define ISP/IAP/EEPROM command */

#define CMD_IDLE 0	   //Stand-By

#define CMD_READ 1	   //Byte-Read

#define CMD_PROGRAM 2  //Byte-Program

#define CMD_ERASE 3	   //Sector-Erase



/* Define ISP/IAP/EEPROM operation const for IAP_CONTR */

#define ENABLE_IAP 0x80	 //if SYSCLK < 30MHz

#define ENABLE_IAP 0x81	 //if SYSCLK < 24MHz

#define ENABLE_IAP 0x82	 //if SYSCLK < 20MHz

#define ENABLE_IAP 0x83	 //if SYSCLK < 12MHz

#define ENABLE_IAP 0x84	 //if SYSCLK < 6MHz

#define ENABLE_IAP 0x85	 //if SYSCLK < 3MHz

#define ENABLE_IAP 0x86	 //if SYSCLK < 2MHz

#define ENABLE_IAP 0x87	 //if SYSCLK < 1MHz



//Start address for STC12C5201AD series EEPROM

#define IAP_ADDRESS 0x0000



void Delay(byte n)

{

	word x;

	while(n--)

	{

		x = 0;

		while(++x);

	}

}



/* Disable ISP/IAP/EEPROM function Make MCU in a safe state */



void IapIdle()

{

	 IAP_CONTR = 0;		   //close IAP function

	 IAP_CMD = 0;		   //clear command to standby

	 IAP_TRIG = 0;		   //clear tirgger register

	 IAP_ADDRH = 0x80;	   //data ptr point to non EEPROM area

	 IAP_ADDRL = 0;		   //clear IAP address to prevent mususe

}



/* read one byte from isp/iap/eeprom area input:addr(isp iap eeprom address) output:flash data */

byte IapReadByte(word addr)

{

	byte dat;



	IAP_CONTR = ENABLE_IAP; //Open IAP function and set wait time

	IAP_CMD = CMD_READ;	    //Set ISAP READ command

	IAP_ADDRL = addr;       //Set ISP address low

	IAP_ADDRH = addr >> 8;  //Set IAP address high

	IAP_TRIG = 0x5a;        //Send trigger command1(0x5a)

	IAP_TRIG = 0xa5;        //Send trigger command2(0xa5)

	_nop_();				//MCU will hold here until IAP operation complete



	dat = IAP_DATA; 		//Read IAP EEPROM data

	IapIdle();				//Close ISP function



	return dat;				//Return Flash data

}



/* Program one byte to ISP area   Input:addr (address)  dat(data)  Output:- */

void IapProgramByte(word addr, byte dat)

{

	IAP_CONTR = ENABLE_IAP;		//Open IAP function, and set wait time

	IAP_CMD   = CMD_PROGRAM;    //Set EEPROM PROGRAM command

	IAP_ADDRL = addr;

	IAP_ADDRH = addr >> 8;

	IAP_DATA = dat;

	IAP_TRIG = 0x5a;

	IAP_TRIG = 0xa5;

	_nop_();



	IapIdle();

}



/* Erase one sector area Input:addr */

void IapEraseSector(word addr)

{

	IAP_CONTR = ENABLE_IAP;

	IAP_CMD = CMD_ERASE;

	IAP_ADDRL = addr;

	IAP_ADDRH = addr >> 8;

	IAP_TRIG = 0x5a;

	IAP_TRIG = 0xa5;

	_nop_();



	IapIdle();

}







int main()

{

	word i;



	P1 = 0xfe;

	Delay(10);

	IapEraseSector(IAP_ADDRESS);



	for(i = 0; i < 512; ++i)

	{

		if(IapReadByte(IAP_ADDRESS + i) != 0xff)

		{}	

	}



	P1 = 0xfc;

	Delay(10);

	for(i = 0; i < 512; ++i)

	{

		IapProgramByte(IAP_ADDRESS + i, (byte)i);

	}

	

	P1 = 0xf8;

	Delay(10);

	for(i = 0; i < 512; ++i)

	{

		if(IapReadByte(IAP_ADDRESS + i) != (byte)i)

		{}

	}



	P1 = 0xf0;

	while(1);



	return 0;

}


 

 

你可能感兴趣的:(ROM)