Home     Projects     Micro     Tools     Delphi     Developer     Blog     Site map
HOME  
 
   

MCS-51 | PIC


Read/Write on-chip 2K EEPROM of AT89S8252

This is an example demonstration how to read On-chip 2K EEPROM of ATMEL AT89S8252 with KEIL C51.

As we know that there are On-chip 2K EEPROM in AT89S8252 which is helpfully for applications that need to store some data even power supplies disconnected.



The on-chip EEPROM data memory is selected by setting the EEMEN bit in the WMCON register at SFR address location 96H. The EEPROM address range is from 000H to
7FFH. The MOVX instructions are used to access the EEPROM. To access off-chip data memory with the MOVX instructions, the EEMEN bit needs to be set to “0”.
The EEMWE bit in the WMCON register needs to be set to “1” before any byte location in the EEPROM can be written. User software should reset EEMWE bit to “0” if no further EEPROM write is required. EEPROM write cycles in the serial programming mode are self-timed and typically take 2.5 ms. The progress of EEPROM write can be monitored by reading the RDY/BSY bit (read-only) in SFR WMCON. RDY/BSY = 0 means programming is still in progress and RDY/BSY = 1 means EEPROM write cycle is completed and another write cycle can be initiated. In addition, during EEPROM programming, an attempted read from the EEPROM will fetch the byte being written
with the MSB complemented. Once the write cycle is completed, true data are valid at all bit locations.

To access the 2K on-chip EEPROM in KEIL C51 we must include the reg8252.h file into our file.

Read/Write 2K On-chip EEPROM function(KEIL C51 V7.5)
#include<reg8252.h>

void WriteEEP(unsigned char xdata * ADDR,unsigned char EEP_Data);
unsigned char ReadEEP(unsigned char xdata * ADDR);

//---------------------------------------
// Write EEPROM funcrion 
// Input : ADDR = address 000h-7FFh 
//         EEP_Data = Data
//---------------------------------------
void WriteEEP(unsigned char xdata * ADDR,unsigned char EEP_Data)
{
	EA=0;	// disable Interrupts during write
	WMCON|=EEMEN_ | EEMWE_;
	*ADDR=EEP_Data;
	WMCON &= ~EEMEN_;
	WMCON &= ~EEMWE_;	
	while((WMCON&WDTRST_)==0);// wait until write is complete	
	EA=1;					
}

//---------------------------------------
// Read EEPROM function
// Input : ADDR = address 
// Output: unsigned char (8bit)
//---------------------------------------
unsigned char ReadEEP(unsigned char xdata *ADDR)
{
	unsigned char EEP_DATA;
	EA=0;	// disable Interrupts during write
	WMCON|=EEMEN_;
	EEP_DATA = *ADDR;
	WMCON &= ~EEMEN_;
	EA=1;    
	return EEP_DATA;		
}
                

To use the above function just call like this

WriteEEP(0x123,0xAA); // Write data 0xAA to EEPROM at address 0x123

unsigned char MyByte;
MyByte = ReadEEP(0x123); // Read EEPROM at address 0x123 and return unsigned char




Download KEIL C51 example




Copyright(c) 2005-2008 sixca.com, All right reserved.
Best view @ 800X600, IE 6.0 up   
Terms of Use  Privacy Policy