基于51单片机的数字电子时钟
程序员文章站
2022-06-08 19:57:37
...
51单片机实现数字电子时钟
基于51单片机的数字电子时钟
该时钟完成的功能:数码管动态显示、蜂鸣器闹钟、按键设置
参考链接:
基于单片机的电子时钟设计(keil+protues仿真,含代码及原理图)
51单片机蜂鸣器播放小星星两只老虎欢乐颂 源程序
功能按键
K1:切换显示内容 时间 日期 闹钟 无
K2:切换设置位
K3:音乐播放 按下后直接播放选择的音乐 只能在开启闹钟的情况下才能播放
K4:音乐切换按键 一共4首音乐 欢乐颂 两只老虎 小星星 生日快乐
K5:减1
K6:加1
K7:闹钟开关 闹钟显示F为关 T为开 播放音乐时长按可以退出播放 此时闹钟自动关闭
仿真图
代码
使用keil编译的代码 想要源码和仿真文件 私信或评论邮箱 看到就发过去注释产生了乱码 C语言
#include<reg51.h>
typedef unsigned int uInt; //¶ÔÊý¾ÝÀàÐͽøÐÐÉùÃ÷¶¨Òå
typedef unsigned char uChar;
// uChar num[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //¶¨ÒåÊýÂë¹ÜÏÔʾÊý×é-¹²Òõ
uChar num[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //¶¨ÒåÊýÂë¹ÜÏÔʾÊý×é-¹²Ñô
//uChar position[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //¶¨ÒåλѡÊý×é-0Ñ¡
uChar position[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //¶¨ÒåλѡÊý×é-1Ñ¡
uChar HH = ~0x40; //¹²ÒõÏÔʾµÄºá¸Ü
//uChar HH = 0xbf; //¹²ÑôÏÔʾµÄºá¸Ü
// uchar FF = 0xff;
uChar display_flag = 0; //¶¨Ò嵱ǰÊýÂë¹ÜÏÔʾ±êÖ¾ 0ʱ¼ä 1ÈÕÆÚ 2ÄÖÖÓ 3ÎÂʪ¶È
uChar change_flag = 0; //¶¨ÒåÇл»Î»È«¾Ö±äÁ¿ 0ÎÞÑ¡Ôñ 1-01λ 2-34λ 3-67λ
uChar alarm_clock_flag = 0; //¶¨ÒåÄÖÖÓ¿ª¹Ø±êÖ¾ 0¹Ø 1¿ª
uChar music_flag = 0; //¶¨ÒåÒôÀÖÑ¡Ôñ±êÖ¾ 0-»¶ÀÖËÌ 1-Á½Ö»ÀÏ»¢ 2-СÐÇÐÇ 3-ÉúÈÕ¿ìÀÖ
uChar music_play_flag = 0; //¶¨ÒåÒôÀÖ²¥·Å±êÖ¾ 0 ²»²¥·Å 1²¥·Å
uChar second=30,minute=30,hour=12,year=20,month=12,day=1,count=0; //³õʼ»¯ÈÕÆÚʱ¼ä
uChar minute_alarm=30,hour_alarm=12; //³õʼ»¯ÄÖÖÓʱ¼ä
sbit Key_display = P3^0; //ÏÔʾÇл»£¬ÉèÖÃλÇл»
sbit Key_change = P3^1; //ÉèÖÃλÇл»
sbit Key_play = P3^2; //²¥·ÅÒôÀÖ°´¼ü
sbit Key_music = P3^3; //¸Ä±äÄÖÖÓÒôÀÖ 0-3
sbit Key_DEC = P3^4; //¼õ1
sbit Key_INC = P3^5; //¼Ó1
sbit Key_TF = P3^6; //¹ØÄÖÖÓ | ÉèÖÃÄÖÖÓ״̬Ï Çл»¿ª¹Ø ÉèÖÃʱ¼ä״̬Ï£¬ÊÇ·ñÉúЧ
sbit Buzzer = P3^7; //·äÃùÆ÷
#define Select P2 //½«P2¿Ú¶¨ÒåΪSelect λѡ
#define Led P0 //½«P0¿Ú¶¨ÒåΪLed ¶ÎÑ¡
int code tho[]={0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfe, //1234567µÄ¸ßλ¶¨Ê±Æ÷µÄÖµ
0xf8,0xf9,0xfa,0xfa,0xfb,0xfb,0xfc, //µÍÒô1234567µÄ¸ßλ¶¨Ê±Æ÷µÄÖµ
0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe}; //¸ßÒô1234567µÄ¸ßλ¶¨Ê±Æ÷µÄÖµ
int code tlo[]={0x44,0xab,0x08,0x33,0x81,0xc7,0x05, //1234567µÄ¸ßλ¶¨Ê±Æ÷µÄÖµ
0x8c, 0x5b,0x15,0x67,0x04,0x90,0x0c, //µÍÒô1234567µÄ¸ßλ¶¨Ê±Æ÷µÄÖµ
0x22,0x56,0x85,0x9a,0xc1,0xd3,0xe4}; //¸ßÒô1234567µÄ¸ßλ¶¨Ê±Æ÷µÄÖµ
int code tontime[]={200,400,600,800,1000,1200,1400,1600};
//tontimeÊý¾ÝµÄÖµ´ú±íËÄ·ÖÖ®Ò»ÅÄ,°ëÅÄ,1ÅÄ,1ÅÄ°ë,2ÅÄ,2ÅÄ°ë,3ÅÄ,3ÅÄ°ë,4ÅÄ
/********************************************************************
»¶ÀÖË̵Ľ£Æ×Êý¾Ý±í£¬Í¨¹ý¸Ä±äÕâÁ½¸öÊý×éÒÔ¼°kµÄÖµ¾Í¿ÉÒÔ²¥·Å²»Í¬µÄÇúÄ¿
ÆäÖÐ5+6ÖеÄ5±íʾµÍÒô5£¬Èç¹ûÐèÒªÆäËüµÄµÍÒô½«5¸ÄΪÆäËüµÄµÍÒôÒô·û¼´¿É
*********************************************************************/
int code gladsong[]={
3,3,4,5,5,4,3,2,1,1,2,3,3,2,2,3,3,4,5,5,4,3,2,1,1,2,3,2,1,1,2,
2,3,1,2,3,4,3,1,2,3,4,3,2,1,2,5+6,3,3,4,5,5,4,3,2,1,1,2,3,2,1,1
};//Õâ¸ö´ú±í¸èÇúµÄ¼òÆ×
float code timenum[]={
1,1,1,1,1,1,1,1,1,1,1,1,1.5,0.5,2,1,1,1,1,1,1,1,1,1,1,1,1,1.5,0.5,2,1,
1,1,1,1,0.5,0.5,1,1,1,0.5,0.5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};//Õâ¸ö´ú±íÿһ¸öÒô·ûµÄ½ÚÅÄ£¬ÒÔ4/4ΪÀý,1´ú±í1ÅÄ,1.5´ú±í1ÅÄ°ë,0.5´ú±í°ëÅÄ,2´ú±í2ÅÄ
//twotigerºÍtigernumÕâÁ½¸öÊý×éÊÇÁ½Ö»ÀÏ»¢µÄ¼òÆ×Êý¾Ý±í
int code twotiger[] = {1,2,3,1,1,2,3,1,3,4,5,3,4,5,5,6,5,4,3,1,5,6,5,4,3,1,2,5+6,1,2,5+6,1};
int code tigernum[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.5,0.5,0.5,0.5,1,1,0.5,0.5,0.5,0.5,1,1,
1,1,1,1,1,1};
//smallstar?starnumÕâÁ½¸öÊý×éÊÇСÐÇÐǵļòÆ×Êý¾Ý±í
int code smallstar[]= {1,1,5,5,6,6,5,4,4,3,3,2,2,1,5,5,4,4,3,3,2,
5,5,4,4,3,3,2,1,1,5,5,6,6,5,4,4,3,3,2,2,1};
int code starnum[]= {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
//happybirthday[]ºÍbirthdaynum[]ÊÇÉúÈÕ¿ìÀÖ¸èµÄ¼òÆ×Êý¾Ý±í
int code happybirthday[]={5+6,5+6,6+6,5+6,1,7+6,5+6,5+6,6+6,5+6,
2,1,5+6,5+6,5,3,1,7+6,6+6,4,4,3,1,2,1};
int code birthdaynum[]={0.5,0.5,1,1,1,1,0.5,0.5,1,1,1,1,1,1,1,1,1,1,1,1.5,0.5,1,1,1,1};
static int HT,LT;//¸ßµÍλ¶¨Ê±Æ÷µÄÖµ
static uChar k=0;//·äÃùÆ÷ÒôÀÖʹÓÃ
/*********************ÑÓʱ×Óº¯Êý********************/
void Delay(uInt t)
{
while(t)
{
t--;
}
}
/********************¶¨Ê±Æ÷0³õʼ»¯*******************/
void Timer0_Init()
{
TMOD|=0X01; //Ñ¡ÔñΪ¶¨Ê±Æ÷0ģʽ£¬¹¤×÷·½Ê½1£¬½öÓÃTR0´ò¿ªÆô¶¯¡£
TH0=0X4C; //¸ø¶¨Ê±Æ÷¸³³õÖµ£¬¶¨Ê±1ms
TL0=0X01;
ET0=1; //´ò¿ª¶¨Ê±Æ÷0ÖжÏÔÊÐí
EA=1; //´ò¿ª×ÜÖжÏ
PT0=1; //ÉèÖö¨Ê±Æ÷ÓÅÏȼ¶×î¸ß
TR0=1; //¿ªÆô¶¨Ê±Æ÷
}
/********************¶¨Ê±Æ÷1³õʼ»¯*******************/
void Timer1_Init()
{
TMOD|=0x10;
TH1=HT;
TL1=LT;
EA=1;
ET1=1;
TR1=0;//¹Ø±Õ¶¨Ê±Æ÷1
}
/*****************************************************
ÒÔ£ºÄꡪ¡ªÔ¡ª¡ªÈÕ µÄ¸ñʽÏÔʾʱ¼ä
ÒÔ£ºÊ±¡ª¡ª·Ö¡ª¡ªÃë µÄ¸ñʽÏÔʾʱ¼ä
ÊäÈë²ÎÊý˳ÐòΪ£ºÃ롪¡ª·Ö¡ª¡ªÊ±
Ì졪¡ªÔ¡ª¡ªÄê
Èç¹ûÊÇÔÚÉèÖÃ״̬Ï£ººá¸ÜÏûʧ
*****************************************************/
void Display_Time(uChar temp_1,uChar temp_2,uChar temp_3)
{
uChar temp1 = temp_1;
uChar temp2 = temp_2;
uChar temp3 = temp_3;
Select = position[0];
Led = num[temp1%10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄÈÕ¡¢Ãë¸öλ
Select = position[1];
Led = num[temp1/10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄÈÕ¡¢Ãëʮλ
Select = position[2];
if (change_flag)
{Select = 0;} //ÉèÖÃ״̬Ϲرպá¸Ü
Led = HH;
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄ¡ª¡ª
Select = position[3];
Led = num[temp2%10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄÔ¡¢·Ö¸öλ
Select = position[4];
Led = num[temp2/10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄÔ¡¢·Öʮλ
Select = position[5];
if (change_flag)
{Select = 0;} //ÉèÖÃ״̬Ϲرպá¸Ü
Led = HH;
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄ¡ª¡ª
Select = position[6];
Led = num[temp3%10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄÄꡢʱ¸öλ
Select = position[7];
Led = num[temp3/10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄÄꡢʱʮλ
}
/*******************ÄÖÖÓʱ¼äÏÔʾ*********************
ÏÔʾ¸ñʽ£º¿ª¹Ø±êÖ¾ ¡ª¡ª Сʱ ¡ª¡ª ·ÖÖÓ ¿ª¹Ø±êÖ¾Tλ¿ªFΪ¹Ø
ÉèÖÃ״̬Ï ºá¸ÜÏûʧ
****************************************************/
void Display_Alarm_Clock()
{
Select = position[0];
Led = num[minute_alarm%10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄ·Ö¸öλ
Select = position[1];
Led = num[minute_alarm/10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄ·Öʮλ
Select = position[2];
if(change_flag)
{Select = 0;}
Led = HH;
Delay(100);
Led = 0xff;
//ÏÔʾ¼ä¸ô¡ª¡ª
Select = position[3];
Led = num[hour_alarm%10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄʱ¸öλ
Select = position[4];
Led = num[hour_alarm/10];
Delay(100);
Led = 0xff;
//ÏÔʾʱ¼äµÄʱʮλ
Select = position[5];
if(change_flag)
{Select = 0;}
Led = HH;
Delay(100);
Led = 0xff;
//ÏÔʾ¼ä¸ô¡ª¡ª
if (alarm_clock_flag)
{
Select = position[6];
Led = ~0x31;
Delay(100);
Led = 0xff;
Select = position[7];
Led = ~0x07;
Delay(100);
Led = 0xff;
//ÏÔʾÄÖÖÓ¿ª £ºT
}
else
{
Select = position[6];
Led = ~0x41;
Delay(100);
Led = 0xff;
Select = position[7];
Led = ~0x71;
Delay(100);
Led = 0xff;
//ÏÔʾÄÖÖӹأºF
}
}
/*******************ʱ¼äÂß¼************************
second=30,minute=30,hour=12,year=20,month=12,day=1
ÅжÏʱ¼äµÄ¸Ä±ä£¬ÔÚʱ¼ä¼Æʱ¡¢µ÷Õûʱ¼ä¡¢ÄÖÖÓµ÷ÕûºóÓÃ
****************************************************/
void Time_logic()
{
if (second==60)
{
second = 0;
minute++;
}//ÿ60Ãë·ÖÖÓ¼Ó1
if (minute==60)
{
minute = 0;
hour++;
}//ÿ60·ÖÖÓСʱ¼Ó1
if (hour==24)
{
hour = 0;
day++;
}//ÿ24СʱÌì¼Ó1
if (day==32)
{
switch(month)
{
case 1:day = 0;month++;break;
case 3:day = 0;month++;break;
case 5:day = 0;month++;break;
case 7:day = 0;month++;break;
case 8:day = 0;month++;break;
case 10:day = 0;month++;break;
case 12:day = 0;month++;break;
default:break;
}
}//1¡¢3¡¢5¡¢7¡¢8¡¢10¡¢12ÔÂ31Ìì
if (month==2&&day==29&&year%4)
{
day = 0;
month++;
}
if (month==2&&day==30&&year%4==0)
{
day = 0;
month++;
}//ÈòÄê2ÔÂ29Ì죬ƽÄê2ÔÂ28Ìì
if (day==31)
{
switch(month)
{
case 2:day = 0;month++;break;
case 4:day = 0;month++;break;
case 9:day = 0;month++;break;
case 11:day = 0;month++;break;
default:break;
}
}//2¡¢4¡¢9¡¢11ÔÂ30Ìì
if (month==13)
{
month = 1;
year++;
}//ÿ12ÔÂÄê¼Ó1
if(second<0)
second=0;
if(minute<0)
minute=0;
if(hour<0)
hour=0;
if(day<0)
day=0;
if(month<0)
month=0;
if(year<0)
year=0;
}
/*****************°´¼üɨÃ躯Êý*********************/
/***************************************************
diaplay_flag ¶ÔÓ¦°´¼ü 0-3
***************************************************/
void Key_display_judge()
{
if(Key_display==0) //¼ì²â°´¼üKey_changeÊÇ·ñ°´ÏÂ
{
Delay(1000); //Ïû³ý¶¶¶¯ Ò»°ã´óÔ¼10ms
if(Key_display==0) //ÔÙ´ÎÅжϰ´¼üÊÇ·ñ°´ÏÂ
{
display_flag++;
if (display_flag==4)
{display_flag=0;}
}
while(!Key_display); //¼ì²â°´¼üÊÇ·ñËÉ¿ª
}
}
/***************************************************
change_flag ¶ÔÓ¦°´¼ü 0-3
***************************************************/
void Key_change_judge()
{
if(Key_change==0) //¼ì²â°´¼üKey_changeÊÇ·ñ°´ÏÂ
{
Delay(1000); //Ïû³ý¶¶¶¯ Ò»°ã´óÔ¼10ms
if(Key_change==0) //ÔÙ´ÎÅжϰ´¼üÊÇ·ñ°´ÏÂ
{
change_flag++;
if (change_flag==4)
{change_flag=0;}
}
while(!Key_change); //¼ì²â°´¼üÊÇ·ñËÉ¿ª
}
}
/***************************************************
music_flag ¶ÔÓ¦°´¼ü 0-3
***************************************************/
void Key_music_judge()
{
if(Key_music==0) //¼ì²â°´¼üKey_changeÊÇ·ñ°´ÏÂ
{
Delay(1000); //Ïû³ý¶¶¶¯ Ò»°ã´óÔ¼10ms
if(Key_music==0) //ÔÙ´ÎÅжϰ´¼üÊÇ·ñ°´ÏÂ
{
music_flag++;
if (music_flag==4)
{music_flag=0;}
}
while(!Key_music); //¼ì²â°´¼üÊÇ·ñËÉ¿ª
}
}
/***************************************************
°´¼üÿ°´Ò»´Î ½«ÊýÖµ¼Ó1²¢·µ»Ø
***************************************************/
uChar Key_INC_judge(uChar flag)
{
uChar temp_flag = flag;
if (Key_INC==0)
{
Delay(1000);
if (Key_INC==0)
{
temp_flag++;
}
while(!Key_INC);
}
return temp_flag;
}
/***************************************************
°´¼üÿ°´Ò»´Î ½«ÊýÖµ¼õ1²¢·µ»Ø
***************************************************/
uChar Key_DEC_judge(uChar flag)
{
uChar temp_flag = flag;
if (Key_DEC==0)
{
Delay(1000);
if (Key_DEC==0)
{
temp_flag--;
}
while(!Key_DEC);
}
return temp_flag;
}
/***************************************************
Key_TF °´¼ü½«±ê־ȡ·´ ¿ØÖÆÄÖÖÓ¿ª¹Ø
***************************************************/
void Key_TF_judge()
{
if(Key_TF==0) //¼ì²â°´¼üKey_returnÊÇ·ñ°´ÏÂ
{
Delay(1000); //Ïû³ý¶¶¶¯ Ò»°ã´óÔ¼10ms
if(Key_TF==0) //ÔÙ´ÎÅжϰ´¼üÊÇ·ñ°´ÏÂ
{
alarm_clock_flag=~alarm_clock_flag; //Inter0_flag״̬ȡ·´
}
while(!Key_TF) //¼ì²â°´¼üÊÇ·ñËÉ¿ª
{
Select = position[2];
Led = HH;
Delay(100);
Led = 0xff;
Select = position[5];
Led = HH;
Delay(100);
Led = 0xff;
} //ÄÖÖÓÇл»±êÖ¾ÏÔʾ °ïÖú¹Ø±ÕÄÖÖÓ
}
}
/****************************************************
Key_play ²¥·ÅÒôÀÖ°´¼ü ÄÖÖÓ¿ªÆô״̬²ÅÄÜʹÓÃ
****************************************************/
void Key_play_judge()
{
if(Key_play==0)
{
Delay(1000);
if(Key_play==0)
{
music_play_flag = ~music_play_flag; //¿ªÆôÄÖÖÓ±êÖ¾²ÅÄܲ¥·ÅÒôÀÖ
}
while(!Key_play);
}
}
/****************************************************
°´¼üɨÃ躯Êý ʶ±ð°´¼ü
****************************************************/
void Key_scan()
{
uChar temp;
Key_change_judge();
Key_display_judge();
Key_music_judge();
Key_TF_judge();
Key_play_judge();
if (change_flag)
{
switch(display_flag*3+change_flag)
{
case 1:temp = second; temp = Key_INC_judge(temp);temp = Key_DEC_judge(temp);second = temp;break;//Ãëλ
case 2:temp = minute; temp = Key_INC_judge(temp);temp = Key_DEC_judge(temp);minute = temp;break;//·Öλ
case 3:temp = hour; temp = Key_INC_judge(temp);temp = Key_DEC_judge(temp);hour = temp;break;//ʱλ
case 4:temp = day; temp = Key_INC_judge(temp);temp = Key_DEC_judge(temp);day = temp;break;//Ììλ
case 5:temp = month; temp = Key_INC_judge(temp);temp = Key_DEC_judge(temp);month = temp;break;//ÔÂλ
case 6:temp = year; temp = Key_INC_judge(temp);temp = Key_DEC_judge(temp);year = temp;break;//Äêλ
case 7:temp = minute_alarm; temp = Key_INC_judge(temp);temp = Key_DEC_judge(temp);minute_alarm = temp;break;
case 8:temp = hour_alarm; temp = Key_INC_judge(temp);temp = Key_DEC_judge(temp);hour_alarm = temp;break;
default: break;
}
Time_logic();
if(minute_alarm == 60 | minute_alarm<0)
{minute_alarm=0;}
if(hour_alarm == 24 | hour_alarm<0)
{hour_alarm = 0;}//ÄÖÖÓÂß¼
}
}
/***************************************************
·äÃùÆ÷ÒôÀÖº¯Êý
***************************************************/
void Delay_music(uInt p)
{
uInt b;
for(p;p>0;p--)
for(b=120;b>0;b--); //1/6½ÚÅÄ
}
/***************Õûµã±¨Ê±**************************/
void Buzzer_Didi()
{
while(second<6) //Õûµã±¨Ê±6Ãë
{
Buzzer=~Buzzer;
Delay(100);
uzzer=~Buzzer;
Display_Alarm_Clock();
}
TR1=0;
Buzzer=1;
}
void Buzzer_music()
{
Buzzer=1;
while(alarm_clock_flag)
{
Key_TF_judge();
if(~alarm_clock_flag)
{Buzzer=1;TR1=0;break;}
switch(music_flag)
{
case 0:{HT=tho[gladsong[k-1]];
LT=tlo[gladsong[k-1]];
k++; if(k>61)k=0;
Delay_music(tontime[(int)timenum[k]*2]);
Key_TF_judge();
if(~alarm_clock_flag)
{Buzzer=1;TR1=0;break;}
break;}
case 1:{HT=tho[twotiger[k]];
LT=tlo[twotiger[k]];
k++; if(k>31)k=0;
Delay_music(tontime[(int)tigernum[k]*2]);
Key_TF_judge();
if(~alarm_clock_flag)
{Buzzer=1;TR1=0;break;}
break;}
case 2:{HT=tho[smallstar[k]];
LT=tlo[smallstar[k]];
k++; if(k>41)k=0;
Delay_music(tontime[(int)starnum[k]*2]);
Key_TF_judge();
if(~alarm_clock_flag)
{Buzzer=1;TR1=0;break;}
break;}
case 3:{HT=tho[happybirthday[k]];
LT=tlo[happybirthday[k]];
k++; if(k>61)k=0;
Delay_music(tontime[(int)birthdaynum[k]*2]);
Key_TF_judge();
if(~alarm_clock_flag)
{Buzzer=1;TR1=0;break;}
break;}
default:break;
}
if(k%4==0)
Delay_music(200);
TR1=0;
Delay_music(100);
TR1=1;
Key_TF_judge();
if(~alarm_clock_flag)
{Buzzer=1;TR1=0;break;}
}
}
/***************************************************
Key_TF_judge();
if(~alarm_clock_flag)
{Buzzer=1;TR1=0;break;}
¹Ø±ÕÒôÀÖ Ìø³öÑ»· »Ö¸´¼ÆʱÆ÷ºÍ·äÃùÆ÷
/***************************************************
Ö÷º¯Êý
***************************************************/
void main()
{
Timer0_Init();
Timer1_Init();
display_flag = 0;
change_flag = 0;
music_flag = 0;
music_play_flag = 0;
alarm_clock_flag = 0;
while(1)
{
Key_scan();
Key_play_judge();
switch (display_flag)
{
case 0:Display_Time(second,minute,hour);break;
case 1:Display_Time(day,month,year); break;
case 2:Display_Alarm_Clock(); break;
default: break;
}
if(alarm_clock_flag&&hour_alarm==hour&&minute_alarm==minute)
Buzzer_music();//ÄÖÖÓʱ¼äµ½ ¿ªÆôÒôÀÖ
if(music_play_flag&&alarm_clock_flag)
Buzzer_music();//¿ªÆôÒôÀÖ²¥·Å°´¼ü
if(minute==0&&second==0)
Buzzer_Didi(); //Õûµã±¨Ê±
}
}
/*******************¶¨Ê±Æ÷0ÖжÏ×Ó³ÌÐò***************
ʱ¼äµÄ¼Æʱ£¬Ã¿50ms½øÈëÖжϣ¬Ã¿1s¸Ä±äʼþÖµ
***************************************************/
void Timer0() interrupt 1
{
TH0=0X4C; //¸ø¶¨Ê±Æ÷¸³³õÖµ£¬¶¨Ê±50ms
TL0=0X10;
count++;
if(count==20)
{
count=0;
second++;
Time_logic();//ÅжÏʱ¼äÂß¼
}
}
/********************¶¨Ê±Æ÷1ÖжÏ×Ó³ÌÐò*************/
void Timer1() interrupt 3
{
Buzzer=~Buzzer;
TH1=HT;
TL1=LT;
}
这是我们学校一个科目的课程设计,参考了很多大佬的代码,欢迎交流
下一篇: 单片机:独立按键的应用实验