hc6800-es v2.0单片机,用数码管做了个简单时钟,加了外部按键中断进入时间设置的功能。能进入中断,然后出错了,加减总是有点随机的样子,再按设置键好像没用了,还不能返回正常时钟。请教各位大虾是哪出了问题。
- #include<reg52.h>
- #include<intrins.h>
- #define duanxian P0
- sbit keyadd=P3^0; //¼ü붨òå
- sbit keysub=P3^1;
- sbit setkey=P3^3;
- sbit led138c=P2^4; //λѡ
- sbit led138b=P2^3;
- sbit led138a=P2^2;
- char duan[10]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //¶ÎÂë
- char displaybuff[8]={0x00,0x00,0x40,0x00,0x00,0x40,0x00,0x00}; //ÏÔê¾»o3å
- void timeinit(); //¶¨ê±Æ÷03õê¼»ˉ
- void displaydateproc(char,char,char,char,char,char); //ÏÔê¾êy¾Y′|àí
- void displaysetproc(char,char,char,char,char,char); //éèÖÃê±êy¾Y′|àí
- void display(); //ÏÔê¾
- void dely(unsigned int dat){while(dat--);} //Ñóê±
- unsigned long int cnt=0; //¶¨ê±Æ÷¼Æêy
- unsigned int sec=0,min=0,hour=0; //ê±·ÖÃë
- unsigned int setsec=0,setmin=0,sethour=0; //éèÖÃê±Öóê±êy¾Y×a»»
- char temp,tempdate=0x00,tempdate1=0x00; //éá˸êy¾Y×a»»
- unsigned int setflag; //ê±ÖóéèÖñêÖ¾è¡3£¬2£¬1£¬0¡£
- unsigned int shine; //éá˸±êÖ¾
- void main()
- {
- timeinit();
- while(1)
- {
- displaydateproc(duan[sec%10],duan[sec/10],duan[min%10],duan[min/10],duan[hour%10],duan[hour/10]);
- display();
- };
- }
-
- void timeinit()
- {
- TMOD |=0X01;
- TH0=0Xfc;
- TL0=0X18;
- ET0=1;
- TR0=1;
- IT1=1;
- EX1=1;
- EA=1;
-
- }
-
- void time0() interrupt 1
- {
-
- TH0=0Xfc;
- TL0=0X18;
- cnt++;
- if(cnt==963)
- {
- cnt=0;
- sec++;
-
- };
- if(sec>=60){sec=0;min++;};
- if(min>=60){min=0;hour++;}
- if(hour>=24)hour=0;
- }
- void keyset() interrupt 2
- {
- unsigned int n=0;
- dely(1000);
- if(setkey==0)
- {
-
- // EX1=0;
- setflag=3;
- setsec=sec;
- setmin=min;
- sethour=hour;
-
-
- while((n<50)&&(setkey==0)){dely(1000);n++;} ;
- while(setflag!=0)
- {
-
- displaysetproc(duan[setsec%10],duan[setsec/10],duan[setmin%10],duan[setmin/10],duan[sethour%10],duan[sethour/10]);
- display();
- if((setkey==0)||(keyadd==0)||(keysub==0))
- {
- dely(1000);
- if(setkey==0)
- {
- setflag--;
- while((n<50)&&(setkey==0)){dely(1000);n++;} ;
- }
- else if(keyadd==0)
- {
- switch(setflag)
- {
- case 3: sethour++; if(sethour>=24)sethour=0; break;
- case 2: setmin++; if(setmin>=60)setmin=0; break;
- case 1: setsec++; if(setsec>=60)setsec=0; break;
- };
- while((n<50)&&(keyadd==0)){dely(1000);n++;} ;
- }
- else if(keysub==0)
- {
- switch(setflag)
- {
- case 3: sethour--; if(sethour<=0)hour=23; break;
- case 2: setmin--; if(setmin<=60)min=59; break;
- case 1: setsec--; if(setsec<=60)sec=59; break;
- };
- while((n<50)&&(keysub==0)){dely(1000);n++;} ;
- };
- };
- };
-
- sec=setsec;
- min=setmin;
- hour=sethour;
- setflag=0;
-
- //EX1=1;
-
- }
- }
- void displaydateproc(char duansec,char duansec1,char duanmin,char duanmin1,char duanhour,char duanhour1)
- {
- if(shine!=sec)
- {
- shine=sec;
- temp=displaybuff[2];
- displaybuff[2]=tempdate;
- tempdate=temp;
- temp=displaybuff[5];
- displaybuff[5]=tempdate1;
- tempdate1=temp;
- };
- displaybuff[0]=duansec; //êy¾YËíÏÔê¾»o′æ¡£
- displaybuff[1]=duansec1;
- displaybuff[3]=duanmin;
- displaybuff[4]=duanmin1;
- displaybuff[6]=duanhour;
- displaybuff[7]=duanhour1; //
- }
- void displaysetproc(char duansec,char duansec1,char duanmin,char duanmin1,char duanhour,char duanhour1)
- {
- displaybuff[0]=duansec; //êy¾YËíÏÔê¾»o′æ¡£
- displaybuff[1]=duansec1;
- displaybuff[2]=0x40;
- displaybuff[3]=duanmin;
- displaybuff[4]=duanmin1;
- displaybuff[5]=0x40;
- displaybuff[6]=duanhour;
- displaybuff[7]=duanhour1; //
-
- if(shine!=sec) //éá˸′|àíê±·ÖÃë
- {
- shine=sec;
- switch(setflag)
- {
- case 3:
-
- temp=displaybuff[6];
- displaybuff[6]=tempdate;
- tempdate=temp;
- temp=displaybuff[7];
- displaybuff[7]=tempdate1;
- tempdate1=temp;
- break;
- case 2:
-
- temp=displaybuff[4];
- displaybuff[4]=tempdate;
- tempdate=temp;
- temp=displaybuff[3];
- displaybuff[3]=tempdate1;
- tempdate1=temp;
- break;
- case 1:
-
- temp=displaybuff[1];
- displaybuff[1]=tempdate;
- tempdate=temp;
- temp=displaybuff[0];
- displaybuff[0]=tempdate1;
- tempdate1=temp;
- break;
- };
- };
- }
- void display()
- {
-
- unsigned int a;
- for(a=0;a<8;a++) //êyÂë1ü¶ˉì¬é¨Ãè¡£
- {
- switch(a)
- {case(0): led138c=0;led138b=0;led138a=0; break;
- case(1): led138c=0;led138b=0;led138a=1; break;
- case(2): led138c=0;led138b=1;led138a=0; break;
- case(3): led138c=0;led138b=1;led138a=1; break;
- case(4): led138c=1;led138b=0;led138a=0; break;
- case(5): led138c=1;led138b=0;led138a=1; break;
- case(6): led138c=1;led138b=1;led138a=0; break;
- case(7): led138c=1;led138b=1;led138a=1;break;
- };
- duanxian=displaybuff[a];
- dely(100);
- duanxian=0x00; //Ïûòt¡£
- };
- }
复制代码
|