#include
#include
#define uint unsigned int
#define uchar unsigned char
#define LED7 XBYTE[0x9f00]
#define LED6 XBYTE[0x9f01]
#define LED5 XBYTE[0x9f02]
#define LED4 XBYTE[0x9f03]
#define LED3 XBYTE[0x9f04]
#define LED2 XBYTE[0x9f05]
#define LED1 XBYTE[0x9f06]
#define LED0 XBYTE[0x9f07]
bit write=0;
sbit sda=P2^0;
sbit scl=P2^1;
uchar sec,tcnt;
uint t[]= //共阴数码显示表(89c52)
{
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x40
};
void init_1()
{
LED0=0;
LED1=0;
LED2=0;
LED3=0;
LED4=0;
LED5=0;
LED6=0;
LED7=0;
}
void delay()
{;;}
void delay1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
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 dat)
{
uchar i,temp;
temp=dat;
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;
|
文件: |
IIC_24C02.rar |
大小: |
16KB |
下载: |
下载 | |
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write_add(uchar address,uchar dat)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(dat);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar dat;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
dat=read_byte();
stop();
return dat;
}
void display(uchar bai_c,uchar shi_c,uchar ge_c)
{
init_1();
LED0=0;
LED1=0;
LED2=0;
LED3=0;
LED4=0;
LED5=t[ge_c];
LED6=t[shi_c];
LED7=t[bai_c];
delay1ms(1);
}
void main()
{
init();
sec=read_add(2);
if(sec>1000)
sec=0;
TMOD=0x01;
ET0=1;
EA=1;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TR0=1;
while(1)
{
display(sec/100,(sec%100)/10,sec%10);
if(write==1)
{
write=0;
write_add(2,sec);
}
}
}
void timer0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
tcnt++;
if(tcnt==20)
{
tcnt=0;
sec++;
write=1;//1 秒写一次24C02
if(sec==1000)
sec=0;
}
}
阅读(1578) | 评论(0) | 转发(0) |