电路原理图如下:
制作出来的实物图如下:
单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include "LED_STRING.h"
- #include "DS1302.h"
- sfr XICON=0xc0; //定义STC89C52中断特殊功能寄存器
- sbit PX3=XICON^7;
- sbit EX3=XICON^6;
- sbit IE3=XICON^5;
- sbit IT3=XICON^4;
- sbit PX2=XICON^3;
- sbit EX2=XICON^2;
- sbit IE2=XICON^1;
- sbit IT2=XICON^0;
- sbit IR=P4^2; //红外
- uchar a[4]; //存储红外解出来的厂家编码和键码值及它们的反码
- uint LowTime,HighTime; //存储高低电平宽度的变量
- uchar L_OnTime,display_flag,time_count,time_count0,time_count1,effect;
- uchar led_0,led_1,display_n,KeyVal;
- bit IR_Flag,display_time_flag,set_second_flag;
- uchar set_count,set_year,set_month,set_day,set_week;
- char set_hour,set_minute,set_second;
- uchar set_year_tmp,set_month_tmp,set_day_tmp,set_week_tmp,set_hour_tmp,set_minute_tmp;
- /*延迟机器周期级的短延迟*/
- void delay(uchar z)
- {
- while (z--)
- _nop_();
- }
- /*开机初始画面*/
- void open_display()
- {
- uchar i;
- uint led_0,led_1,count,num;
- L_OnTime++;
- num++;
- count=num;
- if(num>(16*15))
- {
- display_flag=1;
- TR1=1;
- }
- if(L_OnTime>65) L_OnTime=65;
- for(i=0;i<L_OnTime;i++)
- {
- count--;
- led_0=count*2+1;
- led_1=count*2;
- delay(60);
- P3=tab_open[led_0];
- P1=tab_open[led_1];
- P0=tab_open[led_0];
- P2=tab_open[led_1];
- delay(30);
- P1=0xff;
- P3=0xff;
- P0=0xff;
- P2=0xff;
- delay(3);
- }
- }
- /*LOVE画面*/
- void love_display()
- {
- uchar i;
- uint led_0,led_1,count,num;
- L_OnTime++;
- num++;
- count=num;
- if(num>(16*13)) num=0;
- if(effect==0)
- {
- if(L_OnTime>65) L_OnTime=65;
- }
- else
- {
- if(L_OnTime>125) L_OnTime=125;
- }
- for(i=0;i<L_OnTime;i++)
- {
- count--;
- led_0=count*2+1;
- led_1=count*2;
- delay(60);
- if(effect==0)
- {
- P3=tab_love[led_0];
- P1=tab_love[led_1];
- P0=tab_love[led_0];
- P2=tab_love[led_1];
- }
- else if(effect==1)
- {
- P0=tab_love[led_0];
- P2=tab_love[led_1];
- }
- else
- {
- P3=tab_love[led_0];
- P1=tab_love[led_1];
- }
- delay(30);
- P1=0xff;
- P3=0xff;
- P0=0xff;
- P2=0xff;
- delay(3);
- }
- }
- /*显示“年、月、日、星期”*/
- void time0_display()
- {
- uchar time_count0=8;
- uchar time_count1=16;
- int time_count=159;
- uchar i;
- uchar L_0=159;
- for(i=0;i<L_0;i++)
- {
- if(time_count>143&&time_count<160) //星期值
- {
- led_0=((ReadSet1302(0x8b)&0x0f)-1)*32+(time_count1*2)-1;
- led_1=((ReadSet1302(0x8b)&0x0f)-1)*32+(time_count1*2)-2;
- P3=tab_xingqi[led_0];
- P1=tab_xingqi[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>127&&time_count<144) //“期“
- {
- led_0=3*32+(time_count1*2)-1;
- led_1=3*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- delay(65);
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>111&&time_count<128) //“星“
- {
- led_0=2*32+(time_count1*2)-1;
- led_1=2*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- delay(65);
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>95&&time_count<112) //“日“
- {
- led_0=6*32+(time_count1*2)-1;
- led_1=6*32+(time_count1*2)-2;
- P3=tab_xingqi[led_0];
- P1=tab_xingqi[led_1];
- delay(65);
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>87&&time_count<96) //”日“ 个位
- {
- led_0=((ReadSet1302(0x87)&0x0f)*16)+(time_count0*2)-1;
- led_1=((ReadSet1302(0x87)&0x0f)*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>79&&time_count<88) //”日“ 十位
- {
- led_0=((ReadSet1302(0x87)>>4)*16)+(time_count0*2)-1;
- led_1=((ReadSet1302(0x87)>>4)*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
-
- else if(time_count>63&&time_count<80) //“月“
- {
- led_0=1*32+(time_count1*2)-1;
- led_1=1*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- delay(65);
- if(--time_count1<1) time_count1=16;
- }
-
- else if(time_count>55&&time_count<64) //”月“ 个位
- {
- led_0=((ReadSet1302(0x89)&0x0f)*16)+(time_count0*2)-1;
- led_1=((ReadSet1302(0x89)&0x0f)*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>47&&time_count<56) //”月“ 十位
- {
- led_0=((ReadSet1302(0x89)>>4)*16)+(time_count0*2)-1;
- led_1=((ReadSet1302(0x89)>>4)*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>31&&time_count<48) //“年“
- {
- led_0=0*32+(time_count1*2)-1;
- led_1=0*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- delay(65);
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>23&&time_count<32) //”年“ 个位
- {
- led_0=((ReadSet1302(0x8d)&0x0f)*16)+(time_count0*2)-1;
- led_1=((ReadSet1302(0x8d)&0x0f)*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>15&&time_count<24) //”年“ 十位
- {
- led_0=((ReadSet1302(0x8d)>>4)*16)+(time_count0*2)-1;
- led_1=((ReadSet1302(0x8d)>>4)*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>7&&time_count<16) //"0"
- {
- led_0=(0*16)+(time_count0*2)-1;
- led_1=(0*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- delay(70); //延迟,使得在这里处理的时间与处理1302的时间相同
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>=0&&time_count<8) //"2"
- {
- led_0=(2*16)+(time_count0*2)-1;
- led_1=(2*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- delay(70); //延迟,使得在这里处理的时间与处理1302的时间相同
- if(--time_count0<1) time_count0=8;
- }
- if(--time_count<0)
- {
- time_count=159;
- time_count0=8;
- time_count1=16;
- }
- }
- delay(95);
- P3=0xff;
- P1=0xff;
- delay(5);
- }
- /*显示“时、分、秒”*/
- void time1_display()
- {
- uchar time_count1=8;
- char time_count=79;
- uchar i;
- uchar L_0=79;
- for(i=0;i<L_0;i++)
- {
- if(time_count>63)
- {
- delay(123); //整屏左移
- }
- else if(time_count>55&&time_count<64) //秒个位
- {
- led_0=((ReadSet1302(0x81)&0x0f)*16)+(time_count1*2)-1;
- led_1=((ReadSet1302(0x81)&0x0f)*16)+(time_count1*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- }
- else if(time_count>47&&time_count<56) //秒十位
- {
- led_0=((ReadSet1302(0x81)>>4)*16)+(time_count1*2)-1;
- led_1=((ReadSet1302(0x81)>>4)*16)+(time_count1*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- }
- else if(time_count>39&&time_count<48) //冒号
- {
- led_0=(10*16)+(time_count1*2)-1;
- led_1=(10*16)+(time_count1*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- delay(70); //延迟,使得在这里处理的时间与处理1302的时间相同
- }
- else if(time_count>31&&time_count<40) //分个位
- {
- led_0=((ReadSet1302(0x83)&0x0f)*16)+(time_count1*2)-1;
- led_1=((ReadSet1302(0x83)&0x0f)*16)+(time_count1*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- }
- else if(time_count>23&&time_count<32) //分十位
- {
- led_0=((ReadSet1302(0x83)>>4)*16)+(time_count1*2)-1;
- led_1=((ReadSet1302(0x83)>>4)*16)+(time_count1*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- }
- else if(time_count>15&&time_count<24) //冒号
- {
- led_0=(10*16)+(time_count1*2)-1;
- led_1=(10*16)+(time_count1*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- delay(70);
- }
- else if(time_count>7&&time_count<16) //时个位
- {
- led_0=((ReadSet1302(0x85)&0x0f)*16)+(time_count1*2)-1;
- led_1=((ReadSet1302(0x85)&0x0f)*16)+(time_count1*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- }
- else if(time_count>=0&&time_count<8) //时十位
- {
- led_0=((ReadSet1302(0x85)>>4)*16)+(time_count1*2)-1;
- led_1=((ReadSet1302(0x85)>>4)*16)+(time_count1*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- }
- if(--time_count1<1) time_count1=8;
- if(--time_count<0)
- {
- time_count=79;
- time_count1=8;
- }
- }
- delay(95);
- P3=0xff;
- P1=0xff;
- delay(5);
- }
- /*调“年、月、日、星期、时、分、秒”*/
- void set_time_display()
- {
- uchar time_count0=8;
- uchar time_count1=16;
- int time_count=71;
- uchar i;
- uchar L_0=71;
- switch(set_count)
- {
- case 1:
- {
- for(i=0;i<L_0;i++)
- {
- if(time_count>55)
- {
- delay(85); //整屏左移
- }
- else if(time_count>47&&time_count<56)
- {
- led_0=(set_year%10)*16+(time_count0*2)-1;
- led_1=(set_year%10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>39&&time_count<48)
- {
- led_0=(set_year/10)*16+(time_count0*2)-1;
- led_1=(set_year/10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>31&&time_count<40) //冒号
- {
- led_0=(10*16)+(time_count0*2)-1;
- led_1=(10*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>15&&time_count<32) //“年”
- {
- led_0=0*32+(time_count1*2)-1;
- led_1=0*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>=0&&time_count<16) //“调”
- {
- led_0=4*32+(time_count1*2)-1;
- led_1=4*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- delay(70);
- if(--time_count<0)
- {
- time_count=71;
- time_count0=8;
- time_count1=16;
- }
- }
- }
- break;
- case 2:
- {
- for(i=0;i<L_0;i++)
- {
- if(time_count>55)
- {
- delay(85); //整屏左移
- }
- else if(time_count>47&&time_count<56)
- {
- led_0=(set_month%10)*16+(time_count0*2)-1;
- led_1=(set_month%10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>39&&time_count<48)
- {
- led_0=(set_month/10)*16+(time_count0*2)-1;
- led_1=(set_month/10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>31&&time_count<40) //冒号
- {
- led_0=(10*16)+(time_count0*2)-1;
- led_1=(10*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>15&&time_count<32) //“月”
- {
- led_0=1*32+(time_count1*2)-1;
- led_1=1*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>=0&&time_count<16) //“调”
- {
- led_0=4*32+(time_count1*2)-1;
- led_1=4*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- delay(70);
- if(--time_count<0)
- {
- time_count=71;
- time_count0=8;
- time_count1=16;
- }
- }
- }
- break;
- case 3:
- {
- for(i=0;i<L_0;i++)
- {
- if(time_count>55)
- {
- delay(85); //整屏左移
- }
- else if(time_count>47&&time_count<56)
- {
- led_0=(set_day%10)*16+(time_count0*2)-1;
- led_1=(set_day%10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>39&&time_count<48)
- {
- led_0=(set_day/10)*16+(time_count0*2)-1;
- led_1=(set_day/10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>31&&time_count<40) //冒号
- {
- led_0=(10*16)+(time_count0*2)-1;
- led_1=(10*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>15&&time_count<32) //“日”
- {
- led_0=6*32+(time_count1*2)-1;
- led_1=6*32+(time_count1*2)-2;
- P3=tab_xingqi[led_0];
- P1=tab_xingqi[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>=0&&time_count<16) //“调”
- {
- led_0=4*32+(time_count1*2)-1;
- led_1=4*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- delay(70);
- if(--time_count<0)
- {
- time_count=71;
- time_count0=8;
- time_count1=16;
- }
- }
- }
- break;
- case 4:
- {
- for(i=0;i<L_0;i++)
- {
- if(time_count>55)
- {
- delay(85); //整屏左移
- }
- else if(time_count>39&&time_count<56) //设置星期的值
- {
- led_0=(set_week-1)*32+(time_count1*2)-1;
- led_1=(set_week-1)*32+(time_count1*2)-2;
- P3=tab_xingqi[led_0];
- P1=tab_xingqi[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>31&&time_count<40) //冒号
- {
- led_0=(10*16)+(time_count0*2)-1;
- led_1=(10*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>15&&time_count<32) //“星”
- {
- led_0=2*32+(time_count1*2)-1;
- led_1=2*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>=0&&time_count<16) //“调”
- {
- led_0=4*32+(time_count1*2)-1;
- led_1=4*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- delay(70);
- if(--time_count<0)
- {
- time_count=71;
- time_count0=8;
- time_count1=16;
- }
- }
- }
- break;
- case 5:
- {
- for(i=0;i<L_0;i++)
- {
- if(time_count>55)
- {
- delay(85); //整屏左移
- }
- else if(time_count>47&&time_count<56)
- {
- led_0=(set_hour%10)*16+(time_count0*2)-1;
- led_1=(set_hour%10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>39&&time_count<48)
- {
- led_0=(set_hour/10)*16+(time_count0*2)-1;
- led_1=(set_hour/10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>31&&time_count<40) //冒号
- {
- led_0=(10*16)+(time_count0*2)-1;
- led_1=(10*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>15&&time_count<32) //“时”
- {
- led_0=5*32+(time_count1*2)-1;
- led_1=5*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>=0&&time_count<16) //“调”
- {
- led_0=4*32+(time_count1*2)-1;
- led_1=4*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- delay(70);
- if(--time_count<0)
- {
- time_count=71;
- time_count0=8;
- time_count1=16;
- }
- }
- }
- break;
- case 6:
- {
- for(i=0;i<L_0;i++)
- {
- if(time_count>55)
- {
- delay(85); //整屏左移
- }
- else if(time_count>47&&time_count<56)
- {
- led_0=(set_minute%10)*16+(time_count0*2)-1;
- led_1=(set_minute%10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>39&&time_count<48)
- {
- led_0=(set_minute/10)*16+(time_count0*2)-1;
- led_1=(set_minute/10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>31&&time_count<40) //冒号
- {
- led_0=(10*16)+(time_count0*2)-1;
- led_1=(10*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>15&&time_count<32) //“分”
- {
- led_0=6*32+(time_count1*2)-1;
- led_1=6*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>=0&&time_count<16) //“调”
- {
- led_0=4*32+(time_count1*2)-1;
- led_1=4*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- delay(70);
- if(--time_count<0)
- {
- time_count=71;
- time_count0=8;
- time_count1=16;
- }
- }
- }
- break;
- case 7:
- {
- for(i=0;i<L_0;i++)
- {
- if(time_count>55)
- {
- delay(85); //整屏左移
- }
- else if(time_count>47&&time_count<56)
- {
- led_0=(set_second%10)*16+(time_count0*2)-1;
- led_1=(set_second%10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>39&&time_count<48)
- {
- led_0=(set_second/10)*16+(time_count0*2)-1;
- led_1=(set_second/10)*16+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>31&&time_count<40) //冒号
- {
- led_0=(10*16)+(time_count0*2)-1;
- led_1=(10*16)+(time_count0*2)-2;
- P3=tab_num[led_0];
- P1=tab_num[led_1];
- if(--time_count0<1) time_count0=8;
- }
- else if(time_count>15&&time_count<32) //“秒”
- {
- led_0=7*32+(time_count1*2)-1;
- led_1=7*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- else if(time_count>=0&&time_count<16) //“调”
- {
- led_0=4*32+(time_count1*2)-1;
- led_1=4*32+(time_count1*2)-2;
- P3=tab_time[led_0];
- P1=tab_time[led_1];
- if(--time_count1<1) time_count1=16;
- }
- delay(70);
- if(--time_count<0)
- {
- time_count=71;
- time_count0=8;
- time_count1=16;
- }
- }
- }
- }
- P3=0xff;
- P1=0xff;
- delay(5);
- }
-
- /*红外解码过程函数*/
- bit IRWork()
- {
- uchar i,j;
- uchar dat;
- for(j=0;j<4;j++) //共四个编码
- {
- for(i=0;i<8;i++) //每个编码八位
- {
- dat>>=1; //先移位
- TH0=0;
- TL0=0;
- TR0=1;
- while(!IR); //低电平等待
- TR0=0;
- LowTime=TH0*256+TL0; //将低电平宽度保存
- TH0=0;
- TL0=0;
- TR0=1;
- while(IR); //高电平等待
- TR0=0;
- HighTime=TH0*256+TL0; //将高电平宽度保存
- if(LowTime<380||LowTime>750) return 0; //检测得到的低电平是否在560us左右,如果不是,则返回0;说明解码失败
- if((HighTime>1500)&&(HighTime<1900)) //如果高电平时间在1690微秒左右,(1690-200=1490,1690+200=1890),则该位是1;
- dat=dat|0x80; //是0的情况可判断,也可以不判断了,因为不判断的话上面开始移位后高位已自动清0,在这一行不在高位置1,已说明是0
- }
- a[j]=dat; //将每次解出的编码存到a[]中
- }
- if(a[2]=~a[3]) return 1; //再次判断键码值,如果一致,返回1,解码正确
- }
- void IR_Scan(uchar dat00)
- {
- switch(dat00)
- {
- case 0x46:KeyVal=7;break; //MODE
- case 0x07:KeyVal=8;break; //EQ
- case 0x15:KeyVal=9;break; //--
- case 0x09:KeyVal=10;break; //+
- case 0x19:KeyVal=11;break; //REP
- case 0x0d:KeyVal=12;break; //SCAN
- default:break;
- }
- }
- /*设置时间入口*/
- void KeyScan()
- {
- if(IR_Flag&&(KeyVal==7)) //切换显示时间和日期
- {
- IR_Flag=0; //标志位清零,每次只进入一遍这个函数,即每按一次键加1或减1
- display_time_flag=!display_time_flag;
- TR1=1;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- time_count=0;
- time_count0=0;
- time_count1=0;
- if(display_flag!=1)
- {
- display_flag=1;
- display_time_flag=0;
- }
- if(display_time_flag==1) time_count1=28;
- if(set_count!=0)
- {
- if(set_count==7)
- {
- WriteSet1302(0x80,(((set_second/10)<<4)|(set_second%10)));
- set_second_flag=0;
- }
- WriteSet1302(0x8e,0x80);
- display_time_flag=!display_time_flag;
- set_count=0;
- }
- }
- else if(IR_Flag&&(KeyVal==8)) //设置时间
- {
- IR_Flag=0;
- display_flag=1;
- if(++set_count>8) set_count=0;
- switch(set_count)
- {
- case 1: //设置年
- {
- WriteSet1302(0x8e,0x00); //去掉保护
- set_year_tmp=(ReadSet1302(0x8d)>>4)*10+((ReadSet1302(0x8d))&0x0f); //在当前值设置
- set_year=set_year_tmp;
- }
- break;
- case 2: //设置月
- {
- set_month_tmp=(ReadSet1302(0x89)>>4)*10+((ReadSet1302(0x89))&0x0f);
- set_month=set_month_tmp;
- }
- break;
- case 3: //设置日
- {
- set_day_tmp=(ReadSet1302(0x87)>>4)*10+((ReadSet1302(0x87))&0x0f);
- set_day=set_day_tmp;
- }
- break;
- case 4: //设置星期
- {
- set_week_tmp=ReadSet1302(0x8b);
- set_week=set_week_tmp;
- }
- break;
- case 5: //设置时
- {
- set_hour_tmp=(ReadSet1302(0x85)>>4)*10+((ReadSet1302(0x85))&0x0f);
- set_hour=set_hour_tmp;
- }
- break;
- case 6: //设置分
- {
- set_minute_tmp=(ReadSet1302(0x83)>>4)*10+((ReadSet1302(0x83))&0x0f);
- set_minute=set_minute_tmp;
- }
- break;
- case 8:
- {
- WriteSet1302(0x80,(((set_second/10)<<4)|(set_second%10)));
- WriteSet1302(0x8e,0x80); //写保护
- set_second_flag=0;
- set_count=0;
- time_count1=0;
- }
- break;
- }
- }
- else if(IR_Flag&&(KeyVal==10)) //+
- {
- IR_Flag=0;
- switch(set_count)
- {
- case 1:
- {
- if(++set_year>50) set_year=10;
- }
- break;
- case 2:
- {
- if(++set_month>12) set_month=1;
- }
- break;
- case 3:
- {
- if(++set_day>31) set_day=1;
- }
- break;
- case 4:
- {
- if(++set_week>7) set_week=1;
- }
- break;
- case 5:
- {
- if(++set_hour>23) set_hour=0;
- }
- break;
- case 6:
- {
- if(++set_minute>59) set_minute=0;
- }
- break;
- case 7:
- {
- set_second_flag=1;
- if(++set_second>59) set_second=0;
- }
- break;
- }
- }
- else if(IR_Flag&&(KeyVal==9)) //-
- {
- IR_Flag=0;
- switch(set_count)
- {
- case 1:
- {
- if(--set_year<10) set_year=50;
- }
- break;
- case 2:
- {
- if(--set_month<1) set_month=12;
- }
- break;
- case 3:
- {
- if(--set_day<1) set_day=31;
- }
- break;
- case 4:
- {
- if(--set_week<1) set_week=7;
- }
- break;
- case 5:
- {
- if(--set_hour<0) set_hour=23;
- }
- break;
- case 6:
- {
- if(--set_minute<0) set_minute=59;
- }
- break;
- case 7:
- {
- set_second_flag=1;
- if(--set_second<0) set_second=59;
- }
- }
- }
- else if(IR_Flag&&(KeyVal==11)) //-
- {
- IR_Flag=0;
- display_flag=2;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- time_count=0;
- time_count0=0;
- }
- else if(IR_Flag&&(KeyVal==12)) //-
- {
- IR_Flag=0;
- if(++effect>2) effect=0;
- }
- }
- void set_time()
- {
- switch(set_count)
- {
- case 1:
- {
- if(set_year!=set_year_tmp) WriteSet1302(0x8c,((set_year/10)<<4)|(set_year%10)); //将上个设置数据写入1302
- } break;
- case 2:
- {
- if(set_month!=set_month_tmp) WriteSet1302(0x88,((set_month/10)<<4)|(set_month%10));
- } break;
- case 3:
- {
- if(set_day!=set_day_tmp) WriteSet1302(0x86,((set_day/10)<<4)|(set_day%10));
- } break;
- case 4:
- {
- if(set_week!=set_week_tmp) WriteSet1302(0x8a,set_week);
- } break;
- case 5:
- {
- if(set_hour!=set_hour_tmp) WriteSet1302(0x84,((set_hour/10)<<4)|(set_hour%10));
- } break;
- case 6:
- {
- if(set_minute!=set_minute_tmp) WriteSet1302(0x82,((set_minute/10)<<4)|(set_minute%10));
- } break;
- case 7:
- {
- if(set_second_flag==0)
- {
- set_second=(ReadSet1302(0x81)>>4)*10+((ReadSet1302(0x81))&0x0f);
- }
- else
- {
- WriteSet1302(0x80,((((set_second/10)<<4)|(set_second%10))|0x80)); //秒默认为00分,且先不启动时钟
- }
- } break;
- }
- }
- void main()
- {
- //set_count=1;
- P1=0xff;
- P3=0xff;
- EA=1;
- EX2=1;
- EX3=1;
- ET1=1;
- IT2=0;
- IT3=1; //下降沿触发
- PX2=0;
- PX3=1; //最高优先级
- PT1=1;
- TMOD=0x11;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- Init1302();
- while(1)
- {
- KeyScan();
- set_time();
- EX2=1;
-
- }
- }
- void time1() interrupt 3
- {
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- if(++time_count>=20)
- {
- time_count=0;
- if(set_count==0)
- {
- if(++time_count0>40)
- {
- time_count0=0;
- time_count1=0;
- if(++display_flag>2) display_flag=1;
- }
- time_count1++;
- if(time_count1<28) display_time_flag=0;
- else if(time_count1>27)
- {
- display_time_flag=1;
- if(time_count1>40) time_count1=0;
- }
- }
- }
- }
- void int2_led() interrupt 6
- {
- EX2=0;
- switch(display_flag)
- {
- case 0:open_display();break;
- case 1:
- {
- if(set_count==0)
- {
- if(display_time_flag==0) time1_display(); //显示时间
- else time0_display(); //显示日期
- }
- else set_time_display(); //显示设置状态
- } break;
- case 2:love_display();break;
- }
- EX3=1; //再开外中断INT3
-
- }
- /*红外接收外中断INT0*/
- void int3() interrupt 7
- {
- EX3=0;
- //delay(10);
- if(IR) return; //防止长按时无效尾码的干扰,这个很关键!!!! //暂关T1中断
- TL0=0x00;
- TH0=0x00;
- TR0=1;
- while(!IR); //低电平引导码等待
- TR0=0;
- LowTime=TH0*256+TL0;
- TL0=0x00;
- TH0=0x00;
- TR0=1;
- while(IR); //高电平引导码等待
- TR0=0;
- HighTime=TH0*256+TL0;
- if((LowTime>8250)&&(LowTime<9350)&&(HighTime>3950)&&(HighTime<5050)) //判断是否在合理的范围内
- {
- if(IRWork()&&(a[0]==0x00)) //如果解码正确,且厂家编码为0x04(我的遥控器)才执行子语句,防其它遥控器干扰
- {
- IR_Flag=1;
- IR_Scan(a[2]);
- }
- }
- }
复制代码
所有资料51hei提供下载:
代码.rar
(69.28 KB, 下载次数: 35)
|