微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 电子时钟+测温+红外遥控调时程序

电子时钟+测温+红外遥控调时程序

时间:11-29 来源:互联网 点击:
/*****************************************************************************************************
作者:徐仁俊2011.5.12,采用1602液晶显示器.红外调时采用“海尔”遥控码 CH+(0xb0),  CH-(0xa0) ,  MENU(0x9c) 分别调节时间******************************************************************************************************/#includesbit dula=P2^6;sbit wela=P2^7;sbit lcdwr=P3^6;sbit lcdrs=P3^5;sbit lcden=P3^4;sbit key1=P3^0;sbit key2=P3^1;sbit key3=P3^3;sbit DQ=P2^2;sbit ir=P3^2;#define uchar unsigned char#define uint unsigned intuchar code table0[]=" 2011-04-03 SUN";//16uchar code table1[]="       00:00:00";//10uchar code table9[]="0123456789";uchar code table2[]="MON";uchar code table3[]="TUE";uchar code table4[]="WED";uchar code table5[]="THU";uchar code table6[]="FRI";uchar code table7[]="SAT";uchar code table8[]="SUN";uchar num,m,n,aa,bb,cc,dd,kk,flag,i,j,flag1,pp,flag2;uchar ss[4];char shi,fen,miao,ee,ff,gg;uchar dat=0;uint dd1,temp;void delay(uchar z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void delay1(uchar i){while(i--);}void write_com(uchar com){lcdrs=0;delay(5);P0=com;delay(5);lcden=1;delay(5);lcden=0; }void write_data(uchar date){lcdrs=1;delay(5);P0=date;delay(5);lcden=1;delay(5);lcden=0;}void init(){dula=0;wela=0;lcdwr=0;lcdrs=1;lcden=0;shi=00;fen=00;miao=00;bb=0;cc=0;dd=0;ee=03;ff=04;gg=11;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x80);write_com(0x01);for(num=0;num<15;num++){ write_data(table0[num]);delay(5);}write_com(0x80+0x40);for(num=0;num<15;num++){ write_data(table1[num]);delay(5);}ss[0]=0;ss[1]=0;ss[2]=0;ss[3]=0;TMOD=0x11;TH0=(65536-50000)/256;TL0=(65536-50000)%256;TH1=0;TL1=0;EA=1;ET0=1;TR0=1;TR1=0;write_com(0x80+0x40+4);write_data(0xdf);write_com(0x80+0x40+5);write_data(C); }uint low_ir_time(){TH1=0;TL1=0;TR1=1;while(!ir);TR1=0;return TH1*256+TL1;}uint high_ir_time(){TH1=0;TL1=0;TR1=1;while(ir);TR1=0;return TH1*256+TL1;}void init_DS18B20(){uchar x;DQ=1;delay1(8);DQ=0;delay1(80);DQ=1;delay1(14);x=DQ;delay1(20);  }void sfm_date(uchar add,uchar date1){ uchar shi,ge;shi=date1/10;ge=date1%10;write_com(0x80+0x40+add);write_data(0x30+shi);write_data(0x30+ge); }void nyr_date1(uchar add,uchar date1){ uchar shi,ge;shi=date1/10;ge=date1%10;write_com(0x80+add);write_data(0x30+shi);write_data(0x30+ge); }read_DS18B20(void){uchar i=0;uchar dat=0;for(i=8;i>0;i--){DQ=0;dat=dat>>1;DQ=1;if(DQ==1){dat=dat|0x80;}delay1(4);}return (dat); }write_DS18B20(uchar dat){uchar i=0;for(i=8;i>0;i--){DQ=0;DQ=dat&0x01;delay1(5);DQ=1;dat=dat>>1;}delay1(4);  }chuli_dat(void){uchar aa=0;uchar bb=0;uchar cc=0;init_DS18B20();write_DS18B20(0xCC);write_DS18B20(0x44);init_DS18B20();write_DS18B20(0xCC);write_DS18B20(0xBE);aa=read_DS18B20();bb=read_DS18B20();cc=(aa>>4)|(bb<4);if(cc>128){cc=~cc+1;write_com(0x80+0x40+0x4a);write_data(0x2d);}return(cc);}void ir_key(){if(pp==0x9c){bb++;pp=0;if(bb==1){TR0=0;write_com(0x80+0x40+13);write_com(0x0f);}if(bb==2){write_com(0x80+0x40+10);}if(bb==3){write_com(0x80+0x40+7);}if(bb==4){write_com(0x80+12); }if(bb==5){write_com(0x80+9);}if(bb==6){write_com(0x80+6);}if(bb==7){write_com(0x80+3);}if(bb==8){bb=0;TR0=1;write_com(0x0c);}}if(bb==1){if(pp==0xb0){miao++;pp=0;if(miao==60){miao=0;}sfm_date(13,miao);write_com(0x80+0x40+13); }if(pp==0xa0){miao--;pp=0;if(miao==-1){miao=59;}sfm_date(13,miao);write_com(0x80+0x40+13); }}if(bb==2){if(pp==0xb0){fen++;pp=0;if(fen==60){fen=0;}sfm_date(10,fen);write_com(0x80+0x40+10); }if(pp==0xa0){fen--;pp=0;if(fen==-1){fen=59;}sfm_date(10,fen);write_com(0x80+0x40+10); }}if(bb==3){if(pp==0xb0){shi++;pp=0;if(shi==24){shi=0;}sfm_date(7,shi);write_com(0x80+0x40+7); }if(pp==0xa0){shi--;pp=0;if(shi==-1){shi=23;}sfm_date(7,shi);write_com(0x80+0x40+7); }}if(bb==4){if(pp==0xb0){dd++;pp=0;if(dd==1){for(cc=0;cc<3;cc++){write_data(table2[cc]);}}write_com(0x80+12);if(dd==2){for(cc=0;cc<3;cc++){write_data(table3[cc]);}}write_com(0x80+12);if(dd==3){for(cc=0;cc<3;cc++){write_data(table4[cc]);}}write_com(0x80+12);if(dd==4){for(cc=0;cc<3;cc++){write_data(table5[cc]);}}write_com(0x80+12);if(dd==5){for(cc=0;cc<3;cc++){write_data(table6[cc]);}}write_com(0x80+12);if(dd==6){for(cc=0;cc<3;cc++){write_data(table7[cc]);}}write_com(0x80+12);if(dd==7){dd=0;for(cc=0;cc<3;cc++){write_data(table8[cc]);}}write_com(0x80+12);}if(pp==0xa0){dd++;pp=0;if(dd==1){for(cc=0;cc<3;cc++){write_data(table8[cc]);}}write_com(0x80+12);if(dd==2){for(cc=0;cc<3;cc++){write_data(table7[cc]);}}write_com(0x80+12);if(dd==3){for(cc=0;cc<3;cc++){write_data(table6[cc]);}}write_com(0x80+12);if(dd==4){for(cc=0;cc<3;cc++){write_data(table5[cc]);}}write_com(0x80+12);if(dd==5){for(cc=0;cc<3;cc++){write_data(table4[cc]);}}write_com(0x80+12);if(dd==6){for(cc=0;cc<3;cc++){write_data(table3[cc]);}}write_com(0x80+12);if(dd==7){dd=0;for(cc=0;cc<3;cc++){write_data(table2[cc]);}}write_com(0x80+12);}}if(bb==5){if(pp==0xb0){ee++;pp=0;if(ee==32){ee=1;}nyr_date1(9,ee);write_com(0x80+9); }if(pp==0xa0){ee--;pp=0;if(ee==0){ee=31;}nyr_date1(9,ee);write_com(0x80+9); } }if(bb==6){if(pp==0xb0){ff++;pp=0;if(ff==13){ff=1;}nyr_date1(6,ff);write_com(0x80+6); }if(pp==0xa0){ff--;pp=0;if(ff==0){ff=12;}nyr_date1(6,ff);write_com(0x80+6); } }if(bb==7){if(pp==0xb0){gg++;pp=0;if(gg==41){gg=11;}nyr_date1(3,gg);write_com(0x80+3); }if(pp==0xa0){gg--;pp=0;if(gg==10){gg=41;}nyr_date1(3,gg);write_com(0x80+3); } }}				   void set_key(){if(key1==0){bb++;delay(8);if(key1==0){if(bb==1){while(!key1);TR0=0;write_com(0x80+0x40+13);write_com(0x0f);}if(bb==2){while(!key1);write_com(0x80+0x40+10);}if(bb==3){while(!key1);write_com(0x80+0x40+7);}if(bb==4){while(!key1);write_com(0x80+12);}if(bb==5){while(!key1);write_com(0x80+9);}if(bb==6){while(!key1);write_com(0x80+6);}if(bb==7){while(!key1);write_com(0x80+3);}if(bb==8){bb=0;while(!key1);TR0=1;write_com(0x0c);}}}if(bb==1){if(key2==0){delay(8);while(!key2);miao++;if(miao==60){miao=0;}sfm_date(13,miao);write_com(0x80+0x40+13); }if(key3==0){delay(8);while(!key3);miao--;if(miao==-1){miao=59;}sfm_date(13,miao);write_com(0x80+0x40+13); }}if(bb==2){if(key2==0){delay(8);while(!key2);fen++;if(fen==60){fen=0;}sfm_date(10,fen);write_com(0x80+0x40+10); }if(key3==0){delay(8);while(!key3);fen--;if(fen==-1){fen=59;}sfm_date(10,fen);write_com(0x80+0x40+10); }}if(bb==3){if(key2==0){delay(8);while(!key2);shi++;if(shi==24){shi=0;}sfm_date(7,shi);write_com(0x80+0x40+7); }if(key3==0){delay(8);while(!key3);shi--;if(shi==-1){shi=23;}sfm_date(7,shi);write_com(0x80+0x40+7); }}if(bb==4){if(key2==0){delay(8);dd++;while(!key2);if(dd==1){for(cc=0;cc<3;cc++){write_data(table2[cc]);}}write_com(0x80+12);if(dd==2){for(cc=0;cc<3;cc++){write_data(table3[cc]);}}write_com(0x80+12);if(dd==3){for(cc=0;cc<3;cc++){write_data(table4[cc]);}}write_com(0x80+12);if(dd==4){for(cc=0;cc<3;cc++){write_data(table5[cc]);}}write_com(0x80+12);if(dd==5){for(cc=0;cc<3;cc++){write_data(table6[cc]);}}write_com(0x80+12);if(dd==6){for(cc=0;cc<3;cc++){write_data(table7[cc]);}}write_com(0x80+12);if(dd==7){dd=0;for(cc=0;cc<3;cc++){write_data(table8[cc]);}}write_com(0x80+12);}if(key3==0){dd++;delay(8);while(!key3);if(dd==1){for(cc=0;cc<3;cc++){write_data(table8[cc]);}}write_com(0x80+12);if(dd==2){for(cc=0;cc<3;cc++){write_data(table7[cc]);}}write_com(0x80+12);if(dd==3){for(cc=0;cc<3;cc++){write_data(table6[cc]);}}write_com(0x80+12);if(dd==4){for(cc=0;cc<3;cc++){write_data(table5[cc]);}}write_com(0x80+12);if(dd==5){for(cc=0;cc<3;cc++){write_data(table4[cc]);}}write_com(0x80+12);if(dd==6){for(cc=0;cc<3;cc++){write_data(table3[cc]);}}write_com(0x80+12);if(dd==7){dd=0;for(cc=0;cc<3;cc++){write_data(table2[cc]);}}write_com(0x80+12);}}if(bb==5){if(key2==0){delay(8);while(!key2);ee++;if(ee==32){ee=1;}nyr_date1(9,ee);write_com(0x80+9); }if(key3==0){delay(8);while(!key3);ee--;if(ee==0){ee=31;}nyr_date1(9,ee);write_com(0x80+9); } }if(bb==6){if(key2==0){delay(8);while(!key2);ff++;if(ff==13){ff=1;}nyr_date1(6,ff);write_com(0x80+6); }if(key3==0){delay(8);while(!key3);ff--;if(ff==0){ff=12;}nyr_date1(6,ff);write_com(0x80+6); } }if(bb==7){if(key2==0){delay(8);while(!key2);gg++;if(gg==41){gg=11;}nyr_date1(3,gg);write_com(0x80+3); }if(key3==0){delay(8);while(!key3);gg--;if(gg==10){gg=41;}nyr_date1(3,gg);write_com(0x80+3); } }}void check_DS18B20(){dd1=chuli_dat();m=dd1/10;n=dd1%10;write_com(0x80+0x40+2);write_data(table9);write_com(0x80+0x40+3); write_data(table9[n]);}void main(){init();while(1){if(ir==0){restart:while(ir);temp=low_ir_time();if((temp<8500)||(temp>9500)) continue;temp=high_ir_time();if((temp<4000)&&(temp>5000)) continue;for(i=0;i<4;i++){for(j=0;j<8;j++){temp=low_ir_time();if((temp<200)||(temp>800)) goto restart;temp=high_ir_time();if((temp<200)||(temp>2000)) goto restart;ss[i]=ss[i]>>1;if(temp>1120){ss[i]=ss[i]|0x80;}}}if(ss[2]=~ss[3]){pp=ss[2];  }}set_key();ir_key();}}void timer0() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;aa++;if(aa==20){aa=0;miao++;check_DS18B20();if(miao==60){miao=0;fen++;if(fen==60){fen=0;shi++;if(shi==24){shi=0;flag=1;}sfm_date(7,shi);}sfm_date(10,fen);}sfm_date(13,miao); }if(flag==1){flag=0;ee++;if(ee==32){ee=1;flag1=1;}nyr_date1(9,ee);//write_com(0x80+9);dd++;if(dd==8){dd=0;}if(dd==1){write_com(0x80+12);for(cc=0;cc<3;cc++){write_data(table2[cc]);}}write_com(0x80+12);if(dd==2){write_com(0x80+12);for(cc=0;cc<3;cc++){write_data(table3[cc]);}}//write_com(0x80+12);if(dd==3){write_com(0x80+12);for(cc=0;cc<3;cc++){write_data(table4[cc]);}}//write_com(0x80+12);if(dd==4){write_com(0x80+12);for(cc=0;cc<3;cc++){write_data(table5[cc]);}}//write_com(0x80+12);if(dd==5){write_com(0x80+12);for(cc=0;cc<3;cc++){write_data(table6[cc]);}}//write_com(0x80+12);if(dd==6){write_com(0x80+12);for(cc=0;cc<3;cc++){write_data(table7[cc]);}}//write_com(0x80+12);if(dd==7){dd=0;write_com(0x80+12);for(cc=0;cc<3;cc++){write_data(table8[cc]);}}// write_com(0x80+12);}if(flag1==1){flag1=0;ff++;if(ff==13){ff=1;flag2=1;}nyr_date1(6,ff);write_com(0x80+6); }if(flag2==1){flag2=0;gg++;if(gg==41){gg=11;}nyr_date1(3,gg);write_com(0x80+3); }}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top