www.pudn.com > lcd_dispaly-eeprom.rar > EEPROM.C, change:2013-06-18,size:3714b


/*************************************************************************** 
檔名		: EEPROM.C 
目的		: 24LC02 EEPROM 
程式設計者	:  
日期		: 
版本		:  
****************************************************************************/ 
 
#include "reg51.h" 
 
void delay_1ms(void); 
void EEI2C_START(void); 
void EEI2C_STOP(void); 
void EEI2C_ACK(void); 
 
// IO Polarity 
sfr P1M0      = 0x91; 
sfr P1M1      = 0x92; 
 
// For 24c04以上版本 
void eewrite(unsigned char rom_addressHigh, unsigned char rom_addressLow,	unsigned char write_data); 
unsigned char eeread(unsigned char rom_addressHigh, unsigned char rom_addressLow); 
 
// For 24C00, 24C01, 24C02 版本 
//void eewrite(unsigned char rom_address,	unsigned char write_data); 
//unsigned char eeread(unsigned char read_address); 
 
 
void write_8bit(wData); 
unsigned char read_8bit(void); 
 
 
sbit  EESDA  = P1^4; 
sbit  EESCL  = P1^3; 
 
 
void delay_1ms(void) 
{ 
		unsigned int i,temp; 
   for (i=0;i<50;i++){ 
		 temp =0x00; 
	 }	  
 
} 
 
void EEI2C_START(void) 
{ 
	EESDA=1; 
	delay_1ms(); 
	EESCL=1; 
	delay_1ms(); 
	EESDA=0; 
	delay_1ms(); 
	EESCL=0; 
	delay_1ms(); 
} 
 
void EEI2C_STOP(void) 
{ 
	EESCL=0; 
	delay_1ms(); 
	EESDA=0; 
	delay_1ms(); 
	EESCL=1; 
	delay_1ms(); 
	EESDA=1; 
	delay_1ms(); 
} 
 
void EEI2C_ACK(void) 
{ 	 
	unsigned char z=0; 
P1M0 = 0x10; 
P1M1 = 0xEF;	 
	EESDA=1;         // 還原到初始 
	delay_1ms(); 
	 
    while((EESDA==1)&&(z<200))	z++;   // Z 保護用,避免程式進入無窮回圈 
//	while(EESDA==1)	z++; 
	EESCL=0; 
	delay_1ms(); 
P1M0 = 0x00; 
P1M1 = 0xFF;		 
}  
 
// For 24c04以上版本 
void eewrite(unsigned char rom_addressHigh, unsigned char rom_addressLow,	unsigned char write_data) 
//void eewrite(unsigned char rom_address,	unsigned char write_data) 
{ 
	unsigned int ii; 
 
	 
	    EEI2C_START(); 
     
      write_8bit(0xA0); 
      EEI2C_ACK(); 
       
      write_8bit(rom_addressHigh); 
      EEI2C_ACK(); 
      write_8bit(rom_addressLow); 
      EEI2C_ACK();       
      write_8bit(write_data); 
      EEI2C_ACK(); 
       
    	EEI2C_STOP(); 
	 
	// Delay only 
	for(ii=0;ii<500;ii++){ 
			delay_1ms();			delay_1ms();			delay_1ms();			delay_1ms();			delay_1ms(); 
			delay_1ms();			delay_1ms();			delay_1ms();			delay_1ms();			delay_1ms(); 
	} 
	 
} 
 
 
void write_8bit(wData){ 
 
unsigned char i; 
 
//				EESCL=0; 
//				delay_1ms();	 
				 
			for(i=0;i<8;i++) 
			{ 
							 
				if((wData & 0x80 ) == 0x80)			 
					EESDA=1;  
				else 
					EESDA=0;				 
				 
				delay_1ms(); 
				 
				EESCL=1; 
				delay_1ms(); 
				delay_1ms(); 
				EESCL=0; 
				delay_1ms(); 
				 
				wData=wData<<1; 
							 
			} 
			 
//		EESDA=1;         // 還原到初始 
//		delay_1ms(); 
//		EESCL=1;         //  還原到初始 
//		delay_1ms();   
}		 
 
// For 24c04以上版本 
unsigned char eeread(unsigned char rom_addressHigh,unsigned char rom_addressLow) 
//unsigned char eeread(unsigned char read_address) 
{ 
	 
	unsigned char read_data; 
 
			EEI2C_START(); 
	 
      write_8bit(0xA0); 
      EEI2C_ACK(); 
 
      write_8bit(rom_addressHigh); 
      EEI2C_ACK(); 
      write_8bit(rom_addressLow); 
      EEI2C_ACK();      
//      write_8bit(read_address); 
//      EEI2C_ACK(); 
	     
	    EEI2C_START(); 
      write_8bit(0xA1); 
      EEI2C_ACK(); 
       
	    read_data = read_8bit(); 
			EEI2C_ACK(); 
			 
			EEI2C_STOP(); 
	 
	  return (read_data); 
} 
 
 
unsigned char read_8bit(void){ 
 
unsigned char rdata,i;	 
 
	rdata = 0x00; 
	 
P1M0 = 0x10; 
P1M1 = 0xEF;		 
	 
	for(i=0;i<8;i++){ 
		rdata = rdata<<1; 
		EESDA = 1; 
		delay_1ms(); 
		EESCL = 0; 
		delay_1ms(); 
		EESCL = 1; 
		delay_1ms(); 
		 
		if(EESDA==1)		 
			rdata|=0x01; 
			 
	} 
	 
	return (rdata); 
P1M0 = 0x00; 
P1M1 = 0xFF;		 
}