#include<reg52.h>
sbit K1=P3^1; //闹钟设置
sbit K2=P3^0; //数据加1
sbit K3=P3^2; //进入/退出设置
sbit K4=P3^3; //数据减1
//--定义使用的IO口--//
#define GPIO_DIG P0 //段选
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned char code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
unsigned char xianshi[8];//保存时钟的数据
char second,minit,hours;
unsigned char SetPlace;
unsigned char state=0;
unsigned char xs=0;
void yanshi1ms(unsigned int c) //误差 0us
{
unsigned char a,b;
for (; c>0; c--)
{
for(b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void DigDisplay()
{
unsigned char i;
unsigned int j;
for(i=0;i<8;i++)
{
switch(i) //位选,选择点亮的数码管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//显示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//显示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//显示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//显示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//显示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//显示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//显示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//显示第7位
}
GPIO_DIG=xianshi[7-i];//发送段码
j=200; //扫描间隔时间设定
while(j--);
GPIO_DIG=0x00;//消隐
}
}
void shumaguanxianshi()
{
xianshi[5]=0x40;
xianshi[2]=0x40;
if(xs==0)
{
xianshi[7]=seg[second%10];
xianshi[6]=seg[second/10];
xianshi[4]=seg[minit%10];
xianshi[3]=seg[minit/10];
xianshi[1]=seg[hours%10];
xianshi[0]=seg[hours/10];
}
else
{
if(SetPlace==0)
{
xianshi[7]=0x00;
xianshi[6]=0x00;
}
if(SetPlace==1)
{
xianshi[4]=0x00;
xianshi[3]=0x00;
}
if(SetPlace==2)
{
xianshi[1]=0x00;
xianshi[0]=0x00;
}
}
DigDisplay();
}
void main()
{
unsigned int i;
second=50;
minit=59;
hours=23;
TMOD=0X01;
TH0=0Xfc;
TL0=0X18; //1ms
TH1=(65536-2000)/256;
TL1=(65536-2000)%256;
TR0=1;
ET0=1;
EA=1;//打开总中断
while(1)
{
xs=0;
while(state==0)
{
shumaguanxianshi();
if(K3==0)
{
while(K3==0);
state=1;
}
}
xs=0;
ET1=0;TR1=0;
while(state==1)
{
shumaguanxianshi();
if(K3==0)
{
while(K3==0);
SetPlace++;
if(SetPlace>=3)
{
SetPlace=0;
state=0;
}
}
if(K2==0) //检测按键K2是否按下
{
yanshi1ms(10); //消除抖动
if(K2==0)
{
if(SetPlace==0)
{
second++;
if(second>=60)
second=0;
}
else if(SetPlace==1)
{
minit++;
if(minit>=60)
minit=0;
}
else
{
hours++;
if(hours>=24)
hours=0;
}
}
while((K2==0)) //检测按键是否松开
{
yanshi1ms(1);
i++;
}
i=0;
}
if(K4==0) //检测按键K4是否按下
{
yanshi1ms(10); //消除抖动
if(K4==0)
{
if(SetPlace==0)
{
second--;
if(second<0)
second=59;
}
else if(SetPlace==1)
{
minit--;
if(minit<0)
minit=59;
}
else
{
hours--;
if(hours<0)
hours=23;
}
}
while((K4==0)) //检测按键是否松开
{
yanshi1ms(1);
i++;
}
i=0;
}
}
}
}
void time0() interrupt 1
{
static unsigned int i,j;
TH0=0Xfc;
TL0=0X18; //1ms
if(state==0)
{
j++;
if(j==1000) //1s
{
j=0;
second++;
if(second==60) //秒满60清零
{
second=0;
minit++;
if(minit==60) //分满60清零
{
minit=0;
hours++;
if(hours==24)hours=0; //时满60清零
}
}
}
}
else
{
i++;
if(i>=500) //1s
{
i=0;
xs=!xs;
}
}
}
评论0