📄 改建完成.txt
字号:
#include "reg51.h"
#define uchar unsigned char
#define uint unsigned int
#define true 1
#define faluse 1
sbit set_key = P3^0;
sbit up_key = P3^1;
sbit down_key = P3^2;
sbit sign = P3^3;
sbit select_key= P3^4;
sbit set_ring_key = P3^5;
uint sec;
uint min=23;
uint hour=12;
uint day=10;
uint month=10;
uint yearl=7;
uint yearh=20;
uint tcnt;
uint cnt=0;
uint cursor=0;
uint sjsz=0;
uchar a=0xff;
uchar code Seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(uint t)
{
uint i;
while(t--)
{for (i=0;i<125;i++);}
}
void Kdelay()
{
uchar i,j;
for(i=100;i>0;i--)
for(j=248;j>0;j--);
}
void display(uchar L1,uchar L2,uchar L3,uchar L4,uchar L5,uchar L6,uchar L7,uchar L8,uchar L9,uchar L10,uchar L11,uchar L12,uchar L13,uchar L14)
{
if(cnt==1){
if(cursor==6){P2=0x7F|a;P0=L1;delay(1);}else{P2=0x7F;P0=L1;delay(1);} //yearh
if(cursor==6){P2=0xBF|a;P0=L2;delay(1);}else{P2=0xBF;P0=L2;delay(1);} //yearh
if(cursor==6){P2=0xDF|a;P0=L3;delay(1);}else{P2=0xDF;P0=L3;delay(1);} //yearl
if(cursor==6){P2=0xEF|a;P0=L4;delay(1);}else{P2=0xEF;P0=L4;delay(1);} //yearl
if(cursor==5){P2=0xF7|a;P0=L5;delay(1);}else{P2=0xF7;P0=L5;delay(1);} //month
if(cursor==5){P2=0xFB|a;P0=L6;delay(1);}else{P2=0xFB;P0=L6;delay(1);} //month
if(cursor==4){P2=0xFD|a;P0=L7;delay(1);}else{P2=0xFD;P0=L7;delay(1);} //day
if(cursor==4){P2=0xFE|a;P0=L8;delay(1);}else{P2=0xFE;P0=L8;delay(1);} //day
P2=0xFF;}
else{
if(cursor==3){P2=0x7F|a;P0=L9;delay(1);}else{P2=0x7F;P0=L9;delay(1);} //hour
if(cursor==3){P2=0xBF|a;P0=L10;delay(1);}else{P2=0xBF;P0=L10;delay(1);} //hour
if(cnt!=1){P2=0xDF;P0=0X40;delay(1);}
if(cursor==2){P2=0xEF|a;P0=L11;delay(1);}else{P2=0xEF;P0=L11;delay(1);} //min
if(cursor==2){P2=0xF7|a;P0=L12;delay(1);}else{P2=0xF7;P0=L12;delay(1);} //min
if(cnt!=1){P2=0xFB;P0=0X40;delay(1);}
if(cursor==1){P2=0xFD|a;P0=L13;delay(1);}else{P2=0xFD;P0=L13;delay(1);} //sec
if(cursor==1){P2=0xFE|a;P0=L14;delay(1);}else{P2=0xFE;P0=L14;delay(1);} //sec
P1=0xFF;
}}
main()
{
TMOD=0x02; //设置模式为定时器T0的模式2 断的产生中断去执行中断函数本次定时时间为64ms
TH0=0x06; //设置计数器初值,靠TH0存储重装的计数值X0=256-250=6
TL0=0x06;
TR0=1; //启动T0
ET0=1; //开启定时器T0中断允许
EA=1; //开启中断总控制
while(1)
{
if(set_key==0)
{
Kdelay();
if(set_key==0)
{
cursor++;
if(cursor>=7){cursor=0;} //在选择到调整年时又返回秒设置,当CURSOR=7时不选中
}
}
if(up_key==0)
{
Kdelay();
if(up_key==0)
{ //加部分
if(cursor==1){sec++;if(sec==60)sec=0;}
if(cursor==2){min++;if(min==60)min=0;}
if(cursor==3){hour++;if(hour==24)hour=0;}
if(cursor==4){day++;if(day==31)day=1;}
if(cursor==5){month++;if(month==13)month=1;}
if(cursor==6){yearl++;if(yearl==100)yearl=0;}
if(cursor==7){yearh++;if(yearh==30)yearh=20;}
}
}
if(down_key==0)
{
Kdelay();
if(down_key==0)
{ //减部分
if(cursor==1){sec--;if(sec==00)sec=59;}
if(cursor==2){min--;if(min==0)min=59;}
if(cursor==3){hour--;if(hour==0)hour=23;}
if(cursor==4){day--;if(day==0)day=31;}
if(cursor==5){month--;if(month==0)month=12;}
if(cursor==6){yearl--;if(yearl==0)yearl=99;}
if(cursor==7){yearh--;if(yearh==20)yearh=30;}
}
}
if(select_key==0)
{
Kdelay();
if(select_key==0){
cnt++;if(cnt>=2)cnt=0;}}
display(Seg[yearh/10],Seg[yearh%10],Seg[yearl/10],Seg[yearl%10],Seg[month/10],Seg[month%10],Seg[day/10],Seg[day%10],Seg[hour/10],Seg[hour%10],Seg[min/10],Seg[min%10],Seg[sec/10],Seg[sec%10]);
}
}
void t0(void)interrupt 1 using 0 //t0的中断程序
{
tcnt++;
if(tcnt==4000)//定时器的定时计数,4000次250us为1秒,中断4000次后才执行刷新,期间照上面的语句显示
{
tcnt=0;
sign=~sign;
a=~a;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
day++;
if(month==2&&((yearl==0&&yearh%4==0)||(yearl!=0&&yearl%4==0))&&day==30)day=1;
else if(month==2&&day==29)day=1;
else if((month==4||month==6||month==9||month==11)&&day==31)day=1;
else if(day==32)day=1;
if(day==1)
{
month++;
if(month==13)
{
month=1;
yearl++;
if(yearl==100)
{
yearl=0;
yearh++;
if(yearh==100)
{
yearh=20;
}
}
}
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -