直接复制粘贴然后运行
然后打开stc烧录到开发板上面就能用 程序哪里不懂的话问我,我闲的蛋疼!
#include <STC15F2K60S2.H>
#include <intrins.h>
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char shijian[]={50,59,23,0,0,0,0};
unsigned char num[7];
unsigned char shezhi1=0;
unsigned char shezhi2=0;
unsigned char gaibian=0;
unsigned char panding=0;
long a,b;
unsigned char sansuo=0,sansuo1=0,sansuo2=0;
unsigned char naozhong=0;
unsigned char dingshi[]={0,0,0};
unsigned char huan;
unsigned char jia1=0,jia2=0,jia3=0;
unsigned char jian1=0,jian2=0,jian3=0;
unsigned char shi,fen,miao;
unsigned char shi1,fen1,miao1;
unsigned char yanshi=0;
int m;
bit add=0;
bit qiehuan=0;
unsigned char wendu;
sbit DQ = P1^4; //单总线接口
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
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 chushihua2()
{
DQ=0;
Delay500us();
DQ=1;
Delay500us();
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay80us();
DQ = 1;
dat >>= 1;
}
Delay80us();
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char 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;
chushihua2();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay500us();
chushihua2();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
Delay500us();
di=Read_DS18B20();
gao=Read_DS18B20();
di=di>>4;
gao=gao<<4;
temp=di|gao;
return temp;
}
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_();_nop_();_nop_();_nop_();_nop_();_nop_();;
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=1;
_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();_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_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=0;
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SCK=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=0;
dat1=temp/16;
dat2=temp%16;
temp=dat1*10+dat2;
return temp;
}
void chushihua3()
{
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 DS1302()
{
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 anjian()
{
if(P30==0)
{
Delayms(5);
if(P30==0)
{
while(!P30);
}
if(shezhi1==0)
{
panding=1;
shezhi1=1;
}
else
if(shezhi1==1)
{
shezhi1=2;
}
else
if(shezhi1==2)
{
shezhi1=3;
}
else
if(shezhi1==3)
{
panding=0;
shezhi1=0;
}
}
if(P31==0)
{
Delayms(5);
if(P31==0)
{
while(!P31);
}
if(naozhong==0)
{
naozhong=1;
}
if(shezhi2==0)
{
panding=1;
shezhi2=1;
}
else
if(shezhi2==1)
{
shezhi2=2;
}
else
if(shezhi2==2)
{
shezhi2=3;
}
else
if(shezhi2==3)
{
panding=0;
shezhi2=0;
naozhong=0;
}
}
if(P32==0)
{
Delayms(10);
if(P32==0)
{
while(!P32);
}
if(naozhong==1)
{
if(shezhi2==1)
{
dingshi[2]=dingshi[2]+1;
}
if(shezhi2==2)
{
dingshi[1]=dingshi[1]+1;
}
if(shezhi2==3)
{
dingshi[0]=dingshi[0]+1;
}
}
if(naozhong==0)
{
if(shezhi1==1)
{
jia1=jia1+1;
}
if(shezhi1==2)
{
jia2=jia2+1;
}
if(shezhi2==3)
{
jia3=jia3+1;
}
}
}
if(P33==0) //s4按键
{
Delayms(5);
if(P33==0) //判断按键是否按下
{
qiehuan=1;
if(panding==0)
{
while(!P33) //按下按键后执行死循环,知道松手为止。
{
wendu=zhuchengxu();
yi=11;er=11;san=11;si=11;wu=11;
liu=wendu/10;qi=wendu%10;ba=12;
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}
}
while(!P33); //松开后向下执行
qiehuan=0;
if(naozhong==1)
{
if(shezhi2==1)
{
dingshi[2]=dingshi[2]-1;
}
if(shezhi2==2)
{
dingshi[1]=dingshi[1]-1;
}
if(shezhi2==3)
{
dingshi[0]=dingshi[0]-1;
}
}
if(naozhong==0)
{
if(shezhi1==1)
{
jian1=jian1+1;
}
if(shezhi1==2)
{
jian2=jian2+1;
}
if(shezhi2==3)
{
jian3=jian3+1;
}
}
}
}
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;
ET0=1;
}
void tm0_isr() interrupt 1
{
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
a++;
if(a==100)
{
a=0;
if(naozhong==0)
{
if(shezhi1==1)
{
if(sansuo==0)
{
sansuo=1;
yi=11;er=11;
}
else
if(sansuo==1)
{
sansuo=0;
}
}
if(shezhi1==2)
{
if(sansuo==0)
{
sansuo=1;
si=11;wu=11;
}
else
if(sansuo==1)
{
sansuo=0;
}
}
if(shezhi1==3)
{
if(sansuo==0)
{
sansuo=1;
qi=11;ba=11;
}
else
if(sansuo==1)
{
sansuo=0;
}
}
}
if(naozhong==1)
{
if(shezhi2==1)
{
if(sansuo==0)
{
sansuo=1;
yi=11;er=11;
}
else
if(sansuo==1)
{
sansuo=0;
}
}
if(shezhi2==2)
{
if(sansuo==0)
{
sansuo=1;
si=11;wu=11;
}
else
if(sansuo==1)
{
sansuo=0;
}
}
if(shezhi2==3)
{
if(sansuo==0)
{
sansuo=1;
qi=11;ba=11;
}
else
if(sansuo==1)
{
sansuo=0;
}
}
}
}
}
void Timer1Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初值
TH1 = 0x28; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
}
void tm1_isr() interrupt 3
{
TL1 = 0x00; //设置定时初值
TH1 = 0x28; //设置定时初值
m++;
b++;
if(m<=1000)
{
if(b==40)
{
b=0;
if(sansuo1==0)
{
P2=0X80;P0=0XFE;
sansuo1=1;
sansuo2=1;
}
else
if(sansuo1==1)
{
P2=0X80;
P0=0XFF;
sansuo1=0;
}
}
}
else
{
P2=0X80;
P0=0XFF;
}
}
void jjj()
{
if(miao==1)
{
yanshi=1;
}
if(yanshi==1)
{
if(shi>dingshi[2])
{
EA=1;
ET1=1;
}
else
if(fen>dingshi[1])
{
EA=1;
ET1=1;
}
else
if(miao>dingshi[0])
{
EA=1;
ET1=1;
}
}
}
void main()
{
chushihua();
chushihua3();
Timer0Init();
Timer1Init();
while(1)
{
jjj();
shi=num[2]+jia1-jian1; //用于记录1302时钟跑的时间
fen=num[1]+jia2-jian2;
miao=num[0]+jia3-jian3;
if(dingshi[2]>=24)
{
dingshi[2]=dingshi[2]-23; //将自己定的时间转化为新的一天
}
if(dingshi[1]>=60)
{
dingshi[1]=dingshi[1]-59;
dingshi[2]=dingshi[2]+1;
}
if(dingshi[0]>=60)
{
dingshi[0]=dingshi[0]-59;
dingshi[1]=dingshi[1]+1;
}
anjian();
DS1302();
if(naozhong==0)
{
if(qiehuan==0)
{
if(sansuo==0)
{
yi=shi/10;er=shi%10;
}
san=10;
if(sansuo==0)
{
si=fen/10;wu=fen%10;
}
liu=10;
if(sansuo==0)
{
qi=miao/10;ba=miao%10;
}
}
}
if(naozhong==1)
{
if(sansuo==0)
{
yi=dingshi[2]/10;er=dingshi[2]%10;
}
san=10;
if(sansuo==0)
{
si=dingshi[1]/10;wu=dingshi[1]%10;
}
liu=10;
if(sansuo==0)
{
qi=dingshi[0]/10;ba=dingshi[0]%10;
}
}
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}