📄 闹钟.c
字号:
#include<reg52.h>
#include<stdio.h>
void delay(int a);
int outfunction(int a); //端口输出设置函数
void recount(); //时间计算函数
int alm();
sfr p0=0x80;
sfr p2=0xA0;
sfr p1=0x90;
sbit p13=0x93;
sbit p37=0xb7;
int i,j;
int timecount=0;//用于秒计时器的计数 定时器每250机器周期中断一次,计数4000次就达到1秒
int timecount2=0;//用于计时器误差的校正.
int temp[9],l=0,k=1,m,keyon,keynumber;//用于时间校正的临时变量
int year,month,date,hour,minute,second;
int alm_hour,alm_minute,alm_type,alm_run=1; //闹钟
bit stopb;
bit flagb;
//定义时间初始值
year=2008;
month=00;
date=00;
hour=0;
minute=0;
second=0;
alm_hour=6;
alm_minute=0;
alm_type=0;//默认闹钟关
int cout1[11]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0X40};
int cout2[8]={0XFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F};
///////////////////////////////main()////////////////////////////////////
main()
{
TMOD=0x22;
TH0=0X06;
TL0=0X06;
PT0=1; //定时器0设定为高优先级
PX0=0;//外部中断0设置为低优先级
PX1=0;
ET0=1;
EX0=1;
EX1=1;
EA=1;
TF0=0;
TR0=1;//定时器0准备开始
p37=1;
while(1)
{
p2=cout2[0];
p0=outfunction(hour/10);
delay(5);
p0=0x00;
p2=cout2[1];
p0=outfunction(hour%10);
delay(5);
p0=0x00;
p2=cout2[2];
p0=cout1[10];
delay(7);
p0=0x00;
p2=cout2[3];
p0=outfunction(minute/10);
delay(5);
p0=0x00;
p2=cout2[4];
p0=outfunction(minute%10);
delay(5);
p0=0x00;
p2=cout2[5];
p0=cout1[10];
delay(7);
p0=0x00;
p2=cout2[6];
p0=outfunction(second/10);
delay(5);
p0=0x00;
p2=cout2[7];
p0=outfunction(second%10);
delay(5);
p0=0x00;
if(alm_type==1&&alm_minute==minute&&alm_hour==hour&&alm_run==1) //判断闹钟
alm_run=alm();
if(alm_run==0&&alm_minute!=minute) alm_run=1;
recount();//换算时分秒
}
}
///////////////////////////////计时中断//////////////////////////////////
void Timer0_Overflow() interrupt 1 using 0
{
if(timecount2==4700)
{
timecount++;
timecount2=0;
}
if(timecount<4000)
{
timecount++;
timecount2++;
}
else
{
timecount=0;
second++;
}
}
////////////////////////////显示日期////////////////////////////////////
void int0() interrupt 0 using 0 //中断显示日期
{
j=1000;
while(j--)
{
p2=cout2[0];
p0=outfunction(year/1000);
delay(5);
p0=0x00;
p2=cout2[1];
p0=outfunction((year%1000)/100);
delay(5);
p0=0x00;
p2=cout2[2];
p0=outfunction((year%100)/10);
delay(5);
p0=0x00;
p2=cout2[3];
p0=outfunction(year%10);
delay(5);
p0=0x00;
p2=cout2[4];
p0=outfunction(month/10);
delay(5);
p0=0x00;
p2=cout2[5];
p0=outfunction(month%10);
delay(5);
p0=0x00;
p2=cout2[6];
p0=outfunction(date/10);
delay(5);
p0=0x00;
p2=cout2[7];
p0=outfunction(date%10);
delay(5);
p0=0x00;
recount();//换算时分秒
}
}
///////////////////////////////时间设置////////////////////////////////////
void int1() interrupt 2 using 1
{
temp[3]=year;
temp[4]=month;
temp[5]=date;
temp[6]=alm_hour;
temp[7]=alm_minute;
temp[8]=alm_type;
m=0;
keyon=0;
keynumber=0xff;
while(1)
{
//键盘检测
if(p1==0xff);
else
{
keyon=1;
keynumber=p1;
}
if(keyon==1&&p1==0xff)
{
if(keynumber==0xfe)
{
if(m>=8)
m=0;
else
m++;
}
if(keynumber==0xfd)
{
if(m==0)
{
if(hour<23)
hour++;
else
hour=0;
}
if(m==6)
{
if(temp[m]<23)
temp[m]++;
else
temp[m]=0;
}
if(m==1)
{
if(minute<59)
minute++;
else
minute=0;
}
if(m==2)
{
if(second<59)
second++;
else
second=0;
}
if(m==7)
{
if(temp[m]<59)
temp[m]++;
else
temp[m]=0;
}
//以下3 4 5日期设定
if(m==3) //年
{
if(temp[m]<2032)
temp[m]++;
else
temp[m]=2007;
}
if(m==4) //月
{
if(temp[m]<12)
temp[m]++;
else
temp[m]=1;
}
if(m==5) //日
{
if(temp[4]==1||temp[4]==3||temp[4]==5||temp[4]==7||temp[4]==8||temp[4]==10||temp[4]==12)
{
if(temp[m]<31)
temp[m]++;
else
temp[m]=1;
}
if(temp[4]==4||temp[4]==6||temp[4]==9||temp[4]==11)
{
if(temp[m]<30)
temp[m]++;
else
temp[m]=1;
}
if(temp[4]==2)
{
if(temp[3]%4==0 && temp[3]%100!=0 || temp[3]%400==0)
{
if(temp[m]<29)
temp[m]++;
else
temp[m]=1;
}
if(!(temp[3]%4==0 && temp[3]%100!=0 || temp[3]%400==0))
{
if(temp[m]<28)
temp[m]++;
else
temp[m]=1;
}
}
}
if(m==8) //控制闹钟的开关
{
if(temp[m]==0)
temp[m]=1;
else
temp[m]=0;
}
}
if(keynumber==0xfb)
{
year=temp[3];
month=temp[4];
date=temp[5];
alm_hour=temp[6];
alm_minute=temp[7];
alm_type=temp[8];
break;
}
if(keynumber==0xf7)
{
break;
}
keyon=0;
keynumber=0xff;
}
//以下调节时的显示部分
//时分秒调节
if(m==0||m==1||m==2)
{
if(m==0&&k==-1)
delay(8);
else
{
p2=cout2[0];
p0=outfunction(hour/10);
delay(5);
p0=0x00;
p2=cout2[1];
p0=outfunction(hour%10);
delay(5);
p0=0x00;
}
p2=cout2[2];
p0=cout1[10];
delay(7);
p0=0x00;
if(m==1&&k==-1)
delay(8);
else
{
p2=cout2[3];
p0=outfunction(minute/10);
delay(5);
p0=0x00;
p2=cout2[4];
p0=outfunction(minute%10);
delay(5);
p0=0x00;
}
p2=cout2[5];
p0=cout1[10];
delay(7);
p0=0x00;
if(m==2&&k==-1)
delay(8);
else
{
p2=cout2[6];
p0=outfunction(second/10);
delay(5);
p0=0x00;
p2=cout2[7];
p0=outfunction(second%10);
delay(5);
p0=0x00;
}
if(l==500)
{
k=-k;
l=0;
}
l++;
}
//日期调节
if(m==3||m==4||m==5)
{
if(m==3&&k==-1)
delay(39);
else
{
p2=cout2[0];
p0=outfunction(temp[3]/1000);
delay(5);
p0=0x00;
p2=cout2[1];
p0=outfunction((temp[3]%1000)/100);
delay(5);
p0=0x00;
p2=cout2[2];
p0=outfunction((temp[3]%100)/10);
delay(5);
p0=0x00;
p2=cout2[3];
p0=outfunction(temp[3]%10);
delay(5);
p0=0x00;
}
if(m==4&&k==-1)
delay(8);
else
{
p2=cout2[4];
p0=outfunction(temp[4]/10);
delay(5);
p0=0x00;
p2=cout2[5];
p0=outfunction(temp[4]%10);
delay(5);
p0=0x00;
}
if(m==5&&k==-1)
delay(8);
else
{
p2=cout2[6];
p0=outfunction(temp[5]/10);
delay(5);
p0=0x00;
p2=cout2[7];
p0=outfunction(temp[5]%10);
delay(5);
p0=0x00;
}
if(l==500)
{
k=-k;
l=0;
}
l++;
}
//闹钟调节
if(m==6||m==7||m==8)
{
if(m==6&&k==-1)
delay(8);
else
{
p2=cout2[0];
p0=outfunction(temp[6]/10);
delay(5);
p0=0x00;
p2=cout2[1];
p0=outfunction(temp[6]%10);
delay(5);
p0=0x00;
}
if(m==7&&k==-1)
delay(8);
else
{
p2=cout2[2];
p0=outfunction(temp[7]/10);
delay(5);
p0=0x00;
p2=cout2[3];
p0=outfunction(temp[7]%10);
delay(5);
p0=0x00;
}
p2=cout2[4];
p0=cout1[10];
delay(7);
p0=0x00;
if(m==8&&k==-1)
delay(17);
else
{
if(temp[8]==0)
{
p2=cout2[5];
p0=0x3f;
delay(7);
p0=0x00;
p2=cout2[6];
p0=0x71;
delay(7);
p0=0x00;
p2=cout2[7];
p0=0x71;
delay(7);
p0=0x00;
}
if(temp[8]==1)
{
p2=cout2[5];
p0=0x3f;
delay(7);
p0=0x00;
p2=cout2[6];
p0=0x37;
delay(7);
p0=0x00;
}
}
if(l==500)
{
k=-k;
l=0;
}
l++;
}
recount(); //换算时分秒
}
}
///////////////////////////////模糊延时函数///////////////////////////////////
void delay(int a)
{
for(i=0;i<a;i++);
}
////////////////////////////////输出转换////////////////////////////////
int outfunction(int a)
{
switch(a)
{
case 0:return cout1[0];break;
case 1:return cout1[1];break;
case 2:return cout1[2];break;
case 3:return cout1[3];break;
case 4:return cout1[4];break;
case 5:return cout1[5];break;
case 6:return cout1[6];break;
case 7:return cout1[7];break;
case 8:return cout1[8];break;
case 9:return cout1[9];break;
}
}
///////////////////////////////////时间计算//////////////////////////////////
void recount()
{
if (second==60)
{
second=second%60;
minute++;
if (minute==60)
{
minute=minute%60;
hour++;
if(hour==24)
{
hour=0;
date++;
if(date==29&&month==2&&(!(year%4==0 && year%100!=0 || year%400==0)))
{
date=1;
month++;
}
if(date==30&&month==2&&(year%4==0 && year%100!=0 || year%400==0))
{
date=1;
month++;
}
if(date==31&&(month==4||month==6||month==9||month==11))
{
date=1;
month++;
}
if(date==32&&(month==1||month==3||month==5||month==7||month==8||month==10||month==12))
{
date=1;
month++;
}
if(month==13)
{
month=1;
year++;
}
}
}
}
}
int alm()
{
TH1=0x06;
TL1=0x06;
ET1=1;
EX0=0;
EX1=0;
while(1)
{
p2=cout2[0];
p0=0x77;
delay(5);
p0=0x00;
p2=cout2[1];
p0=0x38;
delay(5);
p0=0x00;
p2=cout2[2];
p0=cout1[10];
delay(7);
p0=0x00;
p2=cout2[3];
p0=outfunction(alm_hour/10);
delay(5);
p0=0x00;
p2=cout2[4];
p0=outfunction(alm_hour%10);
delay(5);
p0=0x00;
p2=cout2[5];
p0=cout1[10];
delay(7);
p0=0x00;
p2=cout2[6];
p0=outfunction(alm_minute/10);
delay(5);
p0=0x00;
p2=cout2[7];
p0=outfunction(alm_minute%10);
delay(5);
p0=0x00;
if(TR1==0)
{
temp[0]=0;
temp[1]=0;
temp[2]=0;
stopb=0;
flagb=0;
TR1=1;
}
if(alm_minute!=minute)
{
TR1=0;
EX0=1;
EX1=1;
p37=1;
return 1;
}
if(p13==0)
{
TR1=0;
EX0=1;
EX1=1;
p37=1;
return 0;
}
recount();
}
}
void Timer1_Overflow() interrupt 3 using 1
{
temp[2]++;
if(temp[2]==2000)
{
temp[2]=0;
if(stopb==0)
{
stopb=~stopb;
}
else
{
stopb=1;
TR1=0;
}
}
if(stopb==0)
{
temp[1]++;
if(temp[1]==3)
{
temp[1]=0;
p37=~p37;
}
}
else
{
temp[0]++;
if(temp[0]==4)
{
temp[0]=0;
p37=~p37;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -