📄 遥控电子钟.txt
字号:
#include<reg51.h>
sbit P32=P3^2;
sbit Led2=P2^1; /* 数码管采用扫描法驱动,共阳极 */
sbit Led1=P2^2;
sbit Led3=P2^3;
sbit Led4=P2^4;
unsigned char code led_table[]={136, 235, 146, 162, 225, 164, 132, 234, 128, 160,8, 107, 18, 34, 97, 36, 4, 106, 0, 32,255}; /* 数码管驱动表 赋值给P0口 */
unsigned char code month[]={00,31,28,31,30,31,30,31,31,30,31,30,31}; /* 月份编码表 */
unsigned char count=0,xuanze=3;
int timecount=0; /* 用于计时的 */
unsigned char hour=23,min=59,sec=50,day=31,mon=12,year=00,week; /* 看英文就知道了吧 */
void delay(unsigned char d1) /* 延时程序 */
{
unsigned i1,i2;
for(i1=0;i1<d1;i1++)
for(i2=0;i2<255;i2++);
return;
}
void show( unsigned char num1,unsigned char num2) /* 数码管驱动模块 */
{
P2=0;
P0=led_table[num1/10];
Led1=1;
delay(3);
P2=0;
P0=led_table[num1%10];
Led2=1;
delay(3);
P2=0;
P0=led_table[num2/10];
Led3=1;
delay(3);
P2=0;
P0=led_table[num2%10];
Led4=1;
delay(3);
return;
}
main() /* 主程序 */
{
void showtime(); /* 显示时间的子程序 */
void showdate(); /* 显示日期的子程序 */
void showyear(); /* 显示年的子程序 */
void showweek(); /* 显示星期的子程序 */
EA=1;
EX0=1;
IT0=1;
ET1=1;
TH1=6;
TL1=6;
PT1=1;
TR1=1;
TMOD=0x21;
P2=0;
TR0=1;
while(1)
{
/* show(count/100,count%100);*/
switch(count) /* 检测遥控编码执行相应的操作 */
{
case 16:xuanze=0;break;
case 144:xuanze=1;break;
case 80:xuanze=2;break;
case 208:xuanze=3;break;
case 48:xuanze=5;break;
case 160:if(xuanze!=4)
xuanze=4;
else
xuanze=0;
count=0;
break;
default :break;
}
switch(xuanze) /* 选择显示那种信息 */
{
case 0:showtime();break;
case 1:showdate();break;
case 2:showyear();break;
case 3:showweek();break;
case 4: P0=255;P2=255;break;
case 5: show(0,sec);break; /* 显示秒 */
default :break;
}
if(timecount>=4000) /* 时间的进制模块 */
{ timecount=timecount-4000;
if (++sec>=60)
{
sec=0;
if(++min==60)
{ min=0;
if(++hour==24)
{
hour=0;
if(++day>month[mon])
{
if(year%4==0 && mon==2)
{
if(day==month[mon]+1)
{
return;
}
}
day=1;
if(++mon==13)
{
mon=1;
if(++year==100)
{
year=0;
}
}
}
}
}
}
}
}
}
void showtime() /* 显示时间的子程序 */
{
if(count!=0) /* 检测遥控编码执行相应的操作 */
{
if(count==192)
{
if(++hour==24)
{
hour=0;
}
}
if(count==32)
{
if(--hour==255)
{
hour=23;
}
}
if(count==128)
{
if(++min==60)
{
min =0;
}
}
if(count==64)
{
if(--min ==255)
{
min=59;
}
}
count=0;
}
show(hour,min);
return;
}
void showdate() /* 显示日期的子程序 */
{
if(count!=0) /* 检测遥控编码执行相应的操作 */
{
if(count==192)
{
if(++mon==13)
{
mon=1;
}
if (day>month[mon])
day=month[mon];
}
if(count==32)
{
if(--mon==00)
{
mon=12;
}
if (day>month[mon])
day=month[mon];
}
if(count==128)
{
if(++day>month[mon])
{ if(year%4==0 && mon==2)
{
if(day>month[mon]+1)
{
day=1;
}
}
else
{day =1;}
}
}
if(count==64)
{
if(--day ==00)
{
if(year%4==0 && mon==2)
{
day=month[mon]+1;
}
else
{day=month[mon];}
}
}
count=0;
}
show(mon,day);
return;
}
void showyear() /* 显示年的子程序 */
{
if(count!=0) /* 检测遥控编码执行相应的操作 */
{
if(count==192)
{
if(++year==100)
{
year=0;
}
if((mon==2)&&(day==29))
day=28;
}
if(count==32)
{
if(--year==255)
{
year=99;
}
if((mon==2)&&(day==29))
day=28;
}
if(count==128)
{
if(++year==100)
{
year=0;
}
if((mon==2)&&(day==29))
day=28;
}
if(count==64)
{
if(--year==255)
{
year=99;
}
if((mon==2)&&(day==29))
day=28;
}
count=0;
}
show(20,year);
return;
}
void showweek() /* 显示星期的子程序 */
{
unsigned char days=5;
unsigned char i1;
days=days+year+year/4;
for(i1=1;i1<mon;i1++)
{
days=days+month[i1]%7;
}
days=days+day;
if (year>0||mon>2){days++;}
week=days%7;
show(0,week);
}
void keyrupt1() interrupt 0 using 1 /* 读遥控编码的中断 */
{
unsigned char i1;
if(TF0)
{ TF0=0;
for(i1=0;i1<10;i1++)
{
while(!P32);
TH0=235;
TL0=0;
TR0=1;
while(P32)
{
if(TF0)
{return;}
}
}
for(i1=0;i1<8;i1++)
{
count=count<<1;
while(!P32);
TH0=235;
TL0=0;
TR0=1;
while(P32)
{
if(TF0)
{return;}
}
TR0=0;
if(TH0>238)
count++;
}
}
TH0=0;
TL0=0;
TR0=1;
return;
}
void time() interrupt 3 using 2 /* 时间的中断 */
{
timecount++;
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -