#include <c8051f020.h> // SFR declarations
#include <intrins.h>
//#define AT24C01 0xa0
//#define first_buff 0x00
//#define SYSCLK 22118400 // SYSCLK frequency in Hz
sbit SDA=P0^4;
sbit SCL=P0^5;
#define uchar unsigned char
#define uint unsigned int
#define Byte unsigned char
#define Word unsigned int
//#define bool bit
#define true 1
#define false 0
// #define SomeNOP(); _nop_();_nop_();_nop_();_nop_();
//unsigned char out[]={1,2,3,4,5,6,7,8,9};
//unsigned char data in[9]={0,0,0,0,0,0,0,0,0};
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<10;j++)
{
_nop_();
}
}
void I2CStart(void)
{
EA=0;
SDA=1;
SCL=1;
//SomeNOP();//INI
delay(10);
SDA=0;
//SomeNOP(); //START
delay(10);
SCL=0;
}
/**--------------------------------------------------------------------------------
调用方式:void I2CStop(void) ﹫2001/07/0 4
函数说明:私有函数,I2C专用
44 ---------------------------------------------------------------------------------*/
void I2CStop(void)
{
SCL=0; SDA=0; /*SomeNOP();*/ delay(10);//INI
SCL=1;/* SomeNOP();*/delay(10); SDA=1; //STOP
EA=1;
}
/**--------------------------------------------------------------------------------
调用方式:bit I2CAck(void) ﹫2001/07/0 4
函数说明:私有函数,I2C专用,等待从器件接收方的应答
55 ---------------------------------------------------------------------------------*/
WaitAck(void)
{
// uchar errtime=255;//因故障接收方无ACK,超时值为255。
SDA=1;delay(10);//SomeNOP();
SCL=1;delay(10);//SomeNOP();
while(SDA) //{errtime--; if (!errtime) {I2CStop();SystemError=0x11;return false;}}
{}
SCL=0;
return true;
//第1 页
//文件: PCF8563T.C 2001-11-27, 18:39:20
}
/**--------------------------------------------------------------------------------
67 调用方式:void SendAck(void) ﹫2001/07/0 4
68 函数说明:私有函数,I2C专用,主器件为接收方,从器件为发送方时,应答信号。
69 ---------------------------------------------------------------------------------*/
void SendAck(void)
{
SDA=0; delay(10);//SomeNOP();
SCL=1; delay(10);//SomeNOP();
SCL=0;
}
void SendNotAck(void)
{
SDA=1; delay(10);//SomeNOP();
SCL=1; delay(10);//SomeNOP();
SCL=0;
}
/**--------------------------------------------------------------------------------
89 调用方式:void I2CSend(uchar ch) ﹫2001/07/0 5
90 函数说明:私有函数,I2C专用
91 ---------------------------------------------------------------------------------*/
void I2CSendByte(Byte ch)
{
uchar i=8;
while (i--)
{
SCL=0;delay(10);//_nop_();
SDA=(bit)(ch&0x80); ch<<=1;delay(10);// SomeNOP();
//delay(20);
SCL=1; delay(10);//SomeNOP();
//delay(10);
}
SCL=0;
}
/**--------------------------------------------------------------------------------
105 调用方式:uchar I2CReceive(void) ﹫2001/07/0 5
106 函数说明:私有函数,I2C专用
107 ---------------------------------------------------------------------------------*/
Byte I2CReceiveByte(void)
{
uchar i=8;
Byte ddata=0;
SDA=1;
while (i--)
{
ddata<<=1;
SCL=0;delay(10);//SomeNOP();
SCL=1;delay(10);//SomeNOP();
ddata|=SDA;
}
SCL=0;
return ddata;
}
void ISendStr(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
I2CStart();
I2CSendByte(sla);
WaitAck();
I2CSendByte(suba);
WaitAck();
I2CSendByte(suba);
WaitAck();
//count=8;
for (i=0;i<no;i++)
{
I2CSendByte(*(s+i));
WaitAck();
//if (i=count-1)
//{SendAck();//除最后一个字节外,其他都要从MASTER发应答。
//_nop_();}
}
//I2CSendByte(value);
//WaitAck();
I2CStop();
delay(200);
}
void IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
I2CStart();
I2CSendByte(sla);
WaitAck();
I2CSendByte(suba);
WaitAck();
I2CSendByte(suba);
WaitAck();
I2CStart();
I2CSendByte(0xA1);
WaitAck();
//count=8;
for (i=0;i<no;i++)
{
*s=I2CReceiveByte();
s++;
if (i!=no-1) SendAck();//除最后一个字节外,其他都要从MASTER发应答。
}
SendNotAck();
I2CStop();
delay(200);
}