我这里有一个是用DS1302的,经过测试的,希望对你有帮助
程序如下:
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rst=P3^5;
sbit sck=P3^4;
sbit io=P3^3;
uchar i,j;
uchar shumaguan[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,
0x1f,0x01,0x09,0x11,0xc1,0x63,0x85,0x61,0x71,0xff,0xff}; //共阳极
uchar shuguan_duan[]={0x20,0x10,0x08,0x04,0x02,0x01};
uchar time_add[]={0x81,0x83,0x85}; //秒、分、时
uchar date[3];
uchar read1302(uchar add)
{
uchar i,dat1,dat2;
rst=1;
sck=0;
for(i=0;i<8;i++)
{
sck=0;
io=add&0x01;
add>>=1;
sck=1;
}
io=1;
for(i=0;i<8;i++)
{
if(io)
dat1|=0x80;
sck=1;
dat1>>=1;
sck=0;
}
rst=0;
dat2=dat1/16;
dat1=dat1%16;
dat1=dat1+dat2*10;
return dat1;
}
void xianshi(void)
{
uchar i,j;
for(i=0;i<3;i++)
{
date[i]=read1302(time_add[i]);
}
for(i=0;i<6;i++)
{
P1=0xff;
switch (i)
{
case 0: j=date[0]%10;break;
case 1: j=date[0]/10;break;
case 2: j=date[1]%10;break;
case 3: j=date[1]/10;break;
case 4: j=date[2]%10;break;
case 5: j=date[2]/10;break;
}
P0=shuguan_duan[i];
P1=shumaguan[j];
}
}
void write1302(uchar add,dat)
{
uchar i;
rst=0;
sck=0;
rst=1;
for(i=8;i>0;i--)
{
sck=0;
io=add&0x01;
add>>=1;
sck=1;
}
for(i=8;i>0;i--)
{
sck=0;
io=dat&0x01;
dat>>=1;
sck=1;
}
rst=0;
}
void main()
{
rst=0;
io=1;
sck=0;
// write1302(0x80,0x55);
// write1302(0x82,0x59);
// write1302(0x84,0x12);
while(1)
{
xianshi();
}
}
参考资料: 单片机
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,//加小数点部分的显示
0xff};
unsigned char buf[8]={0,12,12,12,12,12,12,12};
unsigned char ss[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char n=0;
signed char hour,min,sec; //定义变量存放 时,分,秒
void delay(void)//延时20ms
{
unsigned char i,k;
for(i=1;i>0;i--)
for(k=100;k>0;k--);
}
void disp(void)//显示计时器内容
{
unsigned char i,num;
for(i=0;i<=7;i++)
{P2=ss[i];
num=buf[i];
P0=tab[num];
delay();
}
}
void disp_time(void) //显示时间(时分秒分别送相应buf)
{
buf[0]=sec%10;buf[1]=sec/10;
buf[2]=min%10+10;buf[3]=min/10;
buf[4]=hour%10+10;buf[5]=hour/10;
buf[6]=20;buf[7]=20;
disp();
}
void time_t0(void)interrupt 1//定时/计时器中断,定时1s,
{
TH0=0x3C;
TL0=0xB0;
n++;
while(n==20)
{
n=0;//必须的
sec++;
if(sec>59)
{sec=0;min++;
if(min>59)
{min=0;hour++;
if(hour>23)
{hour=0;} break;
}}
}}
void main(void)
{ hour=23,min=59,sec=59;
//中断设置
TMOD=0x01;//定时计数器初始化
TH0=0x3C;
TL0=0xB0;//初始值
IE=0x82;//开总中断/允许定时/计数器0 中断
TR0=1;
while(1){disp_time();}//重复调用相应函数
}
最好把你qq个给我。打包发给你