#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P3^4;
sbit sda=P3^5 ;
void delay()
{;;}
void delay1(uchar x)
{
uchar i,j;
for(i=x;i>0;i--);
for(j=100;j>0;j--);
}
void start() //起始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop() //停止信号
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void ask() //应答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))
i++;
scl=0;
delay();
}
void write_byte(uchar date) //写入一个字节
{
uchar i,temp;
temp=date;
// scl=0;
//delay();
for(i;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,j,k;
scl=0;
delay();
sda=1;
delay();
for(i;i<8;i++)
{
scl=1;
delay();
j=sda;
k=(k<<1)|j;
scl=0;
delay();
}
delay();
return k;
}
void init() //初始化
{
scl=1;
sda=1;
}
void write_add(uchar address,uchar date)//指定地址写数据
{
start();
write_byte(0xa0);
ask();
write_byte(address);
ask();
write_byte(date);
ask();
stop();
}
uchar read_add(uchar address) //指定地址读
{
uchar date;
start();
write_byte(0xa0);
ask();
write_byte(address);
ask();
start();
write_byte(0xa1);
ask();
date=read_byte();
stop();
return date;
}
void main()
{
init();
write_add(3,0xaa);
delay1(1000);
P0=read_add(3);
while(1);
}分析分析为什么p0输出的是0xa1;并且改变write_byte(0Xa1)括号里的参数,P0输出改变 |