#include
#define uchar unsigned char
#define uint unsigned int
#define PCF8591 0x90
sbit SCL=P2^0;
sbit SDA=P2^1;
sbit dula=P3^6;
uchar code table1[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
uchar code table2[10]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay();
void delay_ms(uchar x);
void init_i2c();
void start_i2c();
void stop_i2c();
void response_i2c();
void write_byte(uchar dat);
void write_add(uchar address,uchar dat);
uchar read_byte();
uchar read_add(uchar control);
void display(uchar n);
void main()
{
uchar num;
init_i2c();
while(1)
{
num=read_add(0x41);
display(num);
}
}
void delay()
{;;}
void delay_ms(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=125;j>0;j--);
}
void init_i2c()
{
SCL=1;
delay();
SDA=1;
delay();
}
void start_i2c()
{
SDA=1;
delay();
SCL=1;
delay();
SDA=0;
delay();
}
void stop_i2c()
{
SDA=0;
delay();
SCL=1;
delay();
SDA=1;
delay();
}
void response_i2c()
{
uchar i;
SCL=1;
delay();
while((SDA==1)&&(i<255))
i++;
SCL=0;
delay();
}
void write_byte(uchar dat)
{
uchar temp,i;
temp=dat;
for(i=0;i<8;i++)
{
temp<<=1;
SCL=0;
delay();
SDA=CY;
delay();
SCL=1;
delay();
}
SCL=0;
delay();
SDA=1;
delay();
}
void write_add(uchar address,uchar dat)
{
start_i2c();
write_byte(PCF8591);
response_i2c();
write_byte(address);
response_i2c();
write_byte(dat);
response_i2c();
stop_i2c();
}
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 control)
{
uchar dat;
start_i2c();
write_byte(PCF8591);
response_i2c();
write_byte(control);
response_i2c();
start_i2c();
write_byte(PCF8591+1);
response_i2c();
dat=read_byte();
stop_i2c();
return dat;
}
void display(uchar n)
{
uchar bai,shi,ge;
bai=n/100;
shi=(n/10);
ge=n;
dula=0;
P2=table1[7];
dula=1;
dula=0;
P0=table2[bai];
dula=1;
dula=0;
delay_ms(5);
dula=0;
P2=table1[6];
dula=1;
dula=0;
P0=table2[shi];
dula=1;
dula=0;
delay_ms(5);
dula=0;
P2=table1[5];
dula=1;
dula=0;
P0=table2[ge];
dula=1;
dula=0;
delay_ms(5);
}