#include<reg52.h>
#include <soft_delay.h>
sbit scl=P2^0;
sbit sda=P2^1; //
sbit led=P2^4; //
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<255))
i++;
scl=0;
delay();
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
delay();
return k;
}
///*Ö¸¶¨µØÖ·¶ÁÒ»¸ö×Ô¼ºµÄº¯Êý*/
uchar read_add(uchar address)
{
uchar date;
start();
// write_byte(0xa0);
write_byte(0xae);
respons();
write_byte(address);
respons();
start();
// write_byte(0xa1);
write_byte(0xaf);
respons();
date=read_byte();
stop();
return date;
}
/*Ö¸¶¨µØַдһ¸ö×ֽڵĺ¯Êý*/
void write_add(uchar address,uchar date)
{
start();
// write_byte(0xa0);
write_byte(0xae);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar p;
void main()
{
init();
write_add(0x10,0x99);
delay();
delay();
delay();
delay();
delay();
delay();
delay();
delay();
delay();
delay();
p=read_add(0x10);
while(1)
{
if(p==0x99)
{
led=~led;
delay();
delay();
}
else
{
led=0;
}
}
}
|
刚刚试验了一下,延时太长也是不可以的。从效率考虑,比规定时间长一点点就可以了。
最小是4.7US,长一点是可以的,例如20US没问题,但短了就不行了...