直接复制粘贴然后运行
然后打开stc烧录到开发板上面就能用 程序哪里不懂的话问我,我闲的蛋疼!
用的是CT107D开发板,
#include <STC15F2K60S2.H>
#include <intrins.h>
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
unsigned char shijian[]={50,59,23,0,0,0,0};
unsigned char num[7];
unsigned char wendu;
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char shezhi=0;
unsigned char jiangge;
unsigned char a=0;
unsigned char jishu[10],c=0;
unsigned char dda=0;
unsigned char ximie=0;
unsigned char jiemian3=0;
unsigned int b;
unsigned char add=0,panding=0,ddc;
unsigned char sansuo=0;
sbit DQ=P1^4;
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
void Write_Ds1302_Byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(((dat/10)<<4)|(dat%10));
RST=0;
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
unsigned char dat1,dat2;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;
_nop_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
dat1=temp/16;
dat2=temp%16;
temp=dat1*10+dat2;
return (temp);
}
void chushihua2()
{
unsigned char i,add;
add=0x80;
Write_Ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302(add,shijian[i]);
add=add+2;
}
Write_Ds1302(0x8e,0x80);
}
void DS1302zhu()
{
unsigned char i,add;
add=0x81;
Write_Ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
num[i]=Read_Ds1302(add);
add=add+2;
}
Write_Ds1302(0x8e,0x80);
}
void Delayms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=845;j>0;j--);
}
void chushihua()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0X00;P2=0XFF;P0=0XFF;
}
void shumaguan1(unsigned char yi,unsigned char er)
{
P2=0XC0;
P0=0X01;
P2=0XFF;
P0=tab[yi];
Delayms(1);
P2=0XC0;
P0=0X02;
P2=0XFF;
P0=tab[er];
Delayms(1);
}
void shumaguan2(unsigned char san,unsigned char si)
{
P2=0XC0;
P0=0X04;
P2=0XFF;
P0=tab[san];
Delayms(1);
P2=0XC0;
P0=0X08;
P2=0XFF;
P0=tab[si];
Delayms(1);
}
void shumaguan3(unsigned char wu,unsigned char liu)
{
P2=0XC0;
P0=0X10;
P2=0XFF;
P0=tab[wu];
Delayms(1);
P2=0XC0;
P0=0X20;
P2=0XFF;
P0=tab[liu];
Delayms(1);
}
void shumaguan4(unsigned char qi,unsigned char ba)
{
P2=0XC0;
P0=0X40;
P2=0XFF;
P0=tab[qi];
Delayms(1);
P2=0XC0;
P0=0X80;
P2=0XFF;
P0=tab[ba];
Delayms(1);
}
void Delay500us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 6;
j = 93;
do
{
while (--j);
} while (--i);
}
void Delay80us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 1;
j = 217;
do
{
while (--j);
} while (--i);
}
void chushihua1()
{
DQ=0;
Delay500us();
DQ=1;
Delay500us();
}
void xiechengxu(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ=0;
DQ = dat&0x01;
Delay80us();
DQ = 1;
dat=dat>>1;
}
Delay80us();
}
unsigned char duchengxu()
{
unsigned char i,dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay80us();
}
return dat;
}
unsigned char zhuchengxu()
{
unsigned char di,gao,temp;
chushihua1();
xiechengxu(0xcc);
xiechengxu(0x44);
Delay500us();
chushihua1();
xiechengxu(0xcc);
xiechengxu(0xbe);
Delay500us();
di=duchengxu();
gao=duchengxu();
di=di>>4;
gao=gao<<4;
temp=di|gao;
return temp;
}
void anjian()
{
if(P30==0)
{
Delayms(5);
if(P30==0)
{
while(!P30);
}
a=0;
shezhi=0;
EA=1;
ET0=1;
a=0;
b=0;
c=0;
dda=0;
ximie=0;
}
if(P31==0)
{
Delayms(5);
if(P31==0)
{
while(!P31);
}
if(a==3)
{
dda++;
EA=0;
ET0=0;
P2=0X80;
P0=0XFF;
if(dda==10)
{
dda=9;
}
}
}
if(P32==0)
{
Delayms(5);
if(P32==0)
{
while(!P32);
}
if(a==0)
{
a=1;
}
else
if(a==1)
{
a=0;
}
}
if(P33==0)
{
Delayms(5);
if(P33==0)
{
while(!P33);
}
if(shezhi==1)
{
shezhi=2;
jiangge=5;
qi=jiangge/10;ba=jiangge%10;
}
else
if(shezhi==2)
{
shezhi=3;
jiangge=30;
qi=jiangge/10;ba=jiangge%10;
}
else
if(shezhi==3)
{
shezhi=4;
jiangge=60;
qi=jiangge/10;ba=jiangge%10;
}
else
if(shezhi==4)
{
shezhi=0;
}
}
}
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void tm0_isr() interrupt 1
{
TL0 = 0x00;
TH0 = 0x28;
if(a==3)
{
ddc++;
if(ddc==50)
{
if(sansuo==0)
{
ddc=0;
P2=0X80;
P0=0XFF;
sansuo=1;
}
else
if(sansuo==1)
{
ddc=0;
P2=0X80;
P0=0XFE;
sansuo=0;
}
}
}
if(a==1)
{
add++;
if(add==50)
{
add=0;
if(panding==0)
{
san=10;
liu=10;
panding=1;
}
else
if(panding==1)
{
san=11;
liu=11;
panding=0;
}
}
}
if(a==1)
{
b++;
if(a==1)
{
if(b==jiangge*200)
{
b=0;
wendu=zhuchengxu();
jishu[c]=wendu;
c++;
if(c==10)
{
c=10;
}
}
}
}
}
void main()
{
chushihua();
chushihua2();
Timer0Init();
EA=1;
ET0=1;
while(1)
{
if(a==0) //进入模式1的话
{
if(shezhi==0)
{
jiangge=1;
yi=11,er=11;san=11;si=11;wu=11;liu=10;qi=jiangge/10;ba=jiangge%10;
shezhi=1;
}
}
if(a==1) //进入模式2的话
{
DS1302zhu();
yi=num[2]/10;er=num[2]%10;
si=num[1]/10;wu=num[1]%10;
qi=num[0]/10;ba=num[0]%10;
if(c==10) //如果读取完后进入的模式3
{
a=3;
yi=10;er=0;san=dda;si=11;wu=11;liu=10;
qi=jishu[dda]/10;
ba=jishu[dda]%10;
}
}
if(a==3)
{
yi=10;er=0;san=dda;si=11;wu=11;liu=10;
qi=jishu[dda]/10;
ba=jishu[dda]%10;
}
anjian();
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}