📄 text1.c
字号:
#include <AT89X52.H>
unsigned char code SEG7[11]={0xC0,/*0*/
0xF9,/*1*/
0xA4,/*2*/
0xB0,/*3*/
0x99,/*4*/
0x92,/*5*/
0x82,/*6*/
0xF8,/*7*/
0x80,/*8*/
0x90,/*9*/
0xbf,/*-*/
};
unsigned short int tcount;
struct time
{
unsigned char second;
unsigned char minute;
unsigned char hour;
}time1;
void delay(k)
unsigned int k; //延时子程序
{
unsigned char j;
while((k--)!=0)
for(j=0;j<125;j++);
}
TimeUpDown(n,x,y) //时钟调整子程序
unsigned char n,x,y;
{
if(P1_1==0)
{
delay(50);
if(P1_1==0)
{
n++;
if(n==x)
n=0;
while(P1_1==0);
}
}
if(P1_2==0)
{
delay(50);
if(P1_2==0)
{
if(n==0)
n=y;
else
n--;
while(P1_2==0);
}
}
return n;
}
void display(DisSecondH,DisSecondL,DisMinutH,DisMinutL,DisHourH,DisHourL)
unsigned char DisSecondH,DisSecondL,DisMinutH,DisMinutL,DisHourH,DisHourL; //显示子程序
{
P0=SEG7[DisSecondH]; //显示秒钟的十位
P2=0x80;
delay(2);
P0=SEG7[DisSecondL]; //显示分钟的个位
P2=0x40;
delay(2);
P0=0xbf; //显示-
P2=0x20;
delay(2);
P0=SEG7[DisMinutH]; //显示秒钟的十位
P2=0x10;
delay(2);
P0=SEG7[DisMinutL]; //显示分钟的个位
P2=0x08;
delay(2);
P0=0xbf; //显示-
P2=0x04;
delay(2);
P0=SEG7[DisHourH]; //显示小时的个位
P2=0x02;
delay(2);
if(DisHourL==0)
P0=0xff;
else
P0=SEG7[DisHourL]; //显示小时的十位
P2=0x01;
delay(2);
}
main()
{
unsigned char Flag;
TMOD = 0x01;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
ET0 = 1;
EA = 1;
while(1)
{
if(P1_0 == 0) //设置标志位 Flag
{
delay(50);
if(P1_0 == 0)
{
TR0 = 0;
Flag++;
if(Flag > 3)
Flag = 0;
}
while(P1_0 == 0);
}
switch(Flag)
{
case 0:
TR0=1;
display(time1.second%10,time1.second/10,time1.minute%10,time1.minute/10,time1.hour%10,time1.hour/10);
break;
case 1: //时钟小时调整
TR0=0;
time1.hour=TimeUpDown(time1.hour,24,23);
display(10,10,10,10,time1.hour%10,time1.hour/10);
break;
case 2: //时钟分钟调整
time1.minute=TimeUpDown(time1.minute,60,59);
display(10,10,time1.minute%10,time1.minute/10,10,10);
break;
case 3: //时钟秒调整
time1.second=TimeUpDown(time1.second,60,59);
display(time1.second%10,time1.second/10,10,10,10,10);
break;
default:
break;
}
}
}
void t0(void) interrupt 1 using 0
{
tcount++;
if(tcount==10)
{
tcount=0;
time1.second++;
}
if (time1.second>=60)
{
time1.second=0;
time1.minute++;
}
if (time1.minute>=60)
{
time1.minute=0;
time1.hour++;
}
if(time1.hour>=24)
time1.hour=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -