#include <reg51.h>
#include <intrins.h>
void keytime();
void display0();
void displayorign();
void delays(unsigned char ms);
struct time
{
// unsigned char year;
unsigned char day;
unsigned char hour;
unsigned char minute;
unsigned char second;
// unsigned char week;
// unsigned char temperature;
}time_orign,time_set;
void startinit();
void scankey();//按健 0 1 2 对应不同的时间
void time_transform(struct time tt );
unsigned char kk[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; //P2的按位选择码
unsigned char code table[]={0xC0,0xF9,0xA4,0xB0,0x99, //0~4
0x92,0x82,0xF8,0x80,0x90, //5~9
};
unsigned char t[6]; //存放转换完的时间
unsigned char mms;
unsigned char i,j,temp,keycount=0;
// 按健0 显示 月 日 |小时 ?
// 按健1 显示 年 |(月 日)阴历
// 按健2 显示 星期 |温度
main()
{
startinit();
while(1)
{
scankey();
delays(10);
}
}
void startinit()
{
TMOD=0X10;
TH1=0X70; //用TR1定时20毫秒
TL1=0X00;
ET1=1;
EA=1;
TR1=1;
// time_orign.year=2006;
// time_orign.month=01;
time_orign.day=01;
time_orign.hour=12;
time_orign.minute=12;
time_orign.second=12;
// time_orign.week=0;
// time_orign.temperature=20;
time_set=time_orign;
while(1)
{
if (temp==0xee) break;
if (temp==0xeb) break;
displayorign();
}
}
void scankey()
{
unsigned char temp_h,temp_l;
P1=0XF0;
temp_h=P1;
temp_h&=0xf0;
if(temp_h!=0xf0)
{
P1=0X0F;
temp_l=P1;
temp_l&=0x0f;
if(temp_l!=0x0f)
{
temp_h|=temp_l;
temp=temp_h;
while(1)
{
while(temp!=0xeb)
{
display0();
}
keytime();
// if(temp==0xde)
// timeadd();
//display1();
//if(temp==0xdd)
//timereduce();
}
}
}
}
void timer1 () interrupt 3 using 1 //中断服务程序
{
TL1=0X00;
TH1=0X70;
TF1=0;
mms++;
if (mms==50)
{
mms=0;
time_set.second++;
if((time_set.second)==60)
{
time_set.second=0;
time_set.minute++;
if((time_set.minute)==60)
{
time_set.minute=0;
time_set.hour++;
if((time_set.hour)==13) //12小时制
{
time_set.hour=1;
}
}
}
}
}
void time_transform(struct time tt )
{
t[0]=tt.hour/10;
t[1]=tt.hour%10;
t[2]=tt.minute/10;
t[3]=tt.minute%10;
t[4]=tt.second/10;
t[5]=tt.second%10;
}
void display0()
{
unsigned char j;
time_transform(time_set );
for(j=0;j<6;j++)
{
P2=kk[j];
P0=table[t[j]];
delays(1);
P2=0XFF;P0=0XFF;
}
}
void displayorign()
{
unsigned char j;
time_transform(time_orign );
for(j=0;j<6;j++)
{
P2=kk[j];
P0=table[t[j]];
delays(1);
P2=0XFF;P0=0XFF;
}
}
void delays ( unsigned char ms)
{
unsigned char i;
while(ms--)
{
for(i=0;i<200;i++);
}
}
void keytime()
{
unsigned i,j;
keycount++;
if(keycount==1)
for(i=0;i<3;i++)
{
display0();
delays(200);
}
if(keycount==2)
for(j=0;j<6;j++)
{
if(j==0|j==1)
delays(200);
else
P2=kk[j];
P0=table[t[j]];
delays(1);
P2=0XFF;P0=0XFF;
}
if(keycount==3)
for(j=0;j<6;j++)
{
if(j==2|j==3)
delays(200);
else
P2=kk[j];
P0=table[t[j]];
delays(1);
P2=0XFF;P0=0XFF;
}
if(keycount==4)
for(j=0;j<6;j++)
{
if(j==4|j==5)
delays(200);
else
P2=kk[j];
P0=table[t[j]];
delays(1);
P2=0XFF;P0=0XFF;
}
if(keycount==5)
keycount=0;
}