#include<reg51.h>
#define uchar unsigned char
sbit sck=P2^0;
sbit sda=P2^1;
void delay()
{
for(;;);
}
void init()
{
sck=1;
delay();
sda=1;
delay();
}
void start()
{
sda=1;
delay();
sck=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
sck=1;
delay();
sda=1;
delay();
}
void write_add(uchar data1)
{
uchar i,temp;
temp=data1;
for(i=0;i<8;i++)
{
temp=temp<<1;
sck=0; //֮ǰstartÐźÅÖÐsck=1,sda=0;ΪÁËдÊý¾Ý
//±ØÐëÈÃsck´¦ÓÚ0·Ç±£³Ö״̬
delay();
sda=CY;
sck=1;
delay();
}
sck=0; //ΪӦ´ðÐźŵÄÉÏÉýÑØ×ö×¼±¸
delay();
sda=1;
delay();
}
void respons()
{
uchar j;
sck=1;
delay();
while((sda==1)&&(j<250)) //Ö»ÈÃ×ö250µÄÑÓʱ£¬Ò»¶Îʱ¼äÄÚûÓÐÊÕµ½´Ó»úµÄ
//Ó¦´ðÐźţ¬Ôò×Ô¶¯ÈÏΪ´Ó»úÒÑÕýÈ·½ÓÊÕµ½Êý¾Ý¡£
{
j++;
}
sck=0; //Í£Ö¹±£³Ö
delay();
}
uchar read_add()
{
uchar k,data2;
sck=0;
delay();
sda=1;
delay();
for(k=0;k<8;k++)
{
sck=1;
delay();
data2=(data2<<1)|sda;
sck=0;
delay();
}
return data2;
}
void write(uchar address,uchar info)
{
start();
write_add(0xa0);
respons();
write_add(address);
respons();
write_add(info);
respons();
stop();
}
uchar read(uchar address)
{
uchar da;
start();
write_add(0xa0);
respons();
write_add(address);
respons();
start();
write_add(0xa1);
respons();
da=read_add();
stop();
return da;
}
void delay1(uchar z)
{
uchar i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void main()
{
init();
//start();
write(23,0xaa);
delay1(100);
P1=read(23);
while(1);
}
想问的是:在仿真中怎么感觉24C02b没工作是的,本来写一个字节给EEPROM,再读取那个字节到数码管让其显示,牛人们帮我看看吧!!不胜感激! |