哦!那就说说吧!放芯片那板是我用油性笔画的。 电路很简单。相信大家都懂!
#include<reg52.h>
#define uchar unsigned char
sbit sda=P1^7;
sbit scl=P1^6;
sbit w1=P2^0;
sbit w2=P2^1;
sbit w3=P2^2;
sbit w4=P2^3;
sbit w5=P2^4;
sbit w6=P2^5;
sbit w7=P2^6;
sbit w8=P2^7;
unsigned char leddata[]={
0xC0, //"0"
0xF9, //"1"
0xA4, //"2"
0xB0, //"3"
0x99, //"4"
0x92, //"5"
0x82, //"6"
0xF8, //"7"
0x80, //"8"
0x90, //"9"
0x88, //"A"
0x83, //"B"
0xC6, //"C"
0xA1, //"D"
0x86, //"E"
0x8E, //"F"
0x89, //"H"
0xC7, //"L"
0xC8, //"n"
0xC1, //"u"
0x8C, //"[s:9]"
0xA3, //"o"
0xBF, //"-"
0xFF, //熄灭
0xFF //自定义
};
uchar t, a,month,day,fen,shi,miao;
uchar mo1,mo2,f1,f2,d1,d2,s1,s2;
void disp(uchar mo1,uchar mo2,uchar day1,uchar day2,uchar shi1,uchar shi2,fen1,uchar fen2);
void delay1(uchar x);
void read_8563();
void delay()
{
; ;
}
void start() //开始信号
{
sda=1;
delay1(1);
//delay1(1);
disp(mo1,mo2,d1,d2,s1,s2,f1,f2);
scl=1;
delay1(1);
//delay1(1);
disp(mo1,mo2,d1,d2,s1,s2,f1,f2);
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();
}
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;
}
void delay1(unsigned int x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--);
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa2);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa2);
respons();
write_byte(address);
respons();
start();
write_byte(0xa3);
respons();
date=read_byte();
stop();
return date;
}
void uart()
{
TMOD=0x21;//设置定时器1为工作方式2,8位自动重装,设定时器0为16位定时器。 d=0;
TH1=0xfd;
TL1=0xfd;
TR0=1;
ET0=1;
TH0=0x4c;
TL0=0x00;
TR1=1;//启动定时器1
REN=1;// 允许串行接收。
SM1=1; //方式1,8位UART
EA=1; //开总中断
ES=1; //开串行中断
}
void main()
{
init();
uart();
write_add(0x07,0x06); //月
delay1(100);
delay1(100);
write_add(0x05,0x29); // 日
delay1(100);
delay1(100);
write_add(0x04,0x23); //时
delay1(100);
delay1(100);
write_add(0x03,0x29); //分
delay1(100);
delay1(100);
P2=0xff;
while(1)
{
mo1=month/10;
mo2=month%10;
d1=day/10;
d2=day%10;
s1=shi/10;
s2=shi%10;
f1=fen/10;
f2=fen%10;
disp(mo1,mo2,d1,d2,s1,s2,f1,f2);
if(t==20)
{
t=0;
read_8563();
ES=0;
SBUF=miao;
while(!TI);
TI=0;
ES=1;
}
}
}
void disp(uchar mo1,uchar mo2,uchar day1,uchar day2,uchar shi1,uchar shi2,fen1,uchar fen2)
{
w1=0;
P0=leddata[mo1];
delay1(10);
w1=1;
P0=0xff;
w2=0;
P0=leddata[mo2];
delay1(10);
w2=1;
P0=0xff;
w3=0;
P0=leddata[day1];
delay1(10);
w3=1;
P0=0xff;
w4=0;
P0=leddata[day2];
delay1(10);
w4=1;
P0=0xff;
w5=0;
P0=leddata[shi1];
delay1(10);
w5=1;
P0=0xff;
w6=0;
P0=leddata[shi2];
delay1(10);
w6=1;
P0=0xff;
w7=0;
P0=leddata[fen1];
delay1(10);
w7=1;
P0=0xff;
w8=0;
P0=leddata[fen2];
delay1(10);
w8=1;
P0=0xff;
}
void read_8563()
{
miao=read_add(0x02)&0x7f;//获取秒数据
/* miao=((miao&0xf0)>>4&0x0f)*10+(miao&0x0f);//BCD码转换成10进制数
if(miao>=60){miao=0;} */
fen=read_add(0x03)&0x7f;//获取分钟
fen=((fen&0xf0)>>4&0x0f)*10+(fen&0x0f);//BCD码转换成10进制数
if(fen>=60){fen=0;}
shi=read_add(0x04)&0x3f;//获取小时
shi=((shi&0xf0)>>4&0x0f)*10+(shi&0x0f);//BCD码转换成10进制数
if(shi>=24){shi=0;}
day=read_add(0x05)&0x3f;//获取日数据
day=(( day&0xf0)>>4&0x0f)*10+( day&0x0f);//BCD码转换成10进制数
if( day>=31){ day=0;}
/* week=read_add(0x06)&0x07;//获取星期
if(week>6){week=0;} */
month=read_add(0x07)&0x1f;//获取月
month=((month&0xf0)>>4&0x0f)*10+(month&0x0f);//BCD码转换成10进制数
if(month>=24){month=0;}
/* year=read_add(0x08)&0xff;//获取年
year=((year&0xf0)>>4&0x0f)*10+(year&0x0f);//BCD码转换成10进制数
delay1(100); */
}
void time() interrupt 1
{
TH0=0x4c;
TL0=0x00;
t++;
}
void ser() interrupt 4
{
RI=0;
P0=SBUF;
}