C51定时闹钟程序
- 格式:doc
- 大小:34.00 KB
- 文档页数:8
课程名称:单片机原理与接口技术实践设计课题:基于MCS 51单片机实现电子闹钟功能的设计学院:电子与信息工程学院专业:通信工程小组成员:电子闹钟在科学技术高度发展的今天,千家万户都少不了它,所以很多家庭个人都需要有一个电子闹钟,为人们提供报时方便,但普通电子闹钟不够方便实用。
本文给出了一种基于MCS51单片机实现电子闹钟功能的设计方法,从而给人们带来更为方便的工作与生活。
一.电子闹钟简介我们设计的电子闹钟是以MCS51单片机中的计时器作为时钟,用8位数码管显示当前时间,并且可以设置闹钟时间,并在设置的时间点发出闹铃。
简易闹钟具有以下功能:1.时钟能准确地走时,并可以通过数码管进行显示2.复位后可以进行当前时间的设置3.可以随意设置闹钟时间,闹钟会在设置时间响铃整个系统的任务要求:1)输入数字按键的功能。
保证数字的输入。
2)复位电路的功能。
所有时间回到初始化状态,用于启动设定时间参数(调时或设定闹钟时间);3)显示电路的功能。
当输入数字时显示24小时时间功能。
4)闹铃功能设置闹铃的时间后.能按设置好的时间准时闹铃。
二.系统方案的设计要求根据以上各模块并结合显示屏的功能及元器件材料的情况,决定采用AT89C51为内核显示设计方案。
先进行系统的整体规划确定整个系统的功能,然后按照每个功能的具体要求,进行各个模块的实物设计并逐个调试,待全部通过后,进行整个系统的联调,最终实现一个完整的系统。
整个系统的设计步骤如下:在单片机最小系统的基础上,完成按键电路和复位电路的设计。
完成显示电路、数字按键、单片机时钟电路。
Ⅰ硬件设计系统硬件的设计可以根据系统的各个功能,把整个系统划分成若干个模块,分别对这些模块来进行设计,然后在通过单片机程序来实现对各个硬件模块功能的调度。
本系统涉及到的硬件模块有:按键电路、数码管显示电路、单片机时钟电路、蜂鸣器电路。
各部分实现功能如下:按键电路:提供按键信号。
单片机时钟电路、复位电路:提供内部时钟。
学校电子钟,有闹钟功能,按键可调时间,可调打铃时间,打铃时间长短显示,每个模块有功能注释。
其中正常时间显示和闹钟时间显示可用一个开关来调整。
芯片选择STC89C52程序:#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//定义显示段码uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};uchar codebbtime[]={0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; uchar clock[]={0,0,0,0};uchar clock1[]={12,30,0};uchar weikong[6];uchar bbduration=4;uchar lingtime=9;//学校打铃时间组uchar shangwu1[]={8,30};uchar shangwu2[]={10,0};uchar shangwu3[]={10,20};uchar shangwu4[]={11,50};uchar xiawu1[]={13,30};uchar xiawu2[]={15,00};uchar xiawu3[]={15,15};uchar xiawu4[]={16,45};//按键定义sbit mode=P1^7;sbit sec_clr=P1^0;sbit min_set_add=P1^3;sbit min_set_sub=P1^4;sbit hour_set_add=P1^1;sbit hour_set_sub=P1^2;sbit bb_set_add=P1^5;sbit bb_set_sub=P1^6;sbit speaker=P2^6;//延时函数void delay(unsigned int t){while(t--);//时钟进位函数void clockjinwei(){clock[0]++;if(clock[0]==20){clock[1]++;clock[0]=0;if(clock[1]==60){clock[2]++;clock[1]=0;if(clock[2]==60){clock[3]++;clock[2]=0;if(clock[3]==24)clock[3]=0;}}}}//定时器0中断服务函数void timer0(void) interrupt 1 using 1 {TMOD=0x01;TH0=0x3c;TL0=0xb0;clockjinwei();}//时钟分位显示函数void fenwei(){weikong[0]=clock[3]/10;weikong[1]=clock[3]%10;weikong[2]=clock[2]/10;weikong[3]=clock[2]%10;weikong[4]=clock[1]/10;weikong[5]=clock[1]%10;}//闹钟分位显示函数void naofen(){weikong[0]=clock1[0]/10;weikong[1]=clock1[0]%10;weikong[2]=clock1[1]/10;weikong[3]=clock1[1]%10;weikong[4]=clock1[2]/10;weikong[5]=clock1[2]%10; }//闹钟定时显示函数void naozhongdisplay(){uchar z,s;uchar x=0x01;naofen();for(z=0;z<6;z++){P2=0;P0=table[weikong[z]];P2=x;x=_crol_(x,1);for(s=0;s<255;s++);}}//时钟显示函数void display(){uchar i,j;uchar x=0x01;fenwei();for(i=0;i<6;i++){P2=0;P0=table[weikong[i]];P2=x;x=_crol_(x,1);for(j=0;j<255;j++);}}//总显示函数void zhongxian(){if(mode==1)delay(100);if(mode==1)display();if(mode==0)delay(100);if(mode==0)naozhongdisplay();}//按键处理程序void key_set(){zhongxian();P1=0xff;if(min_set_add==0){delay(100);if(min_set_add==0){if(mode==1){clock[2]++;if(clock[2]==60){clock[2]=0;}while(min_set_add==0)zhongxian();}}if(mode==0){clock1[1]++;if(clock1[1]==60){clock1[1]=0;}while(min_set_add==0)zhongxian();}}//if(min_set_sub==0){delay(100);if(min_set_sub==0){if(mode==1){clock[2]--;if(clock[2]==0)clock[2]=59;}while(min_set_sub==0)zhongxian();if(mode==0){clock1[1]--;if(clock1[1]==0)clock1[1]=59;}while(min_set_sub==0)zhongxian();}}//if(hour_set_add==0){delay(100);if(hour_set_add==0){if(mode==1){clock[3]++;if(clock[3]==24){clock[3]=0;}while(hour_set_add==0)zhongxian();}if(mode==0){clock1[0]++;if(clock1[0]==24){clock1[0]=0;}while(hour_set_add==0)zhongxian();}}}//if(hour_set_sub==0){delay(100);if(hour_set_sub==0){if(mode==1){clock[3]--;if(clock[3]==0)clock[3]=23;}while(hour_set_sub==0)zhongxian();if(mode==0){clock1[0]--;if(clock1[0]==0)clock1[0]=23;}while(hour_set_sub==0)zhongxian();}}//if(sec_clr==0){delay(100);if(sec_clr==0){clock[1]=0;}while(sec_clr==0)zhongxian();}}//闹钟响铃函数void bb(){if(clock[1]<=bbduration){speaker=1;delay(100);speaker=0;}else speaker=0;}//打铃函数void daling(){if(clock[1]<=lingtime){speaker=1;delay(100);speaker=0;}else speaker=0;}//时间比较函数void bijiao(){if(clock[3]==shangwu1[0]){if(clock[2]==shangwu1[1])daling();}if(clock[3]==shangwu2[0]){if(clock[2]==shangwu2[1])daling();}if(clock[3]==shangwu3[0]){if(clock[2]==shangwu3[1])daling();}if(clock[3]==shangwu4[0]){if(clock[2]==shangwu4[1])daling();}if(clock[3]==xiawu1[0]){if(clock[2]==xiawu1[1])daling();}if(clock[3]==xiawu2[0]){if(clock[2]==xiawu2[1])daling();}if(clock[3]==xiawu3[0]){if(clock[2]==xiawu3[1])daling();}if(clock[3]==xiawu4[0]){if(clock[2]==xiawu4[1])daling();}}//闹钟比较void naobijiao(){if(clock[3]==clock1[0]){if(clock[2]==clock1[1]||clock[2]==clock1[1]+1||clock[2]==clock1[1]+2) bb();}}//响铃时长显示函数void bbtimeshow(){P3=bbtime[bbduration];if(bbduration>15)bbduration=0;}//响铃按键处理函数void bbtime_set(){bbtimeshow();if(bb_set_add==0){delay(100);if(bb_set_add==0)bbduration++;while(bb_set_add==0)bbtimeshow();}if(bb_set_sub==0){delay(100);if(bb_set_sub==0)bbduration--;while(bb_set_sub==0)bbtimeshow();}}//主程序void main(){EA=1;ET0=1;TR0=1;while(1){key_set();bijiao();bbtime_set();naobijiao();}}电路图:分四部分显示:如果在学习这个程序过程中有什么问题,可以发邮件到******************询问。
功能最全的电子钟【单片机】c51数字时钟(带年月日显示)摘要:本设计以单片机为核心,lcd1602显示。
采用独立键盘输入能任意修改当前时间日期和设定闹钟时间。
具有显示年月日(区分闰年和二月),闹钟报警和整点报时功能主程序:/********************************************************************************************************************************************************************************* ****************************************** lcd1602电子钟********************************************************************************************************************************** *************************************************************************************************** ********************/# include <reg52.h># include "lcd16024.h"sbit key1 = P2^0; //调整sbit key2 = P2^1; //加1sbit key3 = P2^2; //减1sbit speaker = P2^3; //蜂鸣器sbit key4 = P2^4; //闹钟设计bit cal_year = 1; //进入判断闰年标志位bit leap_year; //闰年标志位bit calculate = 0; //日加一标记bit run = 0; //闹钟标志bit beep = 0; //整点报时标志//uint8 num = 0; //调整是给的脉冲uint8 code str1[] = "D: ";uint8 code str2[] = "T: ";uint8 code str3[] = "Wek";uint8 daystr[]="2013-07-29 "; //年月日格式uint8 timestr[]="21:30:59 N"; //时分秒格式uint8 daystr1[]="2013-07-29 "; //闹钟年月日格式uint8 timestr1[]="21:30:59 N"; //闹钟时分秒格式uint8 numweek = 0; //星期加1标记char week = 1; //星期char sec = 53; //秒char min = 50; //分char hour = 23; //时uint8 day = 30; // 日uint8 month = 9; //月uint16 year = 2013; //年char week1 = 1; //闹钟星期char sec1 = 58; //闹钟秒char min1 = 50; //闹钟分char hour1 = 23; //闹钟时uint8 day1 = 30; //闹钟日uint8 month1 = 9; //闹钟月uint16 year1 = 2013; //闹钟年uint8 WeekData1; //闹钟星期标记uint8 number = 0; //定时uint8 WeekData; //星期标记uint8 speaker_num; //整点报时次数uint8 scan_key(void); //函数声名/****************************************************************************** ***************************** 更新LCD时间分离读取******************************************************************************************************************* *******/void TimeChange(){//时分秒timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';timestr[4] = min%10+'0';timestr[3] = min/10+'0';timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';//年月日daystr[9] = day%10+'0';daystr[8] = day/10+'0';daystr[6] = month%10+'0';daystr[5] = month/10+'0';daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';//星期WeekData = week+'0';}/****************************************************************************** ***************************** 闹钟更新LCD时间分离读取******************************************************************************************************************* *******/void TimeChange1(){//时分秒timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';//年月日daystr1[9] = day1%10+'0';daystr1[8] = day1/10+'0';daystr1[6] = month1%10+'0';daystr1[5] = month1/10+'0';daystr1[3] = year1%10+'0';daystr1[2] = year1/10%10+'0';daystr1[1] = year1/100%10+'0';daystr1[0] = year1/1000+'0';//星期WeekData1 = week1+'0';}/****************************************************************************** ***************************** 初始化系统定时器0 ******************************************************************************************************************* *******/void systimer0_init(void){TMOD |=0x01;//设置为1时用或(|)TMOD &=0xfd;//设置为0时用与(&)TH0 = 0xDC; // 定时10msTL0 = 0x00;EA = 1;ET0=1;TR0=1;EX0 = 1;}/****************************************************************************** ***************************** 闹钟时间设置*************************************************************************************************************** *******/void naozhong(){uint8 number = 1;uint8 a = 0;uint8 b = 0;if(0 == key4){Delay1Ms(5);if(0 == key4){a = 1;LCD_write_command(0xc0+9);LCD_write_command(0x0f);}while(!key4);}while(a){if(0 == key4){Delay1Ms(5);if(0 == key4){a = 0;run = ~run;while(!key4);}}if(run){timestr[9] = 'Y';}else{timestr[9] = 'N';}if(0 == key1){Delay1Ms(5);if(0 == key1){b = 1;}while(!key1);}while(b){if(0 == key1){Delay1Ms(5);if(0 == key1){number++;if(4 == number)b = 0;}while(!key1);if(number == 1){LCD_write_command(0xc0+9);LCD_write_command(0x0f);}if(number == 2){LCD_write_command(0xc0+6);LCD_write_command(0x0f);}if(number == 3){LCD_write_command(0xc0+3);LCD_write_command(0x0f);}}}switch(number){case 1:if(0 == key2) //闹钟秒加1的设置{Delay1Ms(5);if(0 == key2){sec1++;if(60 == sec1){sec1 = 0;}while(!key2);timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';LCD_write_char(8,1,timestr1[6]);LCD_write_char(9,1,timestr1[7]);LCD_write_command(0xc0+9);}}if(0 == key3) //闹钟秒减1的设置Delay1Ms(5);if(0 == key3){sec1--;if(sec1 < 0){sec1 = 59;}while(!key3);timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';LCD_write_char(8,1,timestr1[6]);LCD_write_char(9,1,timestr1[7]);LCD_write_command(0xc0+9);}}break;case 2:if(0 == key2) //闹钟分加1的设置{Delay1Ms(5);if(0 == key2){min1++;if(60 == min1){min1 = 0;}while(!key2);timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';;LCD_write_char(5,1,timestr1[3]);LCD_write_char(6,1,timestr1[4]);LCD_write_command(0xc0+6);}}if(0 == key3) //闹钟分减1的设置{Delay1Ms(5);if(0 == key3){min1--;if(min1 < 0){min1 = 59;}while(!key3);timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';;LCD_write_char(5,1,timestr1[3]);LCD_write_char(6,1,timestr1[4]);LCD_write_command(0xc0+6);}}break;case 3:if(0 == key2) //闹钟时加1的设置{Delay1Ms(5);if(0 == key2){hour1++;if(24 == hour1){hour1 = 0;}while(!key2);timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';LCD_write_char(2,1,timestr1[0]);LCD_write_char(3,1,timestr1[1]);LCD_write_command(0xc0+3);}}if(0 == key3) //闹钟时减1的设置{Delay1Ms(5);if(0 == key3){hour1--;if(hour1 < 0){hour1 = 23;}while(!key3);timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';LCD_write_char(2,1,timestr1[0]);LCD_write_char(3,1,timestr1[1]);LCD_write_command(0xc0+3);}}break;case 4:b = 0;LCD_write_command(0x0c);break;}}}LCD_write_command(0x0c);while(!key4);}/****************************************************************************** ***************************** 判断按键进入时间调整*************************************************************************************************************** *******/uint8 scan_key(void){uint8 number = 1;uint8 a = 0;if(0 == key1){Delay1Ms(5);if(0 == key1){while(!key1);a = 1;LCD_write_command(0xc0+9);LCD_write_command(0x0f);}}while(a){if(0 == key1){Delay1Ms(5);if(0 == key1){number++;while(!key1);TR0 = 0;if(number == 2){LCD_write_command(0xc0+6);LCD_write_command(0x0f);}if(number == 3){LCD_write_command(0xc0+3);LCD_write_command(0x0f);}if(number == 4){LCD_write_command(0x80+11);LCD_write_command(0x0f);}if(number == 5){LCD_write_command(0x80+8);LCD_write_command(0x0f);}if(number == 6){LCD_write_command(0x80+5);LCD_write_command(0x0f);}if(number == 7){LCD_write_command(0xc0+14);LCD_write_command(0x0f);}if(8 == number){LCD_write_command(0x0c);a = 0;number = 0;}}}switch(number){case 1:if(0 == key2) //秒加1的设置{Delay1Ms(5);if(0 == key2){sec++;if(60 == sec){sec = 0;}timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';LCD_write_char(8,1,timestr[6]);LCD_write_char(9,1,timestr[7]);LCD_write_command(0xc0+9);while(!key2);}}if(0 == key3) //秒减1的设置{Delay1Ms(5);if(0 == key3){sec--;if(sec < 0){sec = 59;}timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';LCD_write_char(8,1,timestr[6]);LCD_write_char(9,1,timestr[7]);LCD_write_command(0xc0+9);while(!key3);}}break;case 2:if(0 == key2) //分加1的设置{Delay1Ms(5);if(0 == key2){min++;if(60 == min){min = 0;}timestr[4] = min%10+'0';timestr[3] = min/10+'0';;LCD_write_char(5,1,timestr[3]);LCD_write_char(6,1,timestr[4]);LCD_write_command(0xc0+6);while(!key2);}}if(0 == key3) //分减1的设置{Delay1Ms(5);if(0 == key3){min--;if(min < 0){min = 59;}timestr[4] = min%10+'0';timestr[3] = min/10+'0';LCD_write_char(5,1,timestr[3]);LCD_write_char(6,1,timestr[4]);LCD_write_command(0xc0+6);while(!key3);}}break;case 3:if(0 == key2) //时加1的设置{Delay1Ms(5);if(0 == key2){hour++;while(!key2);if(24 == hour){hour = 0;}timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';LCD_write_char(2,1,timestr[0]);LCD_write_char(3,1,timestr[1]);LCD_write_command(0xc0+3);}}if(0 == key3) //时减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);hour--;if(hour < 0){hour = 23;}timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';LCD_write_char(2,1,timestr[0]);LCD_write_char(3,1,timestr[1]);LCD_write_command(0xc0+3);}}break;case 4:if(0 == key2) //日加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);calculate = 1;if(calculate == 1){if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day++;if(day > 31){day=1;}}if(month==4|month==6|month==9|month==11){day++;if(day > 30){day=1;}}if(month == 2){cal_year = 1;while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day++;if(day > 30){day=1;}}else{day++;if(day > 29){day=1;}}}calculate = 0;}daystr[9] = day%10+'0';daystr[8] = day/10+'0';LCD_write_char(10,0,daystr[8]);LCD_write_char(11,0,daystr[9]);LCD_write_command(0x80+11);}}if(0 == key3) //日减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);calculate = 1;if(calculate == 1){if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day--;if(day == 0){day=31;}}if(month==4|month==6|month==9|month==11){day--;if(day == 0){day=30;}}if(month == 2){cal_year = 1;while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day--;if(day == 0){day=30;}}else{day--;if(day == 0){day=29;}}}calculate = 0;}daystr[9] = day%10+'0';daystr[8] = day/10+'0';LCD_write_char(10,0,daystr[8]);LCD_write_char(11,0,daystr[9]);LCD_write_command(0x80+11);}}break;case 5:if(0 == key2) //月加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);month++;if(13 == month){month = 1;}daystr[6] = month%10+'0';daystr[5] = month/10+'0';LCD_write_char(7,0,daystr[5]);LCD_write_char(8,0,daystr[6]);LCD_write_command(0x80+8);}}if(0 == key3) //月减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);month--;if(month == 0){month = 12;}daystr[6] = month%10+'0';daystr[5] = month/10+'0';LCD_write_char(7,0,daystr[5]);LCD_write_char(8,0,daystr[6]);LCD_write_command(0x80+8);}}break;case 6:if(0 == key2) //年加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);year++;}daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';LCD_write_char(2,0,daystr[0]);LCD_write_char(3,0,daystr[1]);LCD_write_char(4,0,daystr[2]);LCD_write_char(5,0,daystr[3]);LCD_write_command(0x80+5);}if (0 == key3) //年减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);year--;if(year == 0){year = 2020;}daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';LCD_write_char(2,0,daystr[0]);LCD_write_char(3,0,daystr[1]);LCD_write_char(4,0,daystr[2]);LCD_write_char(5,0,daystr[3]);LCD_write_command(0x80+5);}}break;case 7:if(0 == key2) //星期加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);week++;if(7 == week){week = 0;}LCD_write_char(14,1,week+'0');LCD_write_command(0xc0+14);}}if (0 == key3) //星期减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);week--;if(week < 0){week = 6;}LCD_write_char(14,1,week+'0');LCD_write_command(0xc0+14);}}break;case 8:TR0 = 1;break;}}}/****************************************************************************** ***************************** 主函数******************************************************************************************************************* *******/void main(void){systimer0_init();LCD_init();LCD_write_str(0,0,str1);LCD_write_str(0,1,str2);LCD_write_str(13,0,str3);speaker = 0;while (1){TimeChange();scan_key();naozhong();LCD_write_str(2,0,daystr);LCD_write_str(2,1,timestr);LCD_write_char(14,1,WeekData);if(1 == run){if(sec==sec1 & month==month1 & hour==hour){speaker_num =30;beep = 1;}}else{speaker_num =0;beep = 0;}}}/****************************************************************************** ***************************** 定时中断0 ******************************************************************************************************************* *******/void time_0() interrupt 1{TH0 = 0xDC; // 定时10msTL0 = 0x00;number++;if(number ==100){sec++;if(beep){speaker=!speaker;speaker_num--;if(speaker_num == 0){beep=0;speaker = 0;}}if(sec == 60){sec = 0;min++;if(min == 60){min = 0;hour++; //小时加1speaker_num = hour%12; //蜂鸣器响的次数beep = 1;if(hour == 24){hour = 0;calculate = 1;if(calculate == 1) //判断这个月有多少天{if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day++;if(month==7|month==12){week++;if(7 == week){week = 0;}}else{if(day <= 31){week++;if(7 == week){week = 0;}}}if(day > 31){if(month==7|month==12){day = 1;}else{day=0;}month++;if(month > 12){month=1;year++;cal_year=1;}}}if(month==4|month==6|month==9|month==11) {day++;week++;if(7 == week){week = 0;}if(day > 30){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}if(month == 2){while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day++;if(day <= 30){week++;if(7 == week){week = 0;}}if(day > 30){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}else{day++;week++;if(7 == week){week = 0;}if(day > 29){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}}}calculate=0;}}}}}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Lcd1602.c子程序:////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /****************************************************************************** ***************************** lcd1602低层函数********************************************************************************************************************* *******/# include <reg52.h># include <intrins.h># include "lcd16024.h"# define LCD_DATA P0 //LCD1602的数据口定义sbit LCD_RS = P0^0; //LCD1602控制线的定义,4位控制方式sbit LCD_RW = P0^1;sbit LCD_EN = P0^2;/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void Delay1Us(uint16 n){for(;n>0;n--){_nop_();}}/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void Delay1Ms(uint16 n){while(n--){Delay1Us(1000);}}/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void LCD_en_write(void){LCD_EN=0;LCD_EN=1;Delay1Us(1);LCD_EN=0;}/****************************************************************************** ************************ LCD写一个字节命令函数************************************************************************************************************ *******/void LCD_write_command(uint8 command){Delay1Us(16);LCD_RS=0;LCD_RW=0;LCD_DATA&=0x0f;LCD_DATA|=command&0xf0;LCD_en_write();command=command<<4;LCD_DATA&=0x0f;LCD_DATA|=command&0xf0;LCD_en_write();}/****************************************************************************** ********************** LCD写一个字节数据函数*************************************************************************************************************** *******/void LCD_write_data(uint8 Data){Delay1Us(16);LCD_RS=1;LCD_RW=0;LCD_DATA&=0x0f;LCD_DATA|=Data&0xf0;LCD_en_write();Data=Data<<4;LCD_DATA&=0x0f;LCD_DATA|=Data&0xf0;LCD_en_write();}/****************************************************************************** ***************************** LCD1602光标定位函数************************************************ x--列0~15;y--行0~1********************************************************************************************************* *******/void LCD_set_xy(uint8 x,uint8 y){uint8 address;if(y==0)address=0x80+x;else address=0xc0+x;LCD_write_command(address);}/***************************************************************************************************** LCD1602 初始化函数,四位显示方式******************************************************************************************************* *******/void LCD_init(){LCD_write_command(0x28);// Delay1Us(40);LCD_write_command(0x28);LCD_write_command(0x0c);LCD_write_command(0x01);LCD_write_command(0x06);Delay1Ms(2);}/****************************************************************************** ***************************** LCD写字符串函数******************************************************* x--列0~15;y--行0~1******************************************************* s指向字符串数组**************************************************************************************************************** *******/void LCD_write_str(uint8 x,uint8 y,uint8 *s){LCD_set_xy(x,y);while(*s){LCD_write_data(*s);s++;}}/****************************************************************************** ***************************** LCD写一个字符函数******************************************************* x--列0~15;y--行0~1******************************************************* d--字符的ASCII码**************************************************************************************************************** *******/void LCD_write_char(uint8 x,uint8 y,uint8 d){LCD_set_xy(x,y);LCD_write_data(d);}/*////////////////////////////////////////////////////////////////// 等待繁忙标志/////////////////////////////////////////////////////////////////void LCD_wait(void){P0 = 0xFF;do{LCD_RS = 0;LCD_RW = 1;LCD_EN = 0;LCD_EN = 1;}while (BUSY == 1);LCD_EN = 0;}*//****************************************************************************** ***************************** LCD1602左移********************************************************************************************************************* *******void LCD_youyi(uint8 y,uint8 *s){LCD_write_str(17,y,s);for(a=0;a<16;a++){LCD_write_command(0x1c); //左移LCD_write_command(0x1c); 为右移Delay1Ms(6);}}*///LCD_write_command(0x0d);//光标闪烁//LCD_write_command(0x0e);//光标显示不闪烁//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Lcd1602.h头文件////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef _LCD16024_H_#define _LCD16024_H_typedef unsigned char uint8;typedef unsigned int uint16;typedef unsigned long uint32;void Delay1Us(uint16 n);void Delay1Ms(uint16 n);void LCD_write_data(uint8 dat);void LCD_write_command(uint8 com); //BuysC为0时忽略忙检测void LCD_set_xy(uint8 x,uint8 y);void LCD_en_write(void);void LCD_write_char(uint8 x, uint8 y, uint8 Data1);void LCD_write_str(uint8 x, uint8 y,uint8 *s);void LCD_init();//void LCD_wait(void);//void LCD_youyi(uint8 y,uint8 *s);#endif。
题目:定时闹钟目录一、概述 (1)1.1设计目的及意义 (1)1.2设计任务 (1)1.3设计系统的主要功能 (1)二、系统总体方案及硬件设计 (2)2.1系统总体方案 (2)2.2系统设计总框图 (2)2.3硬件设计 (2)2.3.1单片机最小系统设计 (2)2.3.2报警模块设计 (6)2.3.3显示模块设计 (7)2.3.4调时模块设计 (9)三、软件设计 (10)3.1主程序流程图 (10)3.2定时中断子程序流程图 (11)3.3程序设计 (11)四、系统的仿真与调试 (12)4.1 proteus软件仿真 (12)4.2系统的调试 (11)五、设计总结与体会 (13)参考文献 (13)附录1:源程序代码 (14)附录2:系统原理图 (24)一、概述1.1设计目的及意义学习和巩固单片机技术、电子技术、传感器技术及智能仪器等知识,使对已学过的基础知识能有更深入的理解,并融会贯通。
学会独立思考、独立工作,培养一定的自学能力和独立分析问题能力,以及增强系统地运用已学理论知识去解决实际问题的能力,同时培养成良好的科学态度和严谨的设计习惯。
1.2设计任务完成所选题目的分析与设计,达到技术性能要求。
提交正式课程设计总结报告一份。
本文设计的定时闹钟的核心模块采用AT89C51芯片,时、分、秒用6位LED数码管显示。
在电路中通过四个按键S1、S2、S3和S4来进行定时、调时和复位,定时时间到通过蜂鸣器发出报警声。
1.3设计系统的主要功能(1)能显示时时-分分-秒秒。
(2)能够设置定时时间、修改定时时间。
(3) 定时时间到能发出报警声。
二、系统总体方案及硬件设计2.1系统总体方案(1) 由于LED显示器相对于其它显示器(如LCD显示器)来说其价格要便宜许多,而且亮度更高,耐温范围较广,所以采用6位数码管来显示“时时-分分-秒秒”。
(2) 时间的定时用单片机内部时钟电路,在一定的时间内能使其误差较小,如经过一年其误差才仅有数秒。
沈阳航空航天大学课程设计报告课程设计名称:微机系统综合课程设计课程设计题目:闹钟程序设计院(系):计算机学院专业: 计算机科学与技术班级:学号:姓名:指导教师:说明:结论(优秀、良好、中等、及格、不及格)作为相关教环节考核必要依据;格式不符合要求;数据不实,不予通过。
报告和电子数据必须作为实验现象重复的关键依据。
学术诚信声明本人声明:所呈交的报告(含电子版及数据文件)是我个人在导师指导下独立进行设计工作及取得的研究结果。
尽我所知,除了文中特别加以标注或致谢中所罗列的内容以外,报告中不包含其他人己经发表或撰写过的研究结果,也不包含其它教育机构使用过的材料。
与我一同工作的同学对本研究所做的任何贡献均己在报告中做了明确的说明并表示了谢意。
报告资料及实验数据若有不实之处,本人愿意接受本教学环节“不及格”和“重修或重做”的评分结论并承担相关一切后果。
本人签名: 日期:年月日沈阳航空航天大学课程设计任务书目录学术诚信声明 (I)1 总体设计方案 (1)1.1课程设计的内容和要求 (1)1.2课程设计原理 (1)1.3课程设计思路 (1)2 详细设计方案 (2)2.1实现方法 (2)2.2模块设计 (2)2.2.1 主程序流程图 (2)2.2.2中断程序流程图 (3)2.2.2.1基本显示模块设计 (3)2.2.2.2时间设定模块设计 (3)2.2.2.3闹铃功能的实现 (4)3 调试及结果分析 (5)3.1调试步骤及方法 (5)3.2实验结果 (5)参考文献 (6)附录(源程序) (7)沈阳航空航天大学课程设计报告1 总体设计方案1.1 课程设计的内容和要求本设计是定时闹钟的设计,由MCS51单片机芯片和LED数码管为核心,辅以必要的电路,构成的一个单片机电子定时闹钟。
定时闹钟设计采用单片机来完成,用数码管显示“时”,“分”,“秒”。
使用MCS51系列单片机,单片机结合七段显示器设计的简易定时闹铃时钟,可以设置现在的时间、闹钟定时的时间,可以设置两个闹钟,若时间到则用发光二极管显示。
功能描述:产品可以显示时间和日期,时间格式为 hh mm ss 日期格式为yy.mm.dd时间和日期轮流显示。
时间显示5S 日期显示3S。
可以设置5个闹铃,闹铃音乐可以设置两种:毛驴和童年。
三个按键对时间和闹铃进行设置,六个LED进行显示。
计时采用DS1307。
继电时间不丢失,设置过的闹铃也不丢失。
闹铃音乐由单片机的两个定时器去产生频率实现。
部分程序如下://*************************************************//************************************************//***********************************************//程序名:DS1307 时钟程序//功能描述:用六个八段LED 轮流显示时间// 和日期。
有6个闹钟。
上电时从DS1307中读出// 当前时间、日期、闹钟。
//////////#include <reg52.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned int#define LED P2#define LEDBit P0#define nop _nop_()#define LightCount 40#define LightMax 80sbit SCL=P3^1;sbit SDA=P3^0;sbit ModeKey=P1^0;sbit UpKey=P1^1;sbit DownKey=P1^2;sbit Speak=P3^6;code ucharLCD_NUM[10]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};//0x25,//uchar Clock[]={0x88,0x88,0x88};codeuint Music_Sound_Long1[]={4,8,4,8,8,4,8,4,4,4,/*童年*/8,4,8,4,4,4,4,4,16,4,4,8,4,4,4,4,4,8,4,4,4,8,4,4,4,4,4,4,16,4,4,8,4,4,4,4,4,8,4,4,4,8,4,4,4,4,4,4,16,4,8,4,8,4,4,4,8,8,4,4,4,4,4,4,4,4,4,4,4,16,4,8,4,8,8,4,8,4,4,4,8,4,8,4,4,4,4,4,16,4,8,4,8,8,4,8,4,4,4,8,4,8,4,4,4,4,4,16,0},Music_Sound_Long2[]={4,4,4,4,4,4,4,4,4,4,/*小毛驴*/4,4,16,4,4,4,4,4,4 ,4,4,4,4,4,4,8,4,4,4,4,4,4,4,4,4,4,4,4,4,16,4,4,4,4,2,2,2,2,4,4,4,4,4,4,16,0},Music_Sound_Tone1[]={379,379,379,379,425,477,477,477,425,477,568,637,637,637,568,637, 425,379,477,719,637,637,719,637,568,568, 506,568,568,568,637,477,477,477,477,568,477,568,637,719,637,637,719,637,568,568,506,568,568,568,637,477,477,477,477,568,568,477,851,318,318,318,318,379,425,477,477,568,477,568,477,425,425,425,425,425,477,379,425,425,379,379,379,379,425,477,477,477,425,477,568,637,637,637,568,637,425,379,477,379,379,379,379,425,477,477,477,425,477,568,637,637,637,568,637,425,379,477,0}, /*童年*/Music_Sound_Tone2[]={956,956,956,719,637,637,637,637,568,568, /*小毛驴*/568,477,637,716,716,716, 568,719,719,719,719,851,851,851,851,637,637,956,956,956,719,637,637,637,637,568,568,568,477,637,716,716,716,568,719,719,719,719,719,719,851,851,851,719,956,0};uchar DS1307[27]={0};//00 为秒,01为分,02为时//10 为日,11为月,12为年//20 为闹钟控制,21为分,22为时。
单片机技术课程设计数字电子钟学院:班级:姓名:学号:教师:摘要电子钟在生活中应用非常广泛,而一种简单方便的数字电子钟则更能受到人们的欢迎。
所以设计一个简易数字电子钟很有必要。
本电子钟采用AT89C52单片机为核心,使用12MHz 晶振与单片机AT89C52 相连接,通过软件编程的方法实现以24小时为一个周期,同时8位7段LED数码管(两个四位一体数码管)显示小时、分钟和秒的要求,并在计时过程中具有定时功能,当时间到达提前定好的时间进行蜂鸣报时。
该电子钟设有四个按键KEY1、KEY2、KEY3、KEY4和KEY5键,进行相应的操作就可实现校时、定时、复位功能。
具有时间显示、整点报时、校正等功能。
走时准确、显示直观、运行稳定等优点。
具有极高的推广应用价值。
关键词:电子钟 AT89C52 硬件设计软件设计目录NO TABLE OF CONTENTS ENTRIES FOUND.一、数字电子钟设计任务、功能要求说明及方案介绍1.1 设计课题设计任务设计一个具有特定功能的电子钟。
具有时间显示,并有时间设定,时间调整功能。
1.2 设计课题的功能要求说明设计一个具有特定功能的电子钟。
该电子钟上电或按键复位后能自动显示系统提示符“d.1004-22”,进入时钟准备状态;第一次按电子钟启动/调整键,电子钟从12时59分0秒开始运行,进入时钟运行状态;按电子钟S5键,则电子钟进入时钟调整状态,此时可利用各调整键调整时间,调整结束后可按S5键再次进入时钟运行状态。
1.3 设计课的设计总体方案介绍及工作原理说明本电子钟主要由单片机、键盘、显示接口电路和复位电路构成,设计课题的总体方案如图1所示:图1-1总体设计方案图本电子钟的所有的软件、参数均存放在AT89C52的Flash ROM和内部RAM 中,减少了芯片的使用数量简化了整体电路也降低了整机的工作电流。
键盘采用动态扫描方式。
利用单片机定时器及计数器产生定时效果通过编程形成数字钟效果,再利用数码管动态扫描显示单片机内部处理的数据,同时通过端口读入当前外部控制状态来改变程序的不同状态,实现不同功能。
海南大学单片机课程设计报告专业:0 9电子信息工程姓名:田飞20091601310050指导教师:李京兵摘要单片机自 20 世纪 70 年代问世以来,以其极高的性能价格比,受到人们的重视和关注,应用很广泛、发展很快。
Intel公司生产的 MCS-8051 系列单片机是各单片机中最为典型和最有代表性的一种。
本次设计以 MCS-8051 芯片为核心,辅以必要的外围电路,设计了一个结构简单,功能齐全的数字时钟,它由 5V 直流电源供电。
在硬件方面,单片机外接 12MHz 晶振,使用八个七段数码管来进行显示。
LED 采用动态扫描显示,使用 74LS245 芯片进行位驱动。
通过 LED 能够准确明亮地显示时、分、秒;四个简单的按键实现对时间的调整;蜂鸣器实现闹钟响铃功能;软件方面采用 C 语言编程。
整个电子钟系统能完成时间的显示、调时和一组定时闹钟的功能。
设计过程中使用 Kei uVision 4 单片机模拟调试软件编写调试程序,并用 EDA 工具软件 Proteus ISIS 7 进行仿真。
硬件简明,程序正确,仿真结果满足设计要求。
关键词: 51单片机,定时器,中断,闹钟,LED目录摘要 (1)绪论 (3)第一章系统设计 (4)一、器件选型 (4)二、硬件接线设计 (6)三、系统综述 (9)1.3.1 上电界面 (9)1.3.2 调时界面 (9)1.3.3 闹钟设定界面 (10)1.3.4 正常走时界面 (10)1.3.5 闹钟响应 (11)四、软件部分 (11)1.4.1 主函数流程图 (11)1.4.2 定时器T0中断服务程序流程图 (12)1.4.3 闹钟响应程序流程图 (13)1.4.4 键盘扫描程序流程图 (14)第二章参数计算 (15)一、定时器T0 (15)2.1.2 定时器T0初值计算 (16)二、数码管驱动码 (16)2.2.1 位选码 (17)2.2.2 段选码 (17)第三章结论 (18)参考文献 (19)附录 (20)绪论20 世纪末,电子技术获得了飞速的发展。
电子时钟设计一、设计目的在我们现代日常生活中,电子时钟已得到及其广泛的应用,已成为我们日常生活中的不可或缺的一部分。
本次设计的主要目的即是利用51单片机设计一个可实现24小时计时的电子时钟,计时从0时0分0秒开始,到23小时59分59秒后返回0时0分0秒自动重新开始计时。
本设计拥有时间调整功能和时间显示功能,无年、月计数和闹钟功能。
二、需求分析本设计中的时钟要求使用8个8段数码管显示当前时间,其中秒单元与分单元中间以“-”符号隔开,分单元与时单元中间同样以“-”符号隔开。
计时范围为从00-00-00到23-59-59,当计时到23-59-59后自动返回00-00-00并重新开始计时。
设计中使用3个按键分为set、add和sub,当在计时功能工作时按下set键即可进入调时模式,在调试模式下累计按6次set后便退出调时功能,重新返回计时功能。
三、总体设计1、总体设计框图图3-1 总体设计框图2、器件选型:主要使用的器件为STC89C51RC型单片机。
该型号的单片机有P1、P2、P3、P4共4个准双向口,且包含3个16位可编程定时/计数器T0、T1、T2。
其定时可由硬件电路与中断方式控制,而定时时间和范围则完全由所编写的代码来确定和改变。
在本次设计中主要实用0号和1号定时/计数器,通过设置使它们均实现50ms计数,其中0号计数器配合20次循环计数以实现1m计时,1号计数器配合10次循环计数以实现对相应调整位的0.5m闪烁。
设计中还主要使用到3个74LS373数据锁存器、1个74LS244输入缓冲器以及8个8段数码显示器。
四、硬件设计1、硬件框图图4-1 硬件框图2、硬件模块设计a、时间计时模块设计:该模块的功能实现是将十位时、个位时、十位分、个位分、十位秒和个位秒分别存入s_hou、g_hou、s_min、g_min、s_sec和g_sec中,每个数值对应一个无符号字节。
T0计数器实现计数功能,但计数满20次50ms即1000ms时,g_sec 的值加1,当g_sec计数值为10时将g_sec清零并使s_sec计数加1,以此类推,直到计数值为23-59-59,并在下一秒返回00-00-00。
#include<reg52.h> //头文件#include<intrins.h>#define uchar unsigned char//宏定义#define uint unsigned intsbit key1=P3^5; //位声明sbit key2=P3^6;sbit key3=P3^7;sbit fmq=P2^0;uchar code table[]={0x3f,0x06,0x5b,//数码管显示的数值0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0xbf,0x86,0xdb,//带小数点的数值0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};void jia(); //函数声明void jian();uchar table_1[6]; //定义数组,数组内含有6个数值uchar table_2[6];uchar shi=23,fen=59,miao=50; //显示初始值uchar shi1,fen1,miao1,shi2,fen2,miao2,shi3,fen3,miao3;//定义全局变量uchar flag,flag1,cnt,count;//定义全局变量void delay(uchar i) //延时函数,用于动态扫描数码管{uchar x,y;for(x=i;x>0;x--)for(y=110;y>0;y--);}void init() //初始化函数{TMOD=0X01; //工作方式1TH0=(65536-50000)/256; //定时时间为:50msTL0=(65536-50000)%256;ET0=1; //打开定时器EA=1; //开总中断TR0=1; //启动定时器}void display() //显示子函数,用于显示时间数值{uchar i,j;table_1[0]=miao%10; //分离秒的各位与十位table_1[1]=miao/10;table_1[2]=fen%10+11; //分离分的各位与十位table_1[3]=fen/10;table_1[4]=shi%10+11; //分离时的各位与十位table_1[5]=shi/10;j=0x7f; //从秒到时的扫描for(i=0;i<6;i++){P2=j;P0=table[table_1[i]];//显示数值delay(10);j=_cror_(j,1);//循环右移}}void display_1() //显示子函数,用于显示定时时间{uchar i,j;table_2[0]=miao2%10; //以下含义同上table_2[1]=miao2/10;table_2[2]=fen2%10+11;table_2[3]=fen2/10;table_2[4]=shi2%10+11;table_2[5]=shi2/10;j=0x7f;for(i=0;i<6;i++){P2=j;P0=table[table_2[i]];delay(10);j=_cror_(j,1);}}void shijian() //时间子函数{if(flag>=20) //判断是否到一秒{flag=0; //到了,则标志位清零miao++; //秒加1if(miao>=60) //判断秒是否到60s{miao=0;//到了,则清零fen++; //分加1if(fen>=59) //以下含义同上{fen=0;shi++;if(shi>23)shi=0;}}}}void key_scan() //键盘扫描子函数{uchar i; //定义局部变量if(key1==0){while(!key1) //防止掉显{if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8){display();}if(cnt==5||cnt==6||cnt==7){display_1();}}cnt++; //记下按键key1按下的次数if(cnt==1) //第一次按下,停止计数TR0=0;if(cnt==2) //第二次按下{miao1=miao; //保存秒的数值miao=99;//显示99,表示可以调节秒的数值了for(i=0;i<100;i++)display(); //显示99miao=miao1; //恢复前一刻秒的数值}if(cnt==3) //以下含义同上{fen1=fen;fen=99;for(i=0;i<100;i++)display();fen=fen1;}if(cnt==4){shi1=shi;shi=99;for(i=0;i<100;i++)display();shi=shi1;}if(cnt==5){miao1=miao2;miao2=88;for(i=0;i<100;i++)display_1();miao2=miao1;}if(cnt==6){fen1=fen2;fen2=88;for(i=0;i<100;i++)display_1();fen2=fen1;}if(cnt==7){shi1=shi2;shi2=88;for(i=0;i<100;i++)display_1();shi2=shi1;}if(cnt==8) //第八次按下{TR0=1; //开始计数cnt=0; //按下次数清零}}if(key2==0) //判断key2是否按下{while(!key2) //防止掉显{if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8){display();}if(cnt==5||cnt==6||cnt==7){display_1();}}jia();//调用加1的子函数}if(key3==0) //判断key3是否按下{while(!key3) //防止掉显{if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8){display();}if(cnt==5||cnt==6||cnt==7){display_1();}}jian(); //调用减1子函数}}void jia() //加1子函数{if(cnt==2) //判断key1按下的次数是否为2{miao++; //是,则秒加1if(miao>59) //判断秒是否大于59,是,则秒清零miao=0;}if(cnt==3) //以下含义同上{fen++;if(fen>59)fen=0;}if(cnt==4){shi++;if(shi>23)shi=0;}if(cnt==5){miao2++;if(miao2>59)miao2=0;}if(cnt==6){fen2++;if(fen2>59)fen2=0;}if(cnt==7){shi2++;if(shi2>23)shi2=0;}}void jian() //减1子函数{if(cnt==2) //判断key1按下的次数是否为2,是则秒减1{miao--;if(miao==255) //判断秒是否减到255,是,则秒清零miao=59;}if(cnt==3){fen--;if(fen==255)fen=59;}if(cnt==4){shi--;if(shi==255)shi=23;}if(cnt==5){miao2--;if(miao2==255)miao2=59;}if(cnt==6){fen2--;if(fen2==255)fen2=59;}if(cnt==7){shi2--;if(shi2==255)shi2=23;}}void clock() //闹铃子函数{if(miao2==miao) //显判断秒的数值是否相等if(fen2==fen) //是,在判断分是否相等if(shi2==shi) //是,再判断时是否相等{flag1=0; //是,则标志位,flag1清零while(!(flag1==100)) //判断flag1是否到100{fmq=0; //没有,则,继续驱动蜂鸣器响,时间约为:5sshijian(); //调用时间子函数display(); //调用显示子函数}fmq=1;//关闭蜂鸣器}}void main(){init();//调用初始化子函数while(1){key_scan(); //调用键盘扫描子函数shijian(); //时间子函数clock(); //闹钟子函数//显示子函数if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8){display();}if(cnt==5||cnt==6||cnt==7){display_1();}}}void time0() interrupt 1 //定时器0{TH0=(65536-50000)/256; //初值50msTL0=(65536-50000)%256;flag++; //标志位flag1++;}参考链接:/news/2010-02/1914.htm。