51单片机电子钟
时间:10-02
整理:3721RD
点击:
本人刚接触单片机,自己焊接了一个51单片机电子钟,能正常走时,就走进偏慢,八个小时会偏慢30分钟,请高手指点,谢谢!
以下是单片机源代码,
51μ¥???úμ?×ó???ó3ìDò ?ó???èè??ú??ê?
?§???μ?ê£o12.000M
í¨1yèy??°′?üéè?¨ê±·????μ£?í¨1y??á??ü?ééè?¨??á?ê±??£???á????ééè?¨D?ê±oí·??ó
?úê±????ê?μ?×′ì???°′??DEC?ü£??D??μ????è??ê??£
?úê±????ê?μ?×′ì???°′??ADD?ü£??D??μ?è??ú??ê??£
?ú??ê?ê±??×′ì???°′SET?üéè??ê±?ó?°??á?ê±??£?
?úè??ú??ê?×′ì???°′SET?üéè??è??ú£?
***************************************************************************/
#include<reg51.h>
#include<DS18B20.h> //°üo?DS18B20?y?ˉ3ìDò
unsigned char dispbitcnt; //êy??1ü????é¨?è±?á?
char second,AlarmSec; //·?±eê?ê±?óμ????¢??á??¨ê±μ???
char minite,AlarmMin; //·?±eê?ê±?óμ?·??¢??á??¨ê±μ?·?
char hour,AlarmH; //·?±eê?ê±?óμ?D?ê±?¢??á??¨ê±μ?D?ê±
char Year=12,Month=5,Day=1; //?ê ?? è?±?á?
unsigned int tcnt; //??D?o?2úéú±?á?
unsigned int tp; //2aμ?μ????è?μ
unsigned char mstcnt; //é¨?èê±??±?á?
unsigned char SetFlag=0; //SetFlag=1ê±?éμ÷D?ê±£?=2ê±μ÷·??ó£? =3ê±μ÷???ó £? =4ê±μ÷??á?μ?D?ê±£? =5ê±μ÷??á?μ?·??ó
bit FlashFlag=1; //FlashFlag?a1ê±êy??1ü?àó|??μ?áá£??a0ê±2?áá£?FlashFlag?ú1oí0??±??ˉêy??1ü?í?áéá
bit Alarm; //??á??ì×′ì?μ?±ê????£???á?ê±??μ?ê±??1
//bit AlarmSet; //AlarmSet=1±íê?éè?¨á??¨ê±ê±??£??¨ê±μ?ê±??á??ì
bit AlarmFlash; //éè????á?×′ì?ê±éá?ˉμ?±ê????
bit TempFlag=0,DataFlag=0; //?D????ê????èμ?±ê???? oí1|??μ?è??ú??ê?μ?±ê????
unsigned int FlashCount; //ó?óúéè????á?ê±éáμ?ê±??£??ú?¨ê±?÷2ms?D??à?à??ó μ?250ê±?a0.5s
//const unsigned char dispcode[11]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xfd};//0123456789-????£?êy??1ü??ê??àó|êyê±μ?1ü??μ???
const unsigned char dispcode[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF,0XFF};//0123456789- ?°è??e?± ????
const unsigned char dispbitcode[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //12345678???? £?ó?à′?D??3ìDò?D?ˉì?é¨?èêy??1ü??ò???
unsigned char dispbuf[8]={0,0,0,0,0,0,0,0};//?o3?êy×é
sbit SET=P2^0; //μ÷ê±μ?°′?ü
sbit ADD=P2^1; //?ó?ü
sbit DEC=P2^2; //???ü
sbit BEEP=P2^4; //?ó??á?·??ù?÷
unsigned char getmonthdays(unsigned int year,unsigned char month)/*μ?μ??3??μ?ììêy*/
{
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?D??êy??1üé¨?è3ìDò*********************
// 2msò?′??D??,?D??500′??a1??
void Timer0() interrupt 1
{
P3=0xff; //êy??1ü?è2?áá
P1=0; //??è?2??è?eá?£?·??òóD??ó°
//μ±éè?¨D?ê±ê± BIT6 7á???êy??1üéá?ˉ
if(FlashFlag==0&&(SetFlag==1||SetFlag==4||SetFlag==6)) //FlashFlag?a1ê±êy??1ü?àó|??μ?áá£??a0ê±2?áá£?FlashFlag?ú1oí0??±??ˉêy??1ü?í?áéá
P1=dispbitcode[dispbitcnt]&0x3F; //?úFlashFlag==0ê±μú 6 7??êy??1ü2?áá
else if(FlashFlag==0&&(SetFlag==2||SetFlag==5||SetFlag==7))
P1=dispbitcode[dispbitcnt]&0xE7; //?úFlashFlag==0ê±μú 3 4??êy??1ü2?áá
else if(FlashFlag==0&&(SetFlag==3||SetFlag==8))
P1=dispbitcode[dispbitcnt]&0xFC; //?úFlashFlag==0ê±μú 0 1??êy??1ü2?áá
else if(SetFlag==0&&TempFlag)
P1=dispbitcode[dispbitcnt]&0x07; //?ú???è??ê?×′ì??? £?????ê?êy??1üoó4??
else P1=dispbitcode[dispbitcnt];
P3=dispcode[dispbuf[dispbitcnt]];//????
if(dispbitcnt==1&&TempFlag) //μ±??ê????èê±£??úμú?t????ê?D?êyμ?
P3=P3&0x7F;
dispbitcnt++;
if(dispbitcnt==8) dispbitcnt=0;
tcnt++; //?D??2úéú??D?o?£?2ms???ó1
if(tcnt==500) //2??úéè??μ?×′ì??? ???????ó???ó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 á?êy??1ü2???
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++; //éè????á?ê±×′ì???êy??1ü?àó|??éá
}
else FlashFlag=1; //2?éè????á?ê±êy??1ü2?éá
if(FlashCount==251) //FlashCount?? 2ms???ó1′?£?μ?251?a 0.5s
{
FlashCount=0;
FlashFlag^=1; //êy??1üéáμ?±ê????£?FlashFlag?a1ê±êy??1ü????μ?áá£??a0ê±2?áá
}
TMOD=0x01;
TH0=0xf8;
TL0=0x30;
}
///****************1ms?óê±3ìDò******************
void delay_1ms(unsigned char i)
{
unsigned char j;
for(i=0;i<144;i++)
for(j=0;j<144;j++);
}
///****************°′?ü?ì2a*********************
//
void key_scan(void)
{
/*********************°′??éè???ü **********************************/
if(SET==0&&TempFlag==0) //è?1??ú??ê?ê±??μ?×′ì???°′??á?éè???ü £?êy??1ü?úμ÷ê±·?????ò??ˉ
{
delay_1ms(20); //°′?ü?óê±·à???ˉ
if(SET==0)
{
SetFlag++;
if(SetFlag==4&&DataFlag) SetFlag=0; //μ÷è??úê± ??óDèy???éμ÷
else if(SetFlag==6) SetFlag=0; //μ÷ê±??ê±óD5???éμ÷£??′ê±?¢·??¢??oí??á?μ?ê±?¢·?
}
while(SET==0); //μè′ySET?üμˉ?e
}
/*********************?ú?y3£??ê?ê±??μ?×′ì???°′???ó?ü ?D???aè??ú??ê? **********************************/
else if(ADD==0&&SetFlag==0) //?úê±????ê?×′ì???°′??á??ó?ü£??D??μ?è??ú??ê?×′ì? ?òòa?ú·?éè?¨×′ì???
{
delay_1ms(10);
if(ADD==0)
{
TempFlag=0; //2???ê????è
DataFlag=!DataFlag; //±ê????è?·′
while(ADD==0); //μè′yADD?üμˉ?e
}
}
/*********************** ?ú??ê?ê±??ê± °′á?SETò?′? μ÷D?ê± **************************************/
else if(ADD==0&&DataFlag==0) //?ú2???ê?è??úê± °′á?SETò?′? μ÷D?ê±
{
delay_1ms(20); //°′?ü?óê±·à???ˉ
if(ADD==0&&SetFlag==1) //?úê±????ê?×′ì???°′?üμ÷ê±?ó
{ hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10; //?íêy??1ü??ê?
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) //?ú??á?éè?¨×′ì???°′?üμ÷??á?μ?D?ê±
{
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); //μè′yADD?üμˉ?e
}
/*********************************?úè??ú??ê?×′ì???°′???ó?üμ÷è??ú *********************************************/
else if(ADD==0&&DataFlag)//?úμ÷è??úμ?±ê?????a1ê±°′??ADDê?μ÷è??ú
{
delay_1ms(20);
if(ADD==0&&SetFlag==1) //μ÷è??ú--?ê ?ú°′á?ADD?ò°′??á?SETò?′??ò?úè??ú??ê?μ?±ê?????a1ê±
{
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) //μ?μ?μ±??μ?ììêy
Day=0;
dispbuf[0]=Day%10;
dispbuf[1]=Day/10;
}
while(ADD==0); //μè′yADD?üμˉ?e
}
/*********************???ü***********************************/
if (DEC==0&&SetFlag==0) //?úê±????ê?×′ì???°′??á????ü£??D??μ????è??ê?×′ì?
{
delay_1ms(10);
if(DEC==0&&SetFlag==0)
{
DataFlag=0; //2???ê?è??ú
TempFlag=!TempFlag; //±ê????è?·′
// init_DS(); //3?ê??ˉ???è′??D?÷
while(DEC==0); //μè′yADD?üμˉ?e
}
}
else if(DEC==0&&DataFlag==0) //?ú2???ê?è??úê± °′?????üμ÷ê±????
{
delay_1ms(20);
if(DEC==0&&SetFlag==1) //?úê±????ê?×′ì???°′?üμ÷ê±??
{ hour--;
if(hour==-1)
{hour=23;
}
dispbuf[6]=hour%10; //?íêy??1ü??ê?
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) //?ú??á?éè?¨×′ì???°′?üμ÷??á?μ?D?ê±
{
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); //μè′yADD?üμˉ?e
/*********************** μ÷è??ú ??****************************************/
else if(DEC==0&&SetFlag==1&&DataFlag) //?úê±????ê?×′ì???°′?üμ÷?ê??
{ Year--;
if(Year==-1)
{Year=99;
}
dispbuf[6]=Year%10; //?íêy??1ü??ê?
dispbuf[7]=Year/10;
}
else if(DEC==0&&SetFlag==2&&DataFlag) //?úê±????ê?×′ì???°′?üμ÷????
{ Month--;
if(Month==0)
{Month=12;
}
dispbuf[3]=Month%10; //?íêy??1ü??ê?
dispbuf[4]=Month/10;
}
else if(DEC==0&&SetFlag==3&&DataFlag) //?úê±????ê?×′ì???°′?üμ÷è???
{ Day--;
if(Day==-1)
{Day=getmonthdays(Year,Month); //μ?μ?μ±??μ?ììêy
}
dispbuf[0]=Day%10; //?íêy??1ü??ê?
dispbuf[1]=Day/10;
}
while(DEC==0); //μè′yADD?üμˉ?e
}
/***************************?D??μ???á?éè??×′ì?**************************
if(P2_3==0&&Alarm==0) //2??ú??á??ìê±°′??á???á?éè?¨?ü
{
delay_1ms(20);
if(P2_3==0) //?úê±????ê?×′ì???°′?üμ÷ê±
{
SetFlag=!SetFlag;
AlarmSet=1;
}
while(P2_3==0);
}
}
*/
//*************?÷3ìDò********************
void main(void)
{
TMOD=0x01;
TH0=0xf8;
TL0=0x30; //éè?¨?¨ê±?÷2msò?′??D??
dispbitcnt=0;
P1=0xFF; //?a????
P3=0x0; //?a???? £?êy??1üè???
delay_1ms(255); //?óê±
delay_1ms(255); //?óê±
delay_1ms(255); //?óê±
delay_1ms(255); //?óê±
P3=0x0; //1?????
EA=1; //?a×ü?D??
ET0=1;
TR0=1; //?a?¨ê±?÷0?D??
while(1)
{
key_scan(); //°′?üé¨?è
if(SetFlag==0)
{
if((AlarmH||AlarmMin)&&hour==AlarmH&&AlarmMin==minite)//è?1???á?ê±??2??a0?ò??á?ê±??ó?μ±μàê±·????àμè
{
Alarm=1; //??á??ì×′ì???1£??ú?D????1????AlarmFlashè?·′ò?′?£??a?ùAlarmFlash??ò????ú1oí0??±??ˉ£?·??ù?÷?á2úéú?àó|μ?????éù
if(AlarmFlash&&second<20) //?ìò???í£ò???,?ì10′?
BEEP=0; //??á??ì
else if(second>20)
{Alarm=0; //??á?×′ì???á?
AlarmFlash=0;
}
else
{
BEEP=1; //??á?2??ì
}
}
//else Alarm=0; //??á?×′ì???á?
if(TempFlag)//?D??μ????è??ê?
{
tp=Readtem();//°??t??êy3?ò?10±?3éèy??êy
}
}
}
}
以下是单片机源代码,
51μ¥???úμ?×ó???ó3ìDò ?ó???èè??ú??ê?
?§???μ?ê£o12.000M
í¨1yèy??°′?üéè?¨ê±·????μ£?í¨1y??á??ü?ééè?¨??á?ê±??£???á????ééè?¨D?ê±oí·??ó
?úê±????ê?μ?×′ì???°′??DEC?ü£??D??μ????è??ê??£
?úê±????ê?μ?×′ì???°′??ADD?ü£??D??μ?è??ú??ê??£
?ú??ê?ê±??×′ì???°′SET?üéè??ê±?ó?°??á?ê±??£?
?úè??ú??ê?×′ì???°′SET?üéè??è??ú£?
***************************************************************************/
#include<reg51.h>
#include<DS18B20.h> //°üo?DS18B20?y?ˉ3ìDò
unsigned char dispbitcnt; //êy??1ü????é¨?è±?á?
char second,AlarmSec; //·?±eê?ê±?óμ????¢??á??¨ê±μ???
char minite,AlarmMin; //·?±eê?ê±?óμ?·??¢??á??¨ê±μ?·?
char hour,AlarmH; //·?±eê?ê±?óμ?D?ê±?¢??á??¨ê±μ?D?ê±
char Year=12,Month=5,Day=1; //?ê ?? è?±?á?
unsigned int tcnt; //??D?o?2úéú±?á?
unsigned int tp; //2aμ?μ????è?μ
unsigned char mstcnt; //é¨?èê±??±?á?
unsigned char SetFlag=0; //SetFlag=1ê±?éμ÷D?ê±£?=2ê±μ÷·??ó£? =3ê±μ÷???ó £? =4ê±μ÷??á?μ?D?ê±£? =5ê±μ÷??á?μ?·??ó
bit FlashFlag=1; //FlashFlag?a1ê±êy??1ü?àó|??μ?áá£??a0ê±2?áá£?FlashFlag?ú1oí0??±??ˉêy??1ü?í?áéá
bit Alarm; //??á??ì×′ì?μ?±ê????£???á?ê±??μ?ê±??1
//bit AlarmSet; //AlarmSet=1±íê?éè?¨á??¨ê±ê±??£??¨ê±μ?ê±??á??ì
bit AlarmFlash; //éè????á?×′ì?ê±éá?ˉμ?±ê????
bit TempFlag=0,DataFlag=0; //?D????ê????èμ?±ê???? oí1|??μ?è??ú??ê?μ?±ê????
unsigned int FlashCount; //ó?óúéè????á?ê±éáμ?ê±??£??ú?¨ê±?÷2ms?D??à?à??ó μ?250ê±?a0.5s
//const unsigned char dispcode[11]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xfd};//0123456789-????£?êy??1ü??ê??àó|êyê±μ?1ü??μ???
const unsigned char dispcode[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF,0XFF};//0123456789- ?°è??e?± ????
const unsigned char dispbitcode[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //12345678???? £?ó?à′?D??3ìDò?D?ˉì?é¨?èêy??1ü??ò???
unsigned char dispbuf[8]={0,0,0,0,0,0,0,0};//?o3?êy×é
sbit SET=P2^0; //μ÷ê±μ?°′?ü
sbit ADD=P2^1; //?ó?ü
sbit DEC=P2^2; //???ü
sbit BEEP=P2^4; //?ó??á?·??ù?÷
unsigned char getmonthdays(unsigned int year,unsigned char month)/*μ?μ??3??μ?ììêy*/
{
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?D??êy??1üé¨?è3ìDò*********************
// 2msò?′??D??,?D??500′??a1??
void Timer0() interrupt 1
{
P3=0xff; //êy??1ü?è2?áá
P1=0; //??è?2??è?eá?£?·??òóD??ó°
//μ±éè?¨D?ê±ê± BIT6 7á???êy??1üéá?ˉ
if(FlashFlag==0&&(SetFlag==1||SetFlag==4||SetFlag==6)) //FlashFlag?a1ê±êy??1ü?àó|??μ?áá£??a0ê±2?áá£?FlashFlag?ú1oí0??±??ˉêy??1ü?í?áéá
P1=dispbitcode[dispbitcnt]&0x3F; //?úFlashFlag==0ê±μú 6 7??êy??1ü2?áá
else if(FlashFlag==0&&(SetFlag==2||SetFlag==5||SetFlag==7))
P1=dispbitcode[dispbitcnt]&0xE7; //?úFlashFlag==0ê±μú 3 4??êy??1ü2?áá
else if(FlashFlag==0&&(SetFlag==3||SetFlag==8))
P1=dispbitcode[dispbitcnt]&0xFC; //?úFlashFlag==0ê±μú 0 1??êy??1ü2?áá
else if(SetFlag==0&&TempFlag)
P1=dispbitcode[dispbitcnt]&0x07; //?ú???è??ê?×′ì??? £?????ê?êy??1üoó4??
else P1=dispbitcode[dispbitcnt];
P3=dispcode[dispbuf[dispbitcnt]];//????
if(dispbitcnt==1&&TempFlag) //μ±??ê????èê±£??úμú?t????ê?D?êyμ?
P3=P3&0x7F;
dispbitcnt++;
if(dispbitcnt==8) dispbitcnt=0;
tcnt++; //?D??2úéú??D?o?£?2ms???ó1
if(tcnt==500) //2??úéè??μ?×′ì??? ???????ó???ó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 á?êy??1ü2???
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++; //éè????á?ê±×′ì???êy??1ü?àó|??éá
}
else FlashFlag=1; //2?éè????á?ê±êy??1ü2?éá
if(FlashCount==251) //FlashCount?? 2ms???ó1′?£?μ?251?a 0.5s
{
FlashCount=0;
FlashFlag^=1; //êy??1üéáμ?±ê????£?FlashFlag?a1ê±êy??1ü????μ?áá£??a0ê±2?áá
}
TMOD=0x01;
TH0=0xf8;
TL0=0x30;
}
///****************1ms?óê±3ìDò******************
void delay_1ms(unsigned char i)
{
unsigned char j;
for(i=0;i<144;i++)
for(j=0;j<144;j++);
}
///****************°′?ü?ì2a*********************
//
void key_scan(void)
{
/*********************°′??éè???ü **********************************/
if(SET==0&&TempFlag==0) //è?1??ú??ê?ê±??μ?×′ì???°′??á?éè???ü £?êy??1ü?úμ÷ê±·?????ò??ˉ
{
delay_1ms(20); //°′?ü?óê±·à???ˉ
if(SET==0)
{
SetFlag++;
if(SetFlag==4&&DataFlag) SetFlag=0; //μ÷è??úê± ??óDèy???éμ÷
else if(SetFlag==6) SetFlag=0; //μ÷ê±??ê±óD5???éμ÷£??′ê±?¢·??¢??oí??á?μ?ê±?¢·?
}
while(SET==0); //μè′ySET?üμˉ?e
}
/*********************?ú?y3£??ê?ê±??μ?×′ì???°′???ó?ü ?D???aè??ú??ê? **********************************/
else if(ADD==0&&SetFlag==0) //?úê±????ê?×′ì???°′??á??ó?ü£??D??μ?è??ú??ê?×′ì? ?òòa?ú·?éè?¨×′ì???
{
delay_1ms(10);
if(ADD==0)
{
TempFlag=0; //2???ê????è
DataFlag=!DataFlag; //±ê????è?·′
while(ADD==0); //μè′yADD?üμˉ?e
}
}
/*********************** ?ú??ê?ê±??ê± °′á?SETò?′? μ÷D?ê± **************************************/
else if(ADD==0&&DataFlag==0) //?ú2???ê?è??úê± °′á?SETò?′? μ÷D?ê±
{
delay_1ms(20); //°′?ü?óê±·à???ˉ
if(ADD==0&&SetFlag==1) //?úê±????ê?×′ì???°′?üμ÷ê±?ó
{ hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10; //?íêy??1ü??ê?
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) //?ú??á?éè?¨×′ì???°′?üμ÷??á?μ?D?ê±
{
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); //μè′yADD?üμˉ?e
}
/*********************************?úè??ú??ê?×′ì???°′???ó?üμ÷è??ú *********************************************/
else if(ADD==0&&DataFlag)//?úμ÷è??úμ?±ê?????a1ê±°′??ADDê?μ÷è??ú
{
delay_1ms(20);
if(ADD==0&&SetFlag==1) //μ÷è??ú--?ê ?ú°′á?ADD?ò°′??á?SETò?′??ò?úè??ú??ê?μ?±ê?????a1ê±
{
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) //μ?μ?μ±??μ?ììêy
Day=0;
dispbuf[0]=Day%10;
dispbuf[1]=Day/10;
}
while(ADD==0); //μè′yADD?üμˉ?e
}
/*********************???ü***********************************/
if (DEC==0&&SetFlag==0) //?úê±????ê?×′ì???°′??á????ü£??D??μ????è??ê?×′ì?
{
delay_1ms(10);
if(DEC==0&&SetFlag==0)
{
DataFlag=0; //2???ê?è??ú
TempFlag=!TempFlag; //±ê????è?·′
// init_DS(); //3?ê??ˉ???è′??D?÷
while(DEC==0); //μè′yADD?üμˉ?e
}
}
else if(DEC==0&&DataFlag==0) //?ú2???ê?è??úê± °′?????üμ÷ê±????
{
delay_1ms(20);
if(DEC==0&&SetFlag==1) //?úê±????ê?×′ì???°′?üμ÷ê±??
{ hour--;
if(hour==-1)
{hour=23;
}
dispbuf[6]=hour%10; //?íêy??1ü??ê?
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) //?ú??á?éè?¨×′ì???°′?üμ÷??á?μ?D?ê±
{
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); //μè′yADD?üμˉ?e
/*********************** μ÷è??ú ??****************************************/
else if(DEC==0&&SetFlag==1&&DataFlag) //?úê±????ê?×′ì???°′?üμ÷?ê??
{ Year--;
if(Year==-1)
{Year=99;
}
dispbuf[6]=Year%10; //?íêy??1ü??ê?
dispbuf[7]=Year/10;
}
else if(DEC==0&&SetFlag==2&&DataFlag) //?úê±????ê?×′ì???°′?üμ÷????
{ Month--;
if(Month==0)
{Month=12;
}
dispbuf[3]=Month%10; //?íêy??1ü??ê?
dispbuf[4]=Month/10;
}
else if(DEC==0&&SetFlag==3&&DataFlag) //?úê±????ê?×′ì???°′?üμ÷è???
{ Day--;
if(Day==-1)
{Day=getmonthdays(Year,Month); //μ?μ?μ±??μ?ììêy
}
dispbuf[0]=Day%10; //?íêy??1ü??ê?
dispbuf[1]=Day/10;
}
while(DEC==0); //μè′yADD?üμˉ?e
}
/***************************?D??μ???á?éè??×′ì?**************************
if(P2_3==0&&Alarm==0) //2??ú??á??ìê±°′??á???á?éè?¨?ü
{
delay_1ms(20);
if(P2_3==0) //?úê±????ê?×′ì???°′?üμ÷ê±
{
SetFlag=!SetFlag;
AlarmSet=1;
}
while(P2_3==0);
}
}
*/
//*************?÷3ìDò********************
void main(void)
{
TMOD=0x01;
TH0=0xf8;
TL0=0x30; //éè?¨?¨ê±?÷2msò?′??D??
dispbitcnt=0;
P1=0xFF; //?a????
P3=0x0; //?a???? £?êy??1üè???
delay_1ms(255); //?óê±
delay_1ms(255); //?óê±
delay_1ms(255); //?óê±
delay_1ms(255); //?óê±
P3=0x0; //1?????
EA=1; //?a×ü?D??
ET0=1;
TR0=1; //?a?¨ê±?÷0?D??
while(1)
{
key_scan(); //°′?üé¨?è
if(SetFlag==0)
{
if((AlarmH||AlarmMin)&&hour==AlarmH&&AlarmMin==minite)//è?1???á?ê±??2??a0?ò??á?ê±??ó?μ±μàê±·????àμè
{
Alarm=1; //??á??ì×′ì???1£??ú?D????1????AlarmFlashè?·′ò?′?£??a?ùAlarmFlash??ò????ú1oí0??±??ˉ£?·??ù?÷?á2úéú?àó|μ?????éù
if(AlarmFlash&&second<20) //?ìò???í£ò???,?ì10′?
BEEP=0; //??á??ì
else if(second>20)
{Alarm=0; //??á?×′ì???á?
AlarmFlash=0;
}
else
{
BEEP=1; //??á?2??ì
}
}
//else Alarm=0; //??á?×′ì???á?
if(TempFlag)//?D??μ????è??ê?
{
tp=Readtem();//°??t??êy3?ò?10±?3éèy??êy
}
}
}
}