写的程序如下:实验现象没有。求教高人指点。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P2^1;
sbit sda=P2^0;
sbit de=P1^4;
sbit dula=P2^7;
sbit wela=P2^6;
void delay() [url=]//@12.000MHz[/url]
{
; ;
}
void Delay2ms() [url=]//@12.000MHz[/url] 2毫秒延时
{
unsigned char i, j;
i = 4;
j = 225;
do
{
while (--j);
} while (--i);
}
void init()
{
sda=1;
scl=1;
delay();
}
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 write_byte(uchar date) //写一个字节
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
scl=0;
delay();
temp<<=1;
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte(void) //读取一个字节
{
uchar i,j;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
j=(j<<1)|sda;
scl=0;
delay();
}
return j;
}
void write_add(uchar address,uchar date)
{ //在xx位置写个字节
start(); /**/
write_byte(0xa0); /**/
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void main()
{
init();
start();
write_add(0x01,0xaa);
delay();
Delay4ms();
; ; ; ; ; ; ; ; ; ; ;
P0=read_add(0xaa);
delay();
; ; ; ; ; ; ; ; ; ; ;
Delay4ms();
de=0;
while(1);
}
|