#include "reg51.h"
#include"ds1302.h"
#define uchar unsigned char
#define uint unsigned int
sbit k1=P3^4;//按钮
sbit k2=P3^5;
sbit k3=P3^6;
sbit k4=P3^7;
sbit L1=P2^0;//数码管引脚
sbit L2=P2^1;
sbit L3=P2^2;
sbit L4=P2^3;
sbit L5=P2^4;
sbit L6=P2^5;
sbit L7=P2^6;
sbit L8=P2^7;
uchar code smgduan0[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//显示0~9,无小数点
uchar code smgduan1[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//显示0~9,有小数点
uchar time1=0;//定时
uchar mode=0;//模式
//延时
void delay(uint i)
{
while(i--);
}
//主函数
void main()
{
uchar i,k=0;
Ds1302ReadTime();//初始化DS1302
TIME[0]=TIME[0] & 0x7f;
Ds1302Init();
TMOD|=0X01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
while(1)
{
if(!k1)//进入设置
mode=1;
if(mode)//设置
{
if(!k2 &&(k!=2))//小时
{
i=(TIME[2]>>4)*10+(TIME[2]&0X0F);
if(i<23)
i++;
else
i=0;
TIME[2]=(i/10<<4)+i%10;
Ds1302Init();
k=2;
}
if(!k3 &&(k!=3))//分钟
{
i=(TIME[1]>>4)*10+(TIME[1]&0X0F);
if(i<59)
i++;
else
i=0;
TIME[1]=(i/10<<4)+i%10;
Ds1302Init();
k=3;
}
}
if(k2 && k3)
k=0;
if(!k4)//确认
{
mode=0;
}
//显示
if(mode==0)
{
P0=smgduan0[TIME[2]>>4];L1=0;delay(100);L1=1;
P0=smgduan0[TIME[2] & 0x0f];L2=0;delay(100);L2=1;
P0=0x40;L3=0;delay(100);L3=1;
P0=smgduan0[TIME[1]>>4];L4=0;delay(100);L4=1;
P0=smgduan0[TIME[1] & 0x0f];L5=0;delay(100);L5=1;
P0=0x40;L6=0;delay(100);L6=1;
P0=smgduan0[TIME[0]>>4];L7=0;delay(100);L7=1;
P0=smgduan0[TIME[0] & 0x0f];L8=0;delay(100);L8=1;
}
else
{
P0=smgduan0[TIME[2]>>4];L1=0;delay(100);L1=1;
P0=smgduan0[TIME[2] & 0x0f];L2=0;delay(100);L2=1;
P0=smgduan0[TIME[1]>>4];L4=0;delay(100);L4=1;
P0=smgduan0[TIME[1] & 0x0f];L5=0;delay(100);L5=1;
P0=smgduan0[TIME[0]>>4];L7=0;delay(100);L7=1;
P0=smgduan0[TIME[0] & 0x0f];L8=0;delay(100);L8=1;
}
}
}
//定时器0中断
void Timer0() interrupt 1
{
if(time1<50)//读取时间
time1++;
else
{
time1=0;
Ds1302ReadTime();
}
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
}