这是我的程序,总是写不进去。各位大师帮忙看看/谢谢各位了!
#include<reg52.h>
#include <soft_delay.h>
sbit scl=P2^0; //¶ÔÓ¦c8051f340¿ª·¢°åscl
sbit sda=P2^1; //¶ÔÓ¦c8051f340¿ª·¢°åsda
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<250))
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;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
/*Ö¸¶¨µØÖ·¶ÁÒ»¸ö×Ô¼ºµÄº¯Êý*/
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 write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar p;
void main()
{
init();
write_add(0x00,0x99);
p=read_add(0x00);
if(p==0x99)
{
led=~led;
delay();
delay();
}
else
{
led=0;
}
}
|