写了个PCF8951的程序 但是显示的数字是255 调节电位器数码管没有变化,请大侠看看是哪里错了
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit scl=P2^1;
sbit sda=P2^0;
uchar temp; bai; shi;ge; aa; zhi;
void delay()
{;;}
void delay1(uint z)
{ uint x,y;
for(x=z;x>0;x--)
for(y=25;y>0;y--);}
void delay_1()
{ uint x,y;
for(x=2;x>0;x--)
for(y=110;y>0;y--);}
void display(uchar bai,uchar shi,uchar ge)
{
temp=0xfe;
P1=temp;
P0=table[0];
delay1(5);
temp=0xfd;
P1=temp;
P0=table[bai];
delay1(5);
temp=0xfb;
P1=temp;
P0=table[shi];
delay1(5);
temp=0xf7;
P1=temp;
P0=table[ge];
delay1(5);
}
void init()
{ scl=1;
delay();
sda=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 respond()
{ uchar i=0;
scl=1;
delay();
while((sda==1)&&(i<<255))
i++;
scl=0;
delay();
}
void writebyte(uchar date)
{uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{ temp=temp<<1;
sda=CY;
delay();
scl=1;
delay();
scl=0;
delay();
}
sda=1;
delay();
}
uchar readbyte()
{ uchar i,j,k;
scl=0;
delay();
sda=1;
delay();
for(i=8;i>0;i--)
{ scl= 1;
delay();
j=sda;
k=(k<<1)|j;
scl=0;
delay();
}
return k ;
}
uchar ad()
{ init();
start();
writebyte(0x90);
respond();
writebyte(0x01);
respond();
delay_1();
start();
writebyte(0x91);
respond();
aa=readbyte();
respond();
stop ();
return aa;
}
void main()
{
while(1)
{ zhi=ad();
bai=zhi/100;
shi=zhi%100/10;
ge=zhi%100%10;
display(bai,shi,ge);}
} |