完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本人刚接触单片机,自己焊接了一个51单片机电子钟,能正常走时,就走进偏慢,八个小时会偏慢30分钟,请高手指点,谢谢!
以下是单片机源代码, 51µ¥Æ¬»úµç×ÓÄÖÖÓ³ÌÐò ¼ÓζÈÈÕÆÚÏÔʾ ¾§ÕñƵÂÊ£º12.000M ͨ¹ýÈý¸ö°´¼üÉ趨ʱ·ÖÃëÖµ£¬Í¨¹ýÄÖÁå¼ü¿ÉÉ趨ÄÖÁåʱ¼ä£¬ÄÖÁåÖ»¿ÉÉ趨СʱºÍ·ÖÖÓ ÔÚʱ¼äÏÔʾµÄ״̬Ï°´ÏÂDEC¼ü£¬Çл»µ½Î¶ÈÏÔʾ¡£ ÔÚʱ¼äÏÔʾµÄ״̬Ï°´ÏÂADD¼ü£¬Çл»µ½ÈÕÆÚÏÔʾ¡£ ÔÚÏÔʾʱ¼ä״̬Ï°´SET¼üÉèÖÃʱÖÓ¼°ÄÖÁåʱ¼ä£» ÔÚÈÕÆÚÏÔʾ״̬Ï°´SET¼üÉèÖÃÈÕÆÚ£» ***************************************************************************/ #include #include unsigned char dispbitcnt; //ÊýÂë¹ÜλÂëɨÃè±äÁ¿ char second,AlarmSec; //·Ö±ðÊÇʱÖÓµÄÃë¡¢ÄÖÁ嶨ʱµÄÃë char minite,AlarmMin; //·Ö±ðÊÇʱÖӵķ֡¢ÄÖÁ嶨ʱµÄ·Ö char hour,AlarmH; //·Ö±ðÊÇʱÖÓµÄСʱ¡¢ÄÖÁ嶨ʱµÄСʱ char Year=12,Month=5,Day=1; //Äê Ô ÈÕ±äÁ¿ unsigned int tcnt; //ÃëÐźŲúÉú±äÁ¿ unsigned int tp; //²âµÃµÄζÈÖµ unsigned char mstcnt; //ɨÃèʱ¼ä±äÁ¿ unsigned char SetFlag=0; //SetFlag=1ʱ¿Éµ÷Сʱ£¬=2ʱµ÷·ÖÖÓ£¬ =3ʱµ÷ÃëÖÓ £¬ =4ʱµ÷ÄÖÁåµÄСʱ£¬ =5ʱµ÷ÄÖÁåµÄ·ÖÖÓ bit FlashFlag=1; //FlashFlagΪ1ʱÊýÂë¹ÜÏàӦλµãÁÁ£¬Îª0ʱ²»ÁÁ£¬FlashFlagÔÚ1ºÍ0¼ä±ä»¯ÊýÂë¹Ü¾Í»áÉÁ bit Alarm; //ÄÖÁåÏì״̬µÄ±ê־룬ÄÖÁåʱ¼äµ½Ê±ÖÃ1 //bit AlarmSet; //AlarmSet=1±íʾÉ趨Á˶¨Ê±Ê±¼ä£¬¶¨Ê±µ½Ê±ÄÖÁåÏì bit AlarmFlash; //ÉèÖÃÄÖÁå״̬ʱÉÁ¶¯µÄ±ê־λ bit TempFlag=0,DataFlag=0; //Çл»ÏÔʾζȵıê־λ ºÍ¹¦»»µ½ÈÕÆÚÏÔʾµÄ±ê־λ unsigned int FlashCount; //ÓÃÓÚÉèÖÃÄÖÁåʱÉÁµÄʱ¼ä£¬ÔÚ¶¨Ê±Æ÷2msÖжÏÀïÀÛ¼Ó µ½250ʱΪ0.5s //const unsigned char dispcode[11]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xfd};//0123456789-¶ÎÂ룬ÊýÂë¹ÜÏÔʾÏàÓ¦ÊýʱµÄ¹Ü½Åµçƽ const unsigned char dispcode[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF,0XFF};//0123456789- ¡°È«Ã𡱠¶ÎÂë const unsigned char dispbitcode[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //12345678λÂë £¬ÓÃÀ´ÖжϳÌÐòÖж¯Ì¬É¨ÃèÊýÂë¹Üÿһλ unsigned char dispbuf[8]={0,0,0,0,0,0,0,0};//»º³åÊý×é ***it SET=P2^0; //µ÷ʱµÄ°´¼ü ***it ADD=P2^1; //¼Ó¼ü ***it DEC=P2^2; //¼õ¼ü ***it BEEP=P2^4; //½ÓÄÖÁå·äÃùÆ÷ unsigned char getmonthdays(unsigned int year,unsigned char month)/*µÃµ½Ä³ÔµÄÌìÊý*/ { unsigned char days; switch (month) { case 4: case 6: case 9: case 11:days=30; break; case 2: if(year%4==0) days=29; //ÈòÄê else days=28; break; default:days=31; break; } return days; } //**************TMR0ÖжÏÊýÂë¹ÜɨÃè³ÌÐò********************* // 2msÒ»´ÎÖжÏ,ÖжÏ500´ÎΪ1Ãë void timer0() interrupt 1 { P3=0xff; //ÊýÂë¹ÜÏȲ»ÁÁ P1=0; //λȫ²¿ÏÈÃðÁ㣬·ñÔòÓÐÖØÓ° //µ±É趨Сʱʱ BIT6 7Á½Î»ÊýÂë¹ÜÉÁ¶¯ if(FlashFlag==0&&(SetFlag==1||SetFlag==4||SetFlag==6)) //FlashFlagΪ1ʱÊýÂë¹ÜÏàӦλµãÁÁ£¬Îª0ʱ²»ÁÁ£¬FlashFlagÔÚ1ºÍ0¼ä±ä»¯ÊýÂë¹Ü¾Í»áÉÁ P1=dispbitcode[dispbitcnt]&0x3F; //ÔÚFlashFlag==0ʱµÚ 6 7λÊýÂë¹Ü²»ÁÁ else if(FlashFlag==0&&(SetFlag==2||SetFlag==5||SetFlag==7)) P1=dispbitcode[dispbitcnt]&0xE7; //ÔÚFlashFlag==0ʱµÚ 3 4λÊýÂë¹Ü²»ÁÁ else if(FlashFlag==0&&(SetFlag==3||SetFlag==8)) P1=dispbitcode[dispbitcnt]&0xFC; //ÔÚFlashFlag==0ʱµÚ 0 1λÊýÂë¹Ü²»ÁÁ else if(SetFlag==0&&TempFlag) P1=dispbitcode[dispbitcnt]&0x07; //ÔÚζÈÏÔʾ״̬Ï £¬Ö»ÏÔʾÊýÂë¹Üºó4λ else P1=dispbitcode[dispbitcnt]; P3=dispcode[dispbuf[dispbitcnt]];//¶ÎÂë if(dispbitcnt==1&&TempFlag) //µ±ÏÔʾζÈʱ£¬ÔÚµÚ¶þλÏÔʾСÊýµã P3=P3&0x7F; dispbitcnt++; if(dispbitcnt==8) dispbitcnt=0; tcnt++; //ÖжϲúÉúÃëÐźţ¬2msÔö¼Ó1 if(tcnt==500) //²»ÔÚÉèÖõÄ״̬Ï ÿÃëÃëÖÓÔö¼Ó1 {//½øÈëÒ»ÃëµÄʱ»ù tcnt=0; if(SetFlag==0) second++; if(second==60) {second=0; minite++; if(minite==60) {minite=0; hour++; if(hour==24) {hour=0; Day++; if(Day==getmonthdays(Year,Month)+1) { Day=0; Month++; if(Month==13) { Month=0; Year++; } } } } } if(TempFlag) //ÏÔʾµÄÊÇÎÂ¶È { dispbuf[0]=tp%10%10; dispbuf[1]=tp/10%10; dispbuf[2]=tp/100; dispbuf[3]=0; dispbuf[4]=0; dispbuf[5]=0; dispbuf[6]=0; dispbuf[7]=0; } else if(DataFlag) //ÏÔʾµÄÊÇÈÕÆÚ { dispbuf[0]=Day%10%10; dispbuf[1]=Day/10%10; dispbuf[2]=11; //¶ÎÂë11 ÁîÊýÂë¹Ü²»ÏÔ dispbuf[3]=Month%10; dispbuf[4]=Month/10; dispbuf[5]=11; dispbuf[6]=Year%10; dispbuf[7]=Year/10; } else { if(SetFlag<4) //ÏÔʾµÄÊÇʱ¼ä { dispbuf[0]=second%10; dispbuf[1]=second/10; dispbuf[2]=10; dispbuf[3]=minite%10; dispbuf[4]=minite/10; dispbuf[5]=10; dispbuf[6]=hour%10; dispbuf[7]=hour/10; } else //ÏÔʾµÄÊÇÄÖÁåʱ¼ä { dispbuf[0]=AlarmSec%10; dispbuf[1]=AlarmSec/10; dispbuf[2]=10; dispbuf[3]=AlarmMin%10; dispbuf[4]=AlarmMin/10; dispbuf[5]=10; dispbuf[6]=AlarmH%10; dispbuf[7]=AlarmH/10; } } if(Alarm) // AlarmFlash=!AlarmFlash; //ÄÖÁåʱÿһÃë·äÃùÆ÷ÏìÒ»´Î } if(SetFlag) { FlashCount++; //ÉèÖÃÄÖÁåʱ״̬ÏÂÊýÂë¹ÜÏàӦλÉÁ } else FlashFlag=1; //²»ÉèÖÃÄÖÁåʱÊýÂë¹Ü²»ÉÁ if(FlashCount==251) //FlashCountÿ 2msÔö¼Ó1´Î£¬µ½251Ϊ 0.5s { FlashCount=0; FlashFlag^=1; //ÊýÂë¹ÜÉÁµÄ±ê־룬FlashFlagΪ1ʱÊýÂë¹ÜÿλµãÁÁ£¬Îª0ʱ²»ÁÁ } TMOD=0x01; TH0=0xf8; TL0=0x30; } ///****************1msÑÓʱ³ÌÐò****************** void delay_1ms(unsigned char i) { unsigned char j; for(i=0;i<144;i++) for(j=0;j<144;j++); } ///****************°´¼ü¼ì²â********************* // void key_scan(void) { /*********************°´ÏÂÉèÖüü **********************************/ if(SET==0&&TempFlag==0) //Èç¹ûÔÚÏÔʾʱ¼äµÄ״̬Ï°´ÏÂÁËÉèÖüü £¬ÊýÂë¹ÜÔÚµ÷ʱ·ÖÃë¼äÒƶ¯ { delay_1ms(20); //°´¼üÑÓʱ·À¶¶¶¯ if(SET==0) { SetFlag++; if(SetFlag==4&&DataFlag) SetFlag=0; //µ÷ÈÕÆÚʱ Ö»ÓÐÈýλ¿Éµ÷ else if(SetFlag==6) SetFlag=0; //µ÷ʱ¼äʱÓÐ5λ¿Éµ÷£¬¼´Ê±¡¢·Ö¡¢ÃëºÍÄÖÁåµÄʱ¡¢·Ö } while(SET==0); //µÈ´ýSET¼üµ¯Æð } /*********************ÔÚÕý³£ÏÔʾʱ¼äµÄ״̬Ï°´Ï¼Ӽü Çл»ÎªÈÕÆÚÏÔʾ **********************************/ else if(ADD==0&&SetFlag==0) //ÔÚʱ¼äÏÔʾ״̬Ï°´ÏÂÁ˼Ӽü£¬Çл»µ½ÈÕÆÚÏÔʾ״̬ ÇÒÒªÔÚ·ÇÉ趨״̬Ï { delay_1ms(10); if(ADD==0) { TempFlag=0; //²»ÏÔʾÎÂ¶È DataFlag=!DataFlag; //±ê־λȡ·´ while(ADD==0); //µÈ´ýADD¼üµ¯Æð } } /*********************** ÔÚÏÔʾʱ¼äʱ °´ÁËSETÒ»´Î µ÷Сʱ **************************************/ else if(ADD==0&&DataFlag==0) //ÔÚ²»ÏÔʾÈÕÆÚʱ °´ÁËSETÒ»´Î µ÷Сʱ { delay_1ms(20); //°´¼üÑÓʱ·À¶¶¶¯ if(ADD==0&&SetFlag==1) //ÔÚʱ¼äÏÔʾ״̬Ï°´¼üµ÷ʱ¼Ó { hour++; if(hour==24) { hour=0; } dispbuf[6]=hour%10; //ËÍÊýÂë¹ÜÏÔʾ dispbuf[7]=hour/10; } else if(ADD==0&&SetFlag==2) //°´SETÁ½´Î µ÷·ÖÖÓ { minite++; if(minite==60) {minite=0; } dispbuf[3]=minite%10; dispbuf[4]=minite/10; } else if(ADD==0&&SetFlag==3) //ÔÚʱ¼äÏÔʾ״̬Ï°´¼üµ÷ʱ { second++; if(second==60) {second=0; } dispbuf[0]=second%10; dispbuf[1]=second/10; } else if(ADD==0&&SetFlag==4) //ÔÚÄÖÁåÉ趨״̬Ï°´¼üµ÷ÄÖÁåµÄСʱ { AlarmH++; if(AlarmH==24) AlarmH=0; dispbuf[6]=AlarmH%10; dispbuf[7]=AlarmH/10; } else if(ADD==0&&SetFlag==5) //ÔÚÄÖÁåÉ趨״̬Ï°´¼üµ÷ÄÖÁå·ÖÖÓ { AlarmMin++; if(AlarmMin==60) AlarmMin=0; dispbuf[3]=AlarmMin%10; dispbuf[4]=AlarmMin/10; } while(ADD==0); //µÈ´ýADD¼üµ¯Æð } /*********************************ÔÚÈÕÆÚÏÔʾ״̬Ï°´Ï¼Ӽüµ÷ÈÕÆÚ *********************************************/ else if(ADD==0&&DataFlag)//ÔÚµ÷ÈÕÆڵıê־λΪ1ʱ°´ÏÂADDÊǵ÷ÈÕÆÚ { delay_1ms(20); if(ADD==0&&SetFlag==1) //µ÷ÈÕÆÚ--Äê ÔÚ°´ÁËADDÇÒ°´ÏÂÁËSETÒ»´ÎÇÒÔÚÈÕÆÚÏÔʾµÄ±ê־λΪ1ʱ { Year++; if(Year==100) Year=0; dispbuf[6]=Year%10; dispbuf[7]=Year/10; } else if(ADD==0&&SetFlag==2) //µ÷ÈÕÆÚ--Ô { Month++; if(Month==13) Month=1; dispbuf[3]=Month%10; dispbuf[4]=Month/10; } else if(ADD==0&&SetFlag==3) //µ÷ÈÕÆÚ--ÈÕ { //Day++; if(++Day==getmonthdays(Year,Month)+1) //µÃµ½µ±ÔµÄÌìÊý Day=0; dispbuf[0]=Day%10; dispbuf[1]=Day/10; } while(ADD==0); //µÈ´ýADD¼üµ¯Æð } /*********************¼õ¼ü***********************************/ if (DEC==0&&SetFlag==0) //ÔÚʱ¼äÏÔʾ״̬Ï°´ÏÂÁ˼õ¼ü£¬Çл»µ½Î¶ÈÏÔʾ״̬ { delay_1ms(10); if(DEC==0&&SetFlag==0) { DataFlag=0; //²»ÏÔʾÈÕÆÚ TempFlag=!TempFlag; //±ê־λȡ·´ // init_DS(); //³õʼ»¯Î¶ȴ«¸ÐÆ÷ while(DEC==0); //µÈ´ýADD¼üµ¯Æð } } else if(DEC==0&&DataFlag==0) //ÔÚ²»ÏÔʾÈÕÆÚʱ °´Ï¼õ¼üµ÷ʱ¼ä¼õ { delay_1ms(20); if(DEC==0&&SetFlag==1) //ÔÚʱ¼äÏÔʾ״̬Ï°´¼üµ÷ʱ¼õ { hour--; if(hour==-1) {hour=23; } dispbuf[6]=hour%10; //ËÍÊýÂë¹ÜÏÔʾ dispbuf[7]=hour/10; } else if(DEC==0&&SetFlag==2) //°´SETÁ½´Î µ÷·ÖÖÓ { minite--; if(minite==-1) {minite=59; } dispbuf[3]=minite%10; dispbuf[4]=minite/10; } else if(DEC==0&&SetFlag==3) //ÔÚʱ¼äÏÔʾ״̬Ï°´¼üµ÷ʱ { if(second) //Ãë´óÓÚ0ʱ×Ô¼õ second--; dispbuf[0]=second%10; dispbuf[1]=second/10; } else if(DEC==0&&SetFlag==4) //ÔÚÄÖÁåÉ趨״̬Ï°´¼üµ÷ÄÖÁåµÄСʱ { AlarmH--; if(AlarmH==-1) AlarmH=23; dispbuf[6]=AlarmH%10; dispbuf[7]=AlarmH/10; } else if(DEC==0&&SetFlag==5) //ÔÚÄÖÁåÉ趨״̬Ï°´¼üµ÷ÄÖÁå·ÖÖÓ { AlarmMin--; if(AlarmMin==-1) AlarmMin=59; dispbuf[3]=AlarmMin%10; dispbuf[4]=AlarmMin/10; } } //while(DEC==0); //µÈ´ýADD¼üµ¯Æð /*********************** µ÷ÈÕÆÚ ¼õ****************************************/ else if(DEC==0&&SetFlag==1&&DataFlag) //ÔÚʱ¼äÏÔʾ״̬Ï°´¼üµ÷Äê¼õ { Year--; if(Year==-1) {Year=99; } dispbuf[6]=Year%10; //ËÍÊýÂë¹ÜÏÔʾ dispbuf[7]=Year/10; } else if(DEC==0&&SetFlag==2&&DataFlag) //ÔÚʱ¼äÏÔʾ״̬Ï°´¼üµ÷Ô¼õ { Month--; if(Month==0) {Month=12; } dispbuf[3]=Month%10; //ËÍÊýÂë¹ÜÏÔʾ dispbuf[4]=Month/10; } else if(DEC==0&&SetFlag==3&&DataFlag) //ÔÚʱ¼äÏÔʾ״̬Ï°´¼üµ÷ÈÕ¼õ { Day--; if(Day==-1) {Day=getmonthdays(Year,Month); //µÃµ½µ±ÔµÄÌìÊý } dispbuf[0]=Day%10; //ËÍÊýÂë¹ÜÏÔʾ dispbuf[1]=Day/10; } while(DEC==0); //µÈ´ýADD¼üµ¯Æð } /***************************Çл»µ½ÄÖÁåÉèÖÃ״̬************************** if(P2_3==0&&Alarm==0) //²»ÔÚÄÖÁåÏìʱ°´ÏÂÁËÄÖÁåÉ趨¼ü { delay_1ms(20); if(P2_3==0) //ÔÚʱ¼äÏÔʾ״̬Ï°´¼üµ÷ʱ { SetFlag=!SetFlag; AlarmSet=1; } while(P2_3==0); } } */ //*************Ö÷³ÌÐò******************** void main(void) { TMOD=0x01; TH0=0xf8; TL0=0x30; //É趨¶¨Ê±Æ÷2msÒ»´ÎÖÐ¶Ï dispbitcnt=0; P1=0xFF; //¿ª¶ÎÂë P3=0x0; //¿ªÎ»Âë £¬ÊýÂë¹ÜÈ«ÏÔ delay_1ms(255); //ÑÓʱ delay_1ms(255); //ÑÓʱ delay_1ms(255); //ÑÓʱ delay_1ms(255); //ÑÓʱ P3=0x0; //¹ØλÂë EA=1; //¿ª×ÜÖÐ¶Ï ET0=1; TR0=1; //¿ª¶¨Ê±Æ÷0ÖÐ¶Ï while(1) { key_scan(); //°´¼üɨÃè if(SetFlag==0) { if((AlarmH||AlarmMin)&&hour==AlarmH&&AlarmMin==minite)//Èç¹ûÄÖÁåʱ¼ä²»Îª0ÇÒÄÖÁåʱ¼äÓëµ±µÀʱ·Ö¶¼ÏàµÈ { Alarm=1; //ÄÖÁåÏì״̬ÖÃ1£¬ÔÚÖжÏÿ1Ãë¶ÔAlarmFlashÈ¡·´Ò»´Î£¬ÕâÑùAlarmFlash¸ôÒ»ÃëÔÚ1ºÍ0¼ä±ä»¯£¬·äÃùÆ÷»á²úÉúÏàÓ¦µÄ¼ä¸ôÉù if(AlarmFlash&&second<20) //ÏìÒ»ÃëÍ£Ò»Ãë,Ïì10´Î BEEP=0; //ÄÖÁåÏì else if(second>20) {Alarm=0; //ÄÖÁå״̬ÇåÁã AlarmFlash=0; } else { BEEP=1; //ÄÖÁå²»Ïì } } //else Alarm=0; //ÄÖÁå״̬ÇåÁã if(TempFlag)//Çл»µ½Î¶ÈÏÔʾ { tp=Readtem();//°Ñ¶þλÊý³ËÒÔ10±ä³ÉÈýλÊý } } } } |
|
相关推荐 |
|
你正在撰写讨论
如果你是对讨论或其他讨论精选点评或询问,请使用“评论”功能。
503 浏览 0 评论
求助一下关于51系列单片机的Timer0的计时问题,TH0、TL0+1的时间是怎么算的?
1756 浏览 1 评论
【RA-Eco-RA4E2-64PIN-V1.0开发板试用】开箱+Keil环境搭建+点灯+点亮OLED
1183 浏览 0 评论
【敏矽微ME32G070开发板免费体验】使用coremark测试敏矽微ME32G070 跑分
1053 浏览 0 评论
【敏矽微ME32G070开发板免费体验】开箱+点灯+点亮OLED
1287 浏览 2 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
12022 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 20:59 , Processed in 0.393135 second(s), Total 32, Slave 25 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号