#pragma ot(6 SIZE)
#include
#include
#define ERRORCOUNT 10
sbit SDA=P0^0;
sbit SCL=P0^1;
enum eepromtype {M2401 M2402 M2404 M2408 M2416 M2432 M2464 M24128 M24256};
enum eepromtype EepromType;
//DataBuff为读写数据输入/输出缓冲区的首址
//ByteQuantity 为要读写数据的字节数量
//Address 为EEPROM的片内地址
//ControlByte 为EEPROM的控制字节,具体形式为(1)(0)(1)(0)(A2)(A1)(A0)(R/W) 其中R/W=1
//表示读操作 R/W=0为写操作 A2 A1 A0为EEPROM的页选或片选地址;
//EepromType为枚举变量 需为M2401至M24256中的一种 分别对应24C01至24C256;
//函数返回值为一个位变量,若返回1表示此次操作失效,0表示操作成功;
//ERRORCOUNT为允许最大次数,若出现ERRORCOUNT次操作失效后,则函数中止操作,并返回1
//SDA和SCL由用户自定义,这里暂定义为P0^0和P0^1;
//其余的用户不用管,只要把只子程序放在你的程序中并调用它就可以了;
/***********************************************************************************/
bit RW24XX(unsigned char *DataBuff unsigned char ByteQuantity unsigned int Address
unsigned char ControlByte enum eepromtype EepromType)
{
void Delay(unsigned char DelayCount);
void IICStart(void);
void IICStop(void);
bit IICRecAck(void);
void IICNoAck(void);
void IICAck(void);
unsigned char IICReceiveByte(void);
void IICSendByte(unsigned char sendbyte);
unsigned char data j i=ERRORCOUNT;
bit errorflag=1;
while(i--)
{
IICStart();
IICSendByte(ControlByte&0xfe);
if(IICRecAck())
continue;
if(EepromType>M2416)
{
IICSendByte((unsigned char)(Address>>8));
if(IICRecAck())
continue;
}
IICSendByte((unsigned char)Address);
if(IICRecAck())
continue;
if(!(ControlByte&0x01))
{
j=ByteQuantity;
errorflag=0; //********clr errorflag
while(j--)
{
IICSendByte(*DataBuff++);
if(!IICRecAck())
continue;
errorflag=1;
break;
}
if(errorflag==1)
continue;