void delay1(uchar x)
{
uint i;
for(i=0;i<x;i++);
}
void clock()
{
uchar i=0;
//TRISB = 0B00000011;//设置RB1 SDA为输入
SCL=1;
asm("NOP");
while ((SDA==1)&&(i<255))i++;
SCL=0;
asm("NOP");
// TRISB = 0B00000001;//设置RB1 SDA为输出
}
void WIN24C02_init()
{
// TRISB = 0B00000001;//设置RB1 SDA为输出
SCL=1;
asm("NOP");
SDA=1;
asm("NOP");
}
void start()
{
// TRISB = 0B00000001;//设置RB1 SDA为输出
SDA=1;
asm("NOP");
SCL=1;
asm("NOP");
SDA=0;
asm("NOP");
SCL=0;
asm("NOP");
}
void stop()
{
// TRISB = 0B00000001;//设置RB1 SDA为输出
SDA=0;
asm("NOP");
SCL=1;
asm("NOP");
SDA=1;
asm("NOP");
}
void writex(uchar j)
{
uchar i,temp;
// TRISB = 0B00000001;//设置RB1 SDA为输出
temp=j;
for (i=0;i<8;i++)
{
SCL=0;
asm("NOP");
if (temp & 0x80) //读取
{
SDA=1;
}
else
{
SDA=0;
}
temp=temp<<1;
//SDA=CY;
asm("NOP");
SCL=1;
asm("NOP");
}
SCL=0;
asm("NOP");
SDA=1;
asm("NOP");
}
char readx()
{
uchar i,j,k=0;
// TRISB = 0B00000001;//设置RB1 SDA为输出
SCL=0;
asm("NOP");
SDA=1;
// TRISB = 0B00000011;//设置RB1 SDA为输入
for (i=0;i<8;i++)
{
asm("NOP");
SCL=1;
asm("NOP");
if (SDA==1) j=1;
else j=0;
k=(k<<1)|j;
SCL=0;
}
// TRISB = 0B00000001;//设置RB1 SDA为输出
asm("NOP");
return(k);
}
char WIN24C02_read(uchar address)
{
uchar i;
start();
writex(0xa0);
clock();
writex(address);
clock();
start();
writex(0xa1);
clock();
i=readx();
stop();
delay1(10);
return(i);
}
void WIN24C02_write( uchar address, uchar info)
{
// GIE=0;//关闭中断
start();
writex(0xa0);
clock();
writex(address);
clock();
writex(info);
clock();
stop();
// GIE=1;//打开中断
delay1(50);
} |