Source: Hardware and software 24CXX series EEPROM memory compatible with the I²C protocol
Hardware due to the 24C01 A0A1A2 pin is not allowed to be suspended, so the idea is 24C02 compatible with the temporary---24C16
Use a DIP8 encapsulated chip socket, A0 A1 A2 pins are floating, chip changer convenient
The 24C02 address on the software is only 8 bits, while other models are larger than 8 bits, so the address parameter uses 16 bits
256 bytes As a large page, i.e. Largepage, test chip 24c04 space has 512 bytes
On the code, ask for testing and discussion
#include"MY51. H"//reprint Please specify:http://xouou.iteye.comask for a test discussion//Stc89c52rc,11.0592mhz Crystal OscillatorSbit sda=p2^0;//bus Connector definitionSbit scl=p2^1;//bus Connector definitionvoidDelayus ()//requires 4 machine cycles, approx. 4.34us{ ; //Crystal oscillator frequency 11.0592M, machine cycle is 1.085 microseconds}voidIic_start ()//Start Signal{SDA=1; SCL=1; Delayus (); //SDA and SCL same as high-level hold above 4.7us_nop_ ();//1.085us, Total 5.78usSda=0;//Falling EdgeDelayus ();//SDA low level remains above 4us, here is 4.34us to meet the requirements}voidIic_stop ()//Stop Signal{SDA=0; _nop_ ();//Readiness StatusScl=1; Delayus (); //The state stabilization time required to maintain more than 4usSda=1;//during SCL high, SDA comes up a rising edgeDelayus ();//SDA remains above 4.7us, 4.34 plus function return time greater than 4.7us//Note: Both SCL and SDA are 1 at this point}voidIic_sendbyte (U8 bytedata)//MCU sends a byte{U8 i; U8 Temp=Bytedata; for(i=0;i<8; i++) {Temp=temp<<1;//after moving the highest level to the CY bit of the PSW registerScl=0;//Prepare_nop_ ();//stabilize it, please.Sda=cy;//put the data to be sent to the SDA on one bit_nop_ (); SCL=1;//during each high level, the IC device will take the data away_nop_ (); } SCL=0;//if written scl=1;sda=1 is a stop signal, you can't write this._nop_ (); SDA=1;//release the bus and release the data bus when not in use_nop_ ();} U8 Iic_readbyte ()//read a byte{U8 i,temp; SCL=0;//preparing to read data_nop_ (); SDA=1;//Release Bus_nop_ (); for(i=0;i<8; i++) {SCL=1;//MCU starts fetching dataDelayus ();//when the SCL is high, the IC device sends 1 bits of data to the SDA//The total time will not be greater than 4.34us, then you can let the MCU read SDATemp= (temp<<1) |sda;//read one saved to tempScl=0; Delayus (); } returntemp;}BOOLIic_checkack ()//processing the response signal{U8 errcounts=255;//Define a timeout amount of 255 timesScl=1; _nop_ (); while(SDA)//Sda=0 was detected as a response signal for a period of time. { if(0==errcounts) {SCL=0;//Clamp the bus_nop_ (); returnFALSE;//no response signal .} errcounts--; } SCL=0;//clamp the bus to prepare for the next 1 communications_nop_ (); returnTRUE;//successfully processing the response signal}voidIic_init ()//Bus Initialization{SCL=1; SDA=1; Delayus ();}voidIic_sendack (BOOLB_ack)//send an answer or non-response signal{SCL=0;//Prepare_nop_ (); if(B_ack)//ACK sent should signal{SDA=0; } Else //Unack Send non-response signal{SDA=1; } _nop_ (); SCL=1; Delayus (); //delay of more than 4usScl=0;//Clamp SCL In order to continue receiving data_nop_ ();}voidAt24cxx_writebyte (U16 address,u8 Databyte)//write a byte of data to 24cxx{U8 Largepage= address/ the;//The 24C04 is 512 bytes (addressing range 0~511) and the Largepage maximum value is 1U8 Addressoffset = address% the;//largepage=0 Words address range is (0~255)Iic_start (); Iic_sendbyte (0xa0| (largepage<<1));//control Word, the first 4 bits fixed 1010, the last three bits is the device address, the last 0 is writeIic_checkack ();//MCU processing response signalIic_sendbyte (Addressoffset);//specifies the offset of the address within the Largepage block to be written by the deviceIic_checkack (); Iic_sendbyte (Databyte); //Write DataIic_checkack (); Iic_stop (); Delayms (2); //when writing by Byte, 24cxx erases data to the inside after it receives a stop signal, which takes time//and will not respond to any requests on the bus during this time, so the MCU has more than 2 milliseconds to wait}voidAt24cxx_writedata (U16 address,u8 numbytes,u8* buf)//write arbitrary length data (max. 256 bytes){ while(numbytes--) {At24cxx_writebyte (address++,*buf++); }}voidAt24cxx_readdata (U16 beginaddr,u8 datasize,u8* buf)//reads any length of bytes into the buffer buf{U8 Largepage= beginaddr/ the;//calculate largepage,256 bytes to a large pageU8 Addressoffset = beginaddr% the;//calculating offsets relative to LargepageIic_start ();//Start SignalIic_sendbyte (0xa0| (largepage<<1));//control Word, writeIic_checkack ();//processing the response signalIic_sendbyte (Addressoffset);//The destination address offset to readIic_checkack ();//processing the response signalIic_start ();//Send start SignalIic_sendbyte (0xa1| (largepage<<1));//control word, readIic_checkack ();//processing the response signal while(datasize--)//read DataSize bytes, max 256 bytes{//datasize with U16 type will burst the Ram*buf++=iic_readbyte ();//Read bytes and save to buffer bufIic_sendack (datasize);//send an answer when datasize for 0 o'clock MCU sends non-response} iic_stop (); //Send Stop signal}voidMain ()//Test{U8 buf[3];//buffers that accept dataU8 arr[7]={0x06,1,2,3,4,0x55,0x33};//data to be writtenIic_init (); //Bus InitializationAt24cxx_writedata (0x00+ the,sizeof(arr), arr);//starts writing 7 bytes of data to the specified addressP1=0xFF;//Debug code, with the P1 port LED displayDelayms ( +);//Debug CodeAt24cxx_readdata (0x00+ the,sizeof(BUF), buf);//read 3 bytes from a specified addressp1=buf[2];//Which means 2 .//LED light Display value while(1) {P1=~P1; Delayms ( -); } }
// The main use of the My51.h is #include <reg52.h>"mytype.h"void delayms ( U16 ms) // Soft-delay function { u16 i,j ; for (i=ms;i>0; i--) { for (j=113;j>0; j--) {} }}
The code has been improved by removing the delayms (2bool check_icwritecomplete () when writing the data // { iic_start (); Iic_sendbyte (0xa0); return iic_checkack ();}
Hardware and software compatible with I²C-Protocol 24CXX series EEPROM Memory (RPM)