真心找不到错误了,程序已经写了两遍了求助啊
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit scl=P2^0;
sbit sda=P2^1;
void delay()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void start()
{
scl=0;
delay();
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
scl=0;
delay();
scl=1;
delay();
}
void stop()
{
scl=0;
delay();
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
scl=0;
delay();
}
void respond()
{
uchar i=0;
scl=1;
delay();
while((sda==1)&&(i<255))
i++;
scl=0;
delay();
scl=1;
delay();
}
void no_respond()
{
sda=1;
delay();
scl=1;
delay();
scl=0; //清时钟线,钳住I2C总线以便继续接收
delay();
}
void write_byte(uchar dat)
{
uchar i=0;
scl=0;
for(i=0;i<8;i++)
{
if((dat<<i)&0x80)sda=1; //判断发送位
else sda=0;
delay();
scl=1; //置时钟线为高,通知被控器开始接收数据位
delay();
scl=0;
}
sda=1;
delay();
scl=1;
delay();
}
uchar read_byte(void)
{
uchar dat,i;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
dat=(dat<<1)|sda;
scl=0;
delay();
}
scl=0;
delay();
sda=1;
delay();
return dat;
}
void write_add(uchar add,uchar num)
{
start();
write_byte(0xae);
respond();
write_byte(add);
respond();
write_byte(num);
respond();
stop();
}
uchar read_add(uchar add)
{
uchar dat;
start();
write_byte(0xae);
respond();
write_byte(add);
respond();
start();
write_byte(0xaf);
respond();
dat=read_byte();
no_respond();
stop();
return dat;
}
void main()
{
uchar num;
num=read_add(5);
if(num>=15)num=0;
num++;
P1=~num;
write_add(5,num);
while(1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>