[求助] 用STC89C52RC自带的E2PROM怎样保存新设置的定时时间?

fbzsn   2015-11-22 13:56 楼主
各位仁兄好!,我想用宏晶STC89C52RC自带的E2PROM保存新的定时时间值,使掉电后再接通电源能正常执行原来设定的定时时间,无需再重新设定。以下是我借鉴了别人的一些程序编写的51定时器插座C程序,我把E2PROM相关的读写程序写进去了,由于初学单片机,看了很多例程,还是一头雾水,故请教各位仁兄,需要设定什么变量,在什么地方添加什么代码可完成此项功能设计,以下是我的51定时器插座代码,谢谢指导!!! 【我的定时器实现的功能之一是:比如每天8:00——12:00、14:00——18:00插座接通电源其余时间断开插座电源。如果停电了,等来电时,定时器无需再次设定定时时间,它能自动执行先前设定的定时时间8:00——12:00、14:00——18:00接通电源,使之具有定时掉电记忆功能】,现在是掉电再开机,自动恢复初始设定时间。还望大家耐心指导! #include #include #define uchar unsigned char #define uint unsigned int /*************特殊功能寄存器声明****************/ sfr ISP_DATA=0xe2; //ISP/IAP操作时的数据寄存器 sfr ISP_ADDRH=0xe3; //ISP/IAP操作时存放地址寄存器的高八位 sfr ISP_ADDRL=0xe4; //ISP/IAP操作时存放地址寄存器的第八位 sfr ISP_CMD=0xe5; //命令寄存器 sfr ISP_TRIG=0xe6; //命令触发寄存器 sfr ISP_CONTR=0xe7; //命令寄存器 sbit lcdrs=P2^4; sbit lcdrw=P2^5; sbit lcden=P2^6; sbit inf=P3^3; sbit SCLK=P1^7; sbit IO=P1^6; sbit RST=P1^5; sbit beep=P1^4; sbit P0_2=P1^2; sbit P0_1=P1^1; sbit P0_0=P1^0; sbit s1=P2^0; sbit s2=P2^1; sbit s3=P2^2; sbit s4=P2^3; uchar code table1[]=" : : : "; uchar code table2[]=" : : : "; uchar code table3[]=" Welcome to "; uchar code table4[]=" Timing Sockets "; uchar code table5[]="20 - - "; uchar code table6[]=" : : "; uchar code table7[]="MON"; uchar code table8[]="TUE" ; uchar code table9[]="WED" ; uchar code table10[]="THR"; uchar code table11[]="FRI"; uchar code table12[]="SAT"; uchar code table13[]="SUN"; uchar code table14[]="Set the Calendar"; uchar code table15[]="Set the Mode"; uchar code table16[]="Set the Timing "; uchar code table17[]="Mode Choice"; uchar code table18[]="1 2 3 4 5 6"; uint buf_key_code=0; uint count_T=0,buf_CT=0; uchar key=255; uchar key_bit_count=0; uchar common_code_count=0; uchar inf_status=0; uchar dd[7]; uchar aa[7]={0,0,11,17,4,6,11}; uchar ee[3]={11,0,0}; uchar ff[3]={11,1,0}; uchar kk[2]={11,2}; uchar hh[2]={11,3}; uchar gg[6]; bit flag,flag2,flag3,flag4,flag5,flag6,flag7,flag8,flag9,flag10,flag11,flag12,flag13,flag14; char miao,fen,shi,ri,yue,zhou,nian; uchar sw,gw; uchar s1num,s2num; void display(void); void close_isp_iap(); void delay(uint i) { uint j; for(;i>0;i--) for(j=100;j>0;j--); } /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ 函数:擦除某一扇区(每个扇区512字节) 入口:addr = 某一扇区首地址 ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/ void eraser(uint addr) { // 0x83(晶振<5M) 0x82(晶振<10M) 0x81(晶振<20M) 0x80(晶振<40M) ISP_CONTR = 0x81; // 打开 IAP 功能(ISPEN(ISP_CONTR.7)=1:允许编 程改变Flash, 设置 Flash 操作等待时间。 ISP_CMD = 0x03; // 用户可以对"Data Flash/EEPROM区"进行扇区擦除 ISP_ADDRL = addr; // ISP/IAP操作时的地址寄存器低八位, ISP_ADDRH = addr>>8; // ISP/IAP操作时的地址寄存器高八位。 EA =0; ISP_TRIG = 0x46; // 在ISPEN(ISP_CONTR.7)=1时,对ISP_TRIG先写入46h, ISP_TRIG = 0xB9; // 再写入B9h,ISP/IAP命令才会生效。 _nop_(); close_isp_iap(); // 关闭ISP/IAP } /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ 函数:写一字节 入口:addr = 扇区单元地址 , dat = 待写入数据 ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/ void write(uint addr,uchar dat) { ISP_CONTR = 0x81; ISP_CMD = 0x02; // 用户可以对"Data Flash/EEPROM区"进行字节编程 ISP_ADDRL = addr; ISP_ADDRH = addr>>8; ISP_DATA = dat; // 数据进ISP_DATA EA = 0; ISP_TRIG = 0x46; //先对ISP_TRIG写入46h,再写入b9h ISP/IAP命令才有效 ISP_TRIG = 0xB9; _nop_(); close_isp_iap(); // 关闭ISP/IAP } /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ 函数:读一字节 入口:addr = 扇区单元地址 出口:dat = 读出的数据 ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/ uchar read(uint addr) { uchar dat; ISP_CONTR = 0x81; ISP_CMD = 0x01; // 用户可以对"Data Flash/EEPROM区"进行字节读 ISP_ADDRL = addr; ISP_ADDRH = addr>>8; EA = 0; ISP_TRIG = 0x46; ISP_TRIG = 0xB9; _nop_(); dat = ISP_DATA; // 取出数据 close_isp_iap(); // 关闭ISP/IAP return dat; } /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ 函数:关闭ISP/IAP操作 ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/ void close_isp_iap() { ISP_CONTR = 0; // 关闭IAP功能 ISP_CMD = 0; // 待机模式,无ISP操作 ISP_TRIG = 0; // 关闭IAP功能, 清与ISP有关的特殊功能寄存器 } void write_com(uchar com) //lcd写指令 { lcdrs=0; lcden=0; P0=com; delay(5); lcden=1; delay(5); lcden=0; } void write_data(uchar dat) //lcd写数据 { lcdrs=1; lcden=0; P0=dat; delay(5); lcden=1; delay(5); lcden=0; } void init() //lcd初始化 { uchar num; lcdrw=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); write_com(0x80); write_com(0x01); write_com(0x80); for(num=0;num<16;num++) { write_data(table3[num]); delay(5); } write_com(0xc0); for(num=0;num<16;num++) { write_data(table4[num]); delay(5); } delay(5); write_com(0x01); write_com(0x80); for(num=0;num<8;num++) { write_data(table5[num]); delay(1); } write_com(0xc0); for(num=0;num<8;num++) { write_data(table6[num]); delay(1); } } /*********DS1302程序*********/ void delay1(uint num) { while( --num ); } void DS1302_wbyte(uchar inbyte)//ds1302写数据 { uchar i; for(i=8;i>0;i--) { SCLK=0; if(inbyte&0x01) IO=1; else IO=0; SCLK=1; delay1(1); inbyte=inbyte>>1; } } uchar DS1302_rbyte() //ds1302读数据 { uchar i,temp=0; IO=1; for(i=7;i>0;i--) { SCLK=0; if(IO==1) temp=temp|0x80; else temp=temp&0x7f; SCLK=1; temp=temp>>1; } return (temp); } //----------往ds1302的某个地址写入数据------------- void Write_DS1302(uchar cmd,uchar indata) { RST=1; DS1302_wbyte(cmd); DS1302_wbyte(indata); RST=0; SCLK=0; } //----------读ds1302某地址的的数据----------------- uchar Read_DS1302(uchar addr) { uchar backdata; RST=1; DS1302_wbyte(addr); backdata=DS1302_rbyte();//先写地址,然后读数据 RST=0; SCLK=0; return (backdata); } //------------设置初始时间------------------- void Set_DS1302(uchar addr,uchar *p,uchar n)//写入n个数据 {uchar bb; Write_DS1302(0x8e,0x00);//写控制字,允许写操作 for(;n>0;n--) {sw=(*p)/10; gw=(*p)%10; bb=(sw<<4)|gw; Write_DS1302(addr,bb); p++; addr=addr+2; } Write_DS1302(0x8e,0x80);//写保护,不允许写 } //----------- 读取当前时间------------------- void Read_nowtime(uchar addr,uchar *p,uchar n) { for(;n>0;n--) { *p=Read_DS1302(addr); p++; addr=addr+2; } } void init_DS1302() //ds1302初始化 { RST=0; SCLK=0; Write_DS1302(0x80,0x00);//写控制字,允许写 Write_DS1302(0x90,0x00);//禁止涡流充电 } void write_sfm(uchar add,uchar date) { sw=date/10; gw=date%10; write_com(0x80+0x40+add); write_data(0x30+sw); write_data(0x30+gw); } void write_nyr(uchar add,uchar date) { sw=date/10; gw=date%10; write_com(0x80+add); write_data(0x30+sw); write_data(0x30+gw); } void write_zhou() //星期处理 { uchar num; write_com(0x80+11); switch(zhou) {case 1: for(num=0;num<3;num++) { write_data(table7[num]); delay(1); }; break; case 2: for(num=0;num<3;num++) { write_data(table8[num]); delay(1); }; break; case 3: for(num=0;num<3;num++) { write_data(table9[num]); delay(1); }; break; case 4: for(num=0;num<3;num++) { write_data(table10[num]); delay(1); }; break; case 5: for(num=0;num<3;num++) { write_data(table11[num]); delay(1); }; break; case 6: for(num=0;num<3;num++) { write_data(table12[num]); delay(1); }; break; case 7: for(num=0;num<3;num++) { write_data(table13[num]); delay(1); }; break; default:break; } } void lcall() { uchar num1; write_com(0x01); write_com(0x80); for(num1=0;num1<8;num1++) { write_data(table5[num1]); delay(1); } write_com(0xc0); for(num1=0;num1<8;num1++) { write_data(table6[num1]); delay(1); } display(); } void lcall5() //写入定时开关时间 { uchar num2; write_com(0x01); write_com(0x80); //第一行地址 0x80+0x00=0x80 for(num2=0;num2<15;num2++) { write_data(table1[num2]); delay(1);} write_com(0xc0); //第二行地址 0x80+0x40=0xc0 for(num2=0;num2<15;num2++) { write_data(table2[num2]); delay(1);} //LCD第一行显示数据 write_nyr(0,ee[0]); write_nyr(3,ee[1]); write_nyr(6,ee[2]); write_nyr(0x09,kk[0]); write_nyr(0x0c,kk[1]); //LCD第二行显示数据 write_sfm(0,ff[0]); write_sfm(3,ff[1]); write_sfm(6,ff[2]); write_sfm(0x09,hh[0]); write_sfm(0x0c,hh[1]); } void lcall3() { uchar num3; write_com(0x01); write_com(0x80); for(num3=0;num3<11;num3++) { write_data(table17[num3]); delay(10); } write_com(0xc0); for(num3=0;num3<16;num3++) { write_data(table18[num3]); delay(10); } } /***************按键处理程序*************/ void keyscan() { uchar num; if(s1==0) {delay(5); if(s1==0) { s1num++; flag=1; write_com(0x01); write_com(0x80); while(!s1); switch (s1num) { case 1: for(num=0;num<16;num++) {write_data(table14[num]);write_com(0x0c); delay(1);}; break; case 2: write_com(0x0c);s2num=0; flag3=0; for(num=0;num<16;num++) {write_data(table16[num]);write_com(0x0c); delay(1); } ; break; case 3: for(num=0;num<12;num++) {write_data(table15[num]);write_com(0x0c); delay(1);}; break; case 4: s2num=0;flag6=0;s1num=0;flag=0;flag5=0;write_com(0x0c); for(num=0;num<8;num++) { write_data(table5[num]); delay(1); } write_com(0xc0); for(num=0;num<8;num++) { write_data(table6[num]); delay(1); } break; default:break; } } } if(s1num!=0) { if(s1num==1) { //1 if(s2==0) {delay(5); if(s2==0) {s2num++; if(flag3==0) {lcall();} while(!s2); switch(s2num) { case 1: flag3=1;write_com(0x80+3);write_com(0x0f);break; case 2: write_com(0x80+6);break; case 3: write_com(0x80+9);break; case 4: write_com(0x80+13);break; case 5: write_com(0xc0+1);break; case 6: write_com(0xc0+4);break; case 7: write_com(0xc0+7);break; case 8: flag3=0;s2num=0;write_com(0x0c);Set_DS1302(0x80,aa, 7);break; default:break; } } } if(s2num!=0) {if(s3==0) {delay(5); if(s3==0) {while(!s3); switch(s2num) { case 1:nian++; if(nian==100) nian=0; write_nyr(2,nian);write_com(0x82);aa[6]=nian;Set_DS1302(0x80,aa, 7); break; case 2:yue++; if(yue==13) yue=0; write_nyr(5,yue);write_com(0x85);aa[4]=yue;Set_DS1302(0x80,aa, 7); break; case 3:ri++; if(ri==31) ri=0; write_nyr(8,ri);write_com(0x88);aa[3]=ri;Set_DS1302(0x80,aa, 7); break; case 4:zhou++; if(zhou==8) zhou=1; aa[5]=zhou; write_zhou(); write_com(0x80+13);Set_DS1302(0x80,aa, 7);break; case 5:shi++; if(shi==24) shi=0; write_sfm(0,shi);write_com(0xc0);aa[2]=shi;Set_DS1302(0x80,aa, 7);break; case 6:fen++; if(fen==60) fen=0; write_sfm(3,fen);write_com(0xc3);aa[1]=fen;Set_DS1302(0x80,aa, 7);break; case 7:miao++; if(miao==60) miao=0; write_sfm(6,miao);write_com(0xc6);aa[0]=miao;Set_DS1302(0x80,aa,7);break; default:break; } } } if(s4==0) {delay(5); if(s4==0) {while(!s4); switch(s2num) { case 1:nian--; if(nian==-1) nian=99; write_nyr(2,nian);write_com(0x82);aa[6]=nian;Set_DS1302(0x80,aa, 7); break; case 2:yue--; if(yue==-1) yue=12; write_nyr(5,yue);write_com(0x85);aa[4]=yue; Set_DS1302(0x80,aa, 7);break; case 3:ri--; if(ri==0) ri=31; write_nyr(8,ri);write_com(0x88);aa[3]=ri;Set_DS1302(0x80,aa, 7); break; case 4:zhou--; if(zhou==0) zhou=7; aa[5]=zhou; write_zhou();write_com(0x80+13);Set_DS1302(0x80,aa, 7);break; case 5:shi--; if(shi==-1) shi=23; write_sfm(0,shi);write_com(0xc0);aa[2]=shi;Set_DS1302(0x80,aa, 7);break; case 6:fen--; if(fen==-1) fen=59; write_sfm(3,fen);write_com(0xc3);aa[1]=fen;Set_DS1302(0x80,aa, 7);break; case 7:miao--; if(miao==-1) miao=59; write_sfm(6,miao);write_com(0xc6);aa[0]=miao;Set_DS1302(0x80,aa, 7);break; default:break; } } } }//s2num } //1 if(s1num==2) { if(s2==0) {delay(5); if(s2==0) {s2num++; if(flag6==0) lcall5(); while(!s2); switch(s2num) { case 1: flag6=1;write_com(0x81);write_com(0x0f);break; case 2: write_com(0x84);break; case 3: flag2=1;write_com(0x87);break; case 4: write_com(0xc1);break; case 5: write_com(0xc4);break; case 6: flag4=1;write_com(0xc7);break; case 7: write_com(0x8a);break; case 8: flag13=1;write_com(0x8d);break; case 9: write_com(0xca);break; case 10: flag14=1;write_com(0xcd);break; case 11: flag6=0;s2num=0;write_com(0x0c);break; default: break; } } } if(s2num!=0) { if(s3==0) {delay(5); if(s3==0) {while(!s3); switch(s2num) { case 1:ee[0]+=1; if(ee[0]==24) ee[0]=0; write_nyr(0,ee[0]); write_com(0x80); break; case 2:ee[1]+=1; if(ee[1]==60) ee[1]=0; write_nyr(3,ee[1]);write_com(0x83); break; case 3:ee[2]+=1; if(ee[2]==60) ee[2]=0; write_nyr(6,ee[2]);write_com(0x86); break; case 4:ff[0]+=1; if(ff[0]==24) ff[0]=0; write_sfm(0,ff[0]);write_com(0xc0); break; case 5:ff[1]+=1; if(ff[1]==60) ff[1]=0; write_sfm(3,ff[1]);write_com(0xc3); break; case 6:ff[2]+=1; if(ff[2]==60) ff[2]=0; write_sfm(6,ff[2]);write_com(0xc6); break; case 7:kk[0]+=1; if(kk[0]==24) kk[0]=0; write_nyr(0x09,kk[0]);write_com(0x89); break; case 8:kk[1]+=1; if(kk[1]==60) kk[1]=0; write_nyr(0x0c,kk[1]);write_com(0x8c); break; case 9:hh[0]+=1; if(hh[0]==24) hh[0]=0; write_sfm(0x09,hh[0]);write_com(0xc9); break; case 10:hh[1]+=1; if(hh[1]==60) hh[1]=0; write_sfm(0x0c,hh[1]);write_com(0xcc); break; default:break; } } } if(s4==0) {delay(5); if(s4==0) { while(!s4); switch(s2num) {case 1:ee[0]-=1; if(ee[0]==-1) ee[0]=23; write_nyr(0,ee[0]);write_com(0x80); break; case 2:ee[1]-=1; if(ee[1]==-1) ee[1]=59; write_nyr(3,ee[1]);write_com(0x83); break; case 3:ee[2]-=1; if(ee[2]==-1) ee[2]=59; write_nyr(6,ee[2]);write_com(0x86); break; case 4:ff[0]-=1; if(ff[0]==-1) ff[0]=23; write_sfm(0,ff[0]);write_com(0xc0); break; case 5:ff[1]-=1; if(ff[1]==-1) ff[1]=59; write_sfm(3,ff[1]);write_com(0xc3); break; case 6:ff[2]-=1; if(ff[2]==-1) ff[2]=59; write_sfm(6,ff[2]);write_com(0xc6); break; case 7:kk[0]-=1; if(kk[0]==-1) kk[0]=23; write_nyr(0x09,kk[0]);write_com(0x89); break; case 8:kk[1]-=1; if(kk[1]==-1) kk[1]=59; write_nyr(0x0c,kk[1]);write_com(0x8c); break; case 9:hh[0]-=1; if(hh[0]==-1) hh[0]=23; write_sfm(0x09,hh[0]);write_com(0xc9); break; case 10:hh[1]-=1; if(hh[1]==-1) hh[1]=59; write_sfm(0x0c,hh[1]);write_com(0xcc); break; default:break; } } } } //s2num }//s1num if(s1num==3) { if(s2==0) {delay(5); if(s2==0) {s2num++; if(flag5==0) lcall3(); while(!s2); switch(s2num) { case 1: flag5=1;write_com(0xc0);write_com(0x0f);break; case 2: write_com(0xc3);break; case 3: write_com(0xc6);break; case 4: write_com(0xc9);break; case 5: write_com(0xcc);break; case 6: write_com(0xcf);break; case 7: s2num=0;flag5=0;write_com(0x0c);break; default: break; } } } if(s2num!=0) { if(s3==0) {delay(5); if(s3==0) {while(!s3); switch(s2num) { case 1:gg[0]=fen+10;flag7=1;P0_1=0; P0_2=0;write_com(0x0c); break; case 2:gg[1]=fen+20;flag8=1;P0_1=0; P0_2=0;write_com(0x0c); break; case 3:gg[2]=fen+30;flag9=1;P0_1=0; P0_2=0;write_com(0x0c); break; case 4:gg[3]=fen+40;flag10=1;P0_1=0; P0_2=0;write_com(0x0c); break; case 5:gg[4]=fen+50;flag11=1;P0_1=0; P0_2=0;write_com(0x0c); break; case 6:gg[5]=fen+60;flag11=1;P0_1=0; P0_2=0;write_com(0x0c); break; 吗 default: break; } } } } } } } void dingshi(void) //定时处理 { if((shi==ee[0])&&(fen==ee[1])&&(miao==ee[2])&&(flag2==1)) { flag2=0;P0_1=0;beep=0;delay(1000);beep=1; P0_2=0; } if((shi==ff[0])&&(fen==ff[1])&&(miao==ff[2])&&(flag4==1)) { flag4=0;P0_1=1; beep=0;delay(1000);beep=1;P0_2=1; } if((shi==kk[0])&&(fen==kk[1])&&(flag2==0)&&(flag13==1)) { flag13=0;P0_1=0;beep=0;delay(1000);beep=1;P0_2=0; } if((shi==hh[0])&&(fen==hh[1])&&(flag4==0)&&(flag14==1)) { flag14=0;P0_1=1; beep=0;delay(1000);beep=1;P0_2=1; } if((fen==gg[0])&&(flag7==1)) { flag7=0;P0_1=1;P0_2=1;beep=0;delay(1000);beep=1; } if((fen==gg[1])&&(flag8==1)) { flag8=0;P0_1=1;P0_2=1;beep=0;delay(1000);beep=1; } if((fen==gg[2])&&(flag9==1)) { flag9=0;P0_1=1;P0_2=1;beep=0;delay(1000);beep=1; } if((fen==gg[3])&&(flag10==1)) { flag10=0;P0_1=1;P0_2=1;beep=0;delay(1000);beep=1; } if((fen==gg[4])&&(flag11==1)) { flag11=0;P0_1=1;P0_2=1;beep=0;delay(1000);beep=1; } if((fen==gg[5])&&(flag12==1)) { flag12=0;P0_1=1;P0_2=1;beep=0;delay(1000);beep=1; } } void display(void) //显示时间 { Read_nowtime(0x81,dd,7); miao=dd[0]-(dd[0]>>4)*6; fen=dd[1]-(dd[1]>>4)*6; shi=dd[2]-(dd[2]>>4)*6; ri=dd[3]-(dd[3]>>4)*6; yue=dd[4]-(dd[4]>>4)*6; zhou=dd[5]-(dd[5]>>4)*6; nian=dd[6]-(dd[6]>>4)*6; write_sfm(6,miao); write_sfm(3,fen); write_sfm(0,shi); write_nyr(2,nian); write_nyr(5,yue); write_nyr(8,ri); write_zhou(); } //红外处理部分 void delay10us(unsigned int t) { while(t--); } /********************************************************/ void Time0(void) interrupt 1 { count_T++; if(inf_status==0 && count_T<160) { EX1=0; } else if((inf_status==1||inf_status==2)&&count_T<12) { EX1=0; } else { EX1=1; } if(count_T>500) { inf_status=0; count_T=600; } } /**************************************************/ void int_1(void) interrupt 2 { beep=0; TR0=1; if(count_T>15&&count_T<250) { buf_CT=count_T; count_T=0; } delay10us(10); if(inf==0) { count_T=2; } if(buf_CT>15&&buf_CT<250) { if(inf_status==0) { if(buf_CT>210&&buf_CT<250) { inf_status=1; buf_key_code=0; key_bit_count=0; buf_CT=0; } } else if(inf_status==1) { if((buf_CT>15&&buf_CT<25)||(buf_CT>32&&buf_CT<45)) { common_code_count++; if(common_code_count>=26) { inf_status=2; common_code_count=0; key_bit_count=0; buf_key_code=0; } } } else if(inf_status==2) { if(buf_CT>15&&buf_CT<25) { buf_key_code>>=1; key_bit_count++; } else if(buf_CT>32&&buf_CT<45) { buf_key_code>>=1; buf_key_code|=0x80; key_bit_count++; } if(key_bit_count>=8) { //led1=!led1; inf_status=0; key_bit_count=0; key=buf_key_code; buf_key_code=0; TR0=0; count_T=600; } } buf_CT=0; } beep=1; EX1=1; } void Red_Init(void) //红外初始化 { ET0=1; TMOD=0x02; TH0=0xCA; TL0=0xCA; IT1=1; EX1=1; TR0=0; EA=1; } void Key_Handle(void) //红外按键处理 { switch(key) { case 0x12: P0_1=~P0_1;P0_2=~P0_2; key=255;break; case 0x01: P0_1=0;P0_2=0;flag7=1;gg[0]=fen+10;key=255;break; case 0x02: P0_1=0;P0_2=0;flag8=1;gg[1]=fen+20;key=255;break; case 0x03: P0_1=0;P0_2=0;flag9=1;gg[2]=fen+30;key=255;break; case 0x04: P0_1=0;P0_2=0;flag10=1;gg[3]=fen+40;key=255;break; case 0x05: P0_1=0;P0_2=0;flag11=1;gg[4]=fen+50;key=255;break; case 0x06: P0_1=0;P0_2=0;flag12=1;gg[5]=fen+60;key=255;break; default: key=255;break; }key=255; } void sysinit() //系统初始化 { init(); Red_Init(); init_DS1302(); flag2=0; flag4=0; flag7=0; flag8=0; flag9=0; flag10=0; flag11=0; flag12=0; flag13=0; flag14=0; } void main() //主函数 { sysinit(); //系统初始化 while(1) { EA=0; keyscan(); //按键扫描 if(flag==0) { display(); //显示时间 } dingshi(); //定时处理 EA=1; Key_Handle(); //红外处理 delay(50); } } 本帖最后由 fbzsn 于 2015-11-22 22:33 编辑

回复评论 (4)

不明白你怎么设计的,保存关机时间没有用啊,下次开机什么时间你又不知道.
这种情况有个时钟计数就行了.
点赞  2015-11-22 19:58
建议楼主学习一下时钟芯片的相关内容。典型的芯片是ds1302,电路图和代码容易找到。
点赞  2015-11-22 20:22
谢谢楼上两位师傅的指点!我的定时器实现的功能之一是:比如每天8:00——12:00、14:00——18:00插座接通电源 其余时间断开插座电源。如果停电了,等来电时,定时器无需再次设定定时时间,它能自动执行先前设定的定时时间8:00——12:00、14:00——18:00接通电源使之具有定时掉电记忆功能。 本帖最后由 fbzsn 于 2015-11-22 22:25 编辑
点赞  2015-11-22 22:01
在存储器里找一个地址,记录状态,没有设定时间,地址存的是一个数,设定时间后,存的是另一个数。上电后,检测特定地址存的数据,决定是从新设置,还是以上次设置为准。
点赞  2015-12-1 21:14
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复