#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^0;
sbit scl=P2^1;
uchar a;
//define 函数
void delay();
void start();
void response();
uchar read_byte();
void write_byte(uchar dat);
void init();
void stop();
void delay1(uint z);
void delay()
{
uint i,j;
for(i=0;i<1;i++)
for(j=0;j<1;j++);
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void response()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<255))i++;
scl=0;//destroy the condition of response, to avoid response.
delay();
}
uchar read_byte()
{
uchar i,j,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;delay();
k=((k<<1)|sda);
scl=0;delay();
}
return k;
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;//写byte时把时钟拉低
delay();
sda=CY;// spill registor
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
void init()//bus initation
{
scl=1;
delay();
sda=1;
delay();
}
void main()
{
init();
start();
write_byte(0xa0);
response();
write_byte(3);
response();
write_byte(0xfe);
response();
stop();
delay1(100);
start();
write_byte(0xa0);
response();
write_byte(3);
response();
start();
write_byte(0xa1);
response();
a=read_byte();
P1=a;
response();
stop();
while(1);
}
void delay1(uint z){
uint i,j;
for(i=0;i<z;i++)
for(j=0;j<100;j++);
}
|