基于51单片机的智能电子时钟设计_第1页
基于51单片机的智能电子时钟设计_第2页
基于51单片机的智能电子时钟设计_第3页
基于51单片机的智能电子时钟设计_第4页
基于51单片机的智能电子时钟设计_第5页
已阅读5页,还剩95页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

课题名称:单片机应用指导教师:系别:电子信息系专业:应用电子技术班级:姓名:目录封面·········································································1任务书·······································································2目录·········································································3第一章智能电子时钟的设计内容················································4(1.1)电子时钟设计思路························································4(1.2)电子时钟的程序流程图····················································5(1.3)电子时钟原理图··························································6(1.4)电子时钟的功能说明······················································7第二章电子时钟的硬件内容····················································8(2.1)时钟插件指引图·························································8(2.2)时钟零件清单···························································9(2.3)焊接完毕的电子时钟····················································10第三章电子时钟的软件设计····················································11(3.1)倒计时版本····························································11(3.2)闹钟版本······························································12毕业设计小节································································13致谢········································································14主要参考文献································································15智能电子时钟设计内容1.1电子时钟的设计思路1.要达到的目的利用51单片机及数码管实现时钟的基本功能。在6个数码管上显示年月日、并可切到时分秒。利用4个按键,实现年月日及时分秒的调整。能设置一组闹钟。能实现秒表功能。2.关键问题的解决6个数码管的动态扫描。走时需要使用定时中断,以确保精度。四个功能模块需要重复利用6个数码管来显示,需要组织好复杂的逻辑。1.2电子时钟的程序流程图1.3电子时钟的原理图电子时钟的功能说明操作说明: 4个按键依次为:ModeSetUpDown模式选择: 按Mode键,选择不同的功能,依次为:时间秒表倒计时日期时间调整: 按Mode键进入时间模式 按Set切换要设置的时分秒,按Up或Down对闪烁位置的时间进行设置。秒表功能: 按Mode键进入秒表模式 按Up键启动秒表 在秒表计时过程中按Up键,暂停跑时。再按Up将继续跑时。 在暂停跑时状态下按Set键,重置秒表时间。倒计时功能: 按Mode键进入倒计时功能。 按Set键切换时分秒准备。 在时分秒用Up或Down键设置倒计时的时间长度。 在准备状态下按Down或Up键启动日期功能: 按Mode键进入日期功能。 按Set键切换年—月—日,按Up或Down进行调整。第二章电子时钟的硬件内容2.1时钟插件指引图2.2时钟零件清单CLK01BOMNODescriptionCommentDesignatorQuantity1电池盒BATTERYBT112蜂鸣器BUZZBZ113电容0.1C1,C424电容22pC2,C325电解电容100uFCx116数码管seg-2-10pDeg1,Deg2,Deg337LEDDot1LED1,LED228LEDDot2LED3,LED429LEDTimeLED5110LEDAlarmLED6111LEDStopWatchLED7112LEDDateLED8113三极管8550Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8814三极管8050Q9,Q10,Q11,Q12,Q13,Q14,Q15715电阻10kR1,R2,R3,R4,R5,R6,R7,R8,R17,R18,R19,R20,R21,R22,R23,R24,R25,R26,R27,R282016电阻220R9,R10,R11,R12,R13,R14,R15'R16817轻触按键MODES1118轻触按键SETS2119轻触按键UPS3120轻触按键DOWNS4121单刀双掷开关SW-SPDTS5122单片机P89C51RD2HBPU11IC座123晶振12MXT112.3焊接完毕的时钟第三章电子时钟的软件设计3.1倒计时版本/*******************************************************************************项目编号:CLK01名称:多功能时钟'tonkeys&轻风工作室提供'专业VB编程,毕业设计VB代编程,单片机程序设计。'QQ:58507961(技术支持)10+年编程经验'拍拍:/58507961'淘宝:日期:2021-8-25*******************************************************************************/#include <at89x51.h"//-------------------------------------------------------------------------//缩写宏定义//-------------------------------------------------------------------------#define ucharunsignedchar#defineuintunsignedint//数码管位置宏#defineDEG1 4#defineDEG2 5#defineDEG3 2#defineDEG4 3#defineDEG5 0#defineDEG6 1//-------------------------------------------------------------------------//函数声明//-------------------------------------------------------------------------uchar GetMDays(ucharcyear,ucharcmonth); //返回当前日期的月份天数void T0_init();void LedSet(ucharpos,ucharnum);uchar GetKey(void); //读取按键 void TaskSecond(); //秒表模式void TaskSetTime(); //时间设置模式 void TaskStopWatch();//倒计时void TaskDate(); //日期uchar InKey(); //等待一个按键void Beep(); //200ms按键音//-------------------------------------------------------------------------//全局变量//-------------------------------------------------------------------------uchartm_year=13,tm_month=8,tm_day=1; //年月日uchartm_hour=10,tm_minute=15,tm_second=0; //时分秒uchartm_x10ms=0; //x10mSuintgms;ucharmode=0;ucharbeep_delay=0;ucharLight_Set=7; //7--最亮20--暗ucharLight_Delay=60; //延时变暗//-------------------------------------------------------------------------//主函数//-------------------------------------------------------------------------voidmain(){ T0_init(); while(1) { //显示时间 LedSet(DEG1,tm_hour/10); LedSet(DEG2,tm_hour%10); LedSet(DEG3,tm_minute/10); LedSet(DEG4,tm_minute%10); LedSet(DEG5,tm_second/10); LedSet(DEG6,tm_second%10); LedSet(6,0xE0); switch(GetKey()) { case0x01: //mode Beep(); TaskSecond(); //秒表模式 TaskStopWatch(); //倒计时模式 TaskDate(); //日期模式 break; case0x02: //Set Beep(); TaskSetTime(); //进入时间设置模式 break; case0x04: //up break; case0x08: //down break; default: ; } }}//-------------------------------------------------------------------------//LED动态扫描//-------------------------------------------------------------------------ucharcodeled_code[]={3,207,42,74,198,82,18,203,2,66, 0,0,0,0,0,0,0,0,0,0,255};uchar led_buf[8];uchar led_scan_step=0;voidLedScan(){ P1=0;//off if(led_scan_step<7) { P0=led_buf[led_scan_step]; P1=1<<led_scan_step; } led_scan_step++; if(led_scan_step>Light_Set)led_scan_step=0;}//-------------------------------------------------------------------------//定时器初始化 125uSfor12MhzOsc//-------------------------------------------------------------------------voidT0_init(){ TMOD=0X02; TH0=(256-125); TL0=(256-125); EA=1; ET0=1; TR0=1; }//-------------------------------------------------------------------------//定时中断//-------------------------------------------------------------------------ucharxcou=0;ucharms=0;bitbz_flag;bitbeep_en=0;voidtime0(void)interrupt1{ if(beep_en) { bz_flag=!bz_flag; P2_7=bz_flag; } xcou++; if(xcou==2) { LedScan(); //LED扫描 }elseif(xcou==8) { xcou=0; ms++; gms++; //临时计数用的 if(ms==10) { tm_x10ms++; ms=0; if(beep_delay!=0) { beep_delay--; if(beep_delay!=0) beep_en=1; else beep_en=0; } if(tm_x10ms==100) //秒 { tm_second++; if(Light_Delay!=0) { Light_Delay--; if(Light_Delay==0) Light_Set=20; //变暗 else Light_Set=7; //亮度高 } tm_x10ms=0; if(tm_second==60)//分 { tm_minute++; tm_second=0; if(tm_minute==60)//时 { tm_hour++; tm_minute=0; if(tm_hour==24)//日 { tm_day++; tm_hour=0; if(tm_day>GetMDays(tm_year,tm_month)) //月 { tm_month++; tm_day=1; if(tm_month>12)//年 { tm_year++; tm_month=1; } } } } } } } }}//-------------------------------------------------------------------------//返回当前日期的月份天数//-------------------------------------------------------------------------ucharcodedaylist[]={31,28,31,30,31,30,31,31,30,31,30,32};//平年各月天数ucharGetMDays(ucharcyear,ucharcmonth) { ucharrtn; rtn=daylist[cmonth-1]; if(cyear%4==0&&cmonth==2) //闰年2月 rtn=29; return(rtn); }//-------------------------------------------------------------------------//设置指定位置数码管的字符//-------------------------------------------------------------------------voidLedSet(ucharpos,ucharnum){ if(pos>=6) led_buf[pos]=num; else { led_buf[pos]=led_code[num]; }}//-------------------------------------------------------------------------//GetKey读取按键//-------------------------------------------------------------------------ucharGetKey(void){ ucharrtn=0; bitflag; flag=1; if(P3_4==0) rtn=0x01; //mode elseif(P3_5==0) rtn|=0x02; //set elseif(P3_6==0) rtn|=0x04; //up elseif(P3_7==0) rtn|=0x08; //down else flag=0; //pressed if(flag) { gms=0; while(P3_4==0||P3_5==0||P3_6==0||P3_7==0) { if(gms>1000)gms=1000; } if(gms<10) rtn=0;//抗抖过滤小于10ms的信号 else { Beep(); Light_Delay=60; } }else rtn=0; return(rtn);}//-------------------------------------------------------------------------//秒表模式//-------------------------------------------------------------------------void TaskSecond(){ ucharhh=0,mm=0,ss=0,x10ms=0,ccms; uchartmp; while(1) { LedSet(DEG1,0); LedSet(DEG2,0); LedSet(DEG3,0); LedSet(DEG4,0); LedSet(DEG5,0); LedSet(DEG6,0); LedSet(6,0xD4); tmp=InKey(); //等待启动键 if(tmp==0x01) //modekeyexit { return; } elseif(tmp==0x04||tmp==0x08) //setkeystart { //运行中 gms=0; while(1) { if(gms!=0) { ccms++; gms--; if(ccms==10) { x10ms++; ccms=0; if(x10ms==100) { ss++; x10ms=0; if(ss==60) { ss=0; mm++; if(mm==60) { hh++; if(hh==100)hh=0; } } } }elseif(ccms==9) //刷新显示 { if(hh==0) { LedSet(DEG1,mm/10); LedSet(DEG2,mm%10); LedSet(DEG3,ss/10); LedSet(DEG4,ss%10); LedSet(DEG5,x10ms/10); LedSet(DEG6,x10ms%10); LedSet(6,0xD4); }else { LedSet(DEG1,hh/10); LedSet(DEG2,hh%10); LedSet(DEG3,mm/10); LedSet(DEG4,mm%10); LedSet(DEG5,ss/10); LedSet(DEG6,ss%10); LedSet(6,0xD0); } }else { switch(GetKey()) { case0x01: //mode退出 return; break; case0x02: //set暂停 while(1) { tmp=GetKey(); if(tmp==0x01) //modeexit return; elseif(tmp==0x04||tmp==0x08) //继续 break; elseif(tmp==0x02)//重置 { LedSet(DEG1,0); LedSet(DEG2,0); LedSet(DEG3,0); LedSet(DEG4,0); LedSet(DEG5,0); LedSet(DEG6,0); LedSet(6,0xD4); hh=0;mm=0;ss=0;x10ms=0;ccms =0; } } break; } } } } } } }//-------------------------------------------------------------------------//时间设置模式 //-------------------------------------------------------------------------void TaskSetTime(){ uchartmp,pos=0; uintcms=0; gms=0; while(1) { if(gms!=0) { gms--; cms++; tmp=GetKey(); if(tmp==0x01) return; elseif(tmp==0x02) { //set pos++; cms=0; if(pos==3) return; }elseif(tmp==0x04) //up { if(pos==0)//hh { tmp=tm_hour; tmp++; if(tmp>=24)tmp=0; tm_hour=tmp; }elseif(pos==1)//mm { tmp=tm_minute; tmp++; if(tmp>=60)tmp=0; tm_second=0; tm_minute=tmp; }elseif(pos==2) { tm_second=0; } cms=0; }elseif(tmp==0x08) //down { if(pos==0)//hh { tmp=tm_hour; if(tmp==0) tmp=23; else tmp--; tm_hour=tmp; }elseif(pos==1)//mm { tmp=tm_minute; if(tmp==0) tmp=59; else tmp--; tm_second=0; tm_minute=tmp; }elseif(pos==2) { tm_second=0; } cms=0; } if(cms==1) { LedSet(DEG1,tm_hour/10); LedSet(DEG2,tm_hour%10); LedSet(DEG3,tm_minute/10); LedSet(DEG4,tm_minute%10); LedSet(DEG5,tm_second/10); LedSet(DEG6,tm_second%10); LedSet(6,0xE0); }elseif(cms==500) { if(pos==0) { LedSet(DEG1,20); LedSet(DEG2,20); }elseif(pos==1) { LedSet(DEG3,20); LedSet(DEG4,20); }elseif(pos==2) { LedSet(DEG5,20); LedSet(DEG6,20); } }elseif(cms==1000) cms=0; } }} //-------------------------------------------------------------------------//等待一个按键 //-------------------------------------------------------------------------ucharInKey(void){ ucharrtn=0; while(rtn==0) { rtn=GetKey(); } return(rtn);}//-------------------------------------------------------------------------//倒计时//-------------------------------------------------------------------------void TaskStopWatch(){ uchartmp,pos=0; ucharhh=0,mm=0,ss=30,mms=0,m10ms=0; uintcms=0; while(1) { LedSet(DEG1,hh/10); LedSet(DEG2,hh%10); LedSet(DEG3,mm/10); LedSet(DEG4,mm%10); LedSet(DEG5,ss/10); LedSet(DEG6,ss%10); LedSet(6,0xB0); //hh=0,mm=0,ss=30,mms=0,m10ms=0; tmp=InKey(); if(tmp==0x01) return; elseif(tmp==0x02) { //stoptimeset gms=0;cms=0; pos=1; while(1) { if(gms!=0) { cms++; gms--; tmp=GetKey(); if(tmp==0x01) //exit { return; }elseif(tmp==0x02) //nextpos { pos++; if(pos==4) { pos=0; break; } } elseif(tmp==0x04) //up { if(pos==1)//hh { if(hh<99) hh++; else hh=0; }elseif(pos==2)//mm { if(mm<59) mm++; else mm=0; }elseif(pos==3)//s { if(ss<59) ss++; else ss=0; } } elseif(tmp==0x08) //down { if(pos==1)//hh { if(hh>0) hh--; else hh=99; }elseif(pos==2)//mm { if(mm>0) mm--; else mm=59; }elseif(pos==3)//s { if(ss>0) ss--; else ss=59; } } if(cms==1) { LedSet(DEG1,hh/10); LedSet(DEG2,hh%10); LedSet(DEG3,mm/10); LedSet(DEG4,mm%10); LedSet(DEG5,ss/10); LedSet(DEG6,ss%10); LedSet(6,0xB0); }elseif(cms==500) { if(pos==1) { LedSet(DEG1,20); LedSet(DEG2,20); }elseif(pos==2) { LedSet(DEG3,20); LedSet(DEG4,20); }elseif(pos==3) { LedSet(DEG5,20); LedSet(DEG6,20); } }elseif(cms==1000) cms=0; } } } elseif(tmp==0x04||tmp==0x08) { //start cms=0;gms=0; while(1) { if(gms!=0) { cms++; gms--; if(cms%10==1) { if(m10ms==0) { if(ss==0) { if(mm==0) { if(hh==0) //stop break; else { hh--; mm=59; } }else { mm--; ss=59; } }else { ss--; m10ms=99; } }else m10ms--; }else { if(hh!=0) { LedSet(DEG1,hh/10); LedSet(DEG2,hh%10); LedSet(DEG3,mm/10); LedSet(DEG4,mm%10); LedSet(DEG5,ss/10); LedSet(DEG6,ss%10); LedSet(6,0xB0); }else { LedSet(DEG1,mm/10); LedSet(DEG2,mm%10); LedSet(DEG3,ss/10); LedSet(DEG4,ss%10); LedSet(DEG5,m10ms/10); LedSet(DEG6,m10ms%10); LedSet(6,0xB4); } } } if(cms==1000) cms=0; if(GetKey()!=0) return; } //时间到 gms=0;cms=0; while(1) { if(gms!=0) { cms++; gms--; if(cms==1) { LedSet(DEG1,0); LedSet(DEG2,0); LedSet(DEG3,0); LedSet(DEG4,0); LedSet(DEG5,0); LedSet(DEG6,0); LedSet(6,0xB0); }elseif(cms==500) { LedSet(DEG1,20); LedSet(DEG2,20); LedSet(DEG3,20); LedSet(DEG4,20); LedSet(DEG5,20); LedSet(DEG6,20); LedSet(6,0xFF); }elseif(cms==1000) cms=0; if(cms==1) beep_en=1; elseif(cms==200) beep_en=0; elseif(cms==300) beep_en=1; elseif(cms==500) beep_en=0; if(GetKey()!=0) { beep_en=0; return; } } } } } }//-------------------------------------------------------------------------//日期//-------------------------------------------------------------------------void TaskDate(){ uchartmp,pos=0; uintcms=0; while(1) { LedSet(DEG1,tm_year/10); LedSet(DEG2,tm_year%10); LedSet(DEG3,tm_month/10); LedSet(DEG4,tm_month%10); LedSet(DEG5,tm_day/10); LedSet(DEG6,tm_day%10); LedSet(6,0x7A); tmp=GetKey(); if(tmp==0x01) return; elseif(tmp==0x02) { //dateset pos=1;//year cms=0;gms=0; while(1) { if(gms!=0) { gms--; cms++; if(cms==1) { LedSet(DEG1,tm_year/10); LedSet(DEG2,tm_year%10); LedSet(DEG3,tm_month/10); LedSet(DEG4,tm_month%10); LedSet(DEG5,tm_day/10); LedSet(DEG6,tm_day%10); LedSet(6,0x7A); }elseif(cms==500) { if(pos==1) { LedSet(DEG1,20); LedSet(DEG2,20); }elseif(pos==2) { LedSet(DEG3,20); LedSet(DEG4,20); }elseif(pos==3) { LedSet(DEG5,20); LedSet(DEG6,20); } }elseif(cms==1000) cms=0; tmp=GetKey(); if(tmp==0x01) return; elseif(tmp==0x02) //nextpos { pos++; if(pos==4) { pos=0; break; } }elseif(tmp==0x04) //up { if(pos==1) //year { if(tm_year<99) tm_year++; else tm_year=1; }elseif(pos==2) //month { if(tm_month<12) tm_month++; else tm_month=1; }elseif(pos==3) { if(tm_day< GetMDays(tm_year,tm_month)) tm_day++; else tm_day=1; } }elseif(tmp==0x08) //down { if(pos==1) //year { if(tm_year>1) tm_year--; else tm_year=99; }elseif(pos==2) //month { if(tm_month>1) tm_month--; else tm_month=12; }elseif(pos==3) { if(tm_day>1) tm_day--; else tm_day=GetMDays(tm_year,tm_month); } } } } } }}//-------------------------------------------------------------------------//200msBi---//-------------------------------------------------------------------------voidBeep(){ beep_delay=20;}3.2闹钟版本/*******************************************************************************项目编号:CLK01名称:多功能时钟'tonkeys&轻风工作室提供'专业VB编程,毕业设计VB代编程,单片机程序设计。'QQ:58507961(技术支持)10+年编程经验'拍拍:/58507961'淘宝:日期:2021-8-25*******************************************************************************/#include <at89x51.h"//-------------------------------------------------------------------------//缩写宏定义//-------------------------------------------------------------------------#define ucharunsignedchar#defineuintunsignedint//数码管位置宏#defineDEG1 4#defineDEG2 5#defineDEG3 2#defineDEG4 3#defineDEG5 0#defineDEG6 1//-------------------------------------------------------------------------//函数声明//-------------------------------------------------------------------------uchar GetMDays(ucharcyear,ucharcmonth); //返回当前日期的月份天数void T0_init();void LedSet(ucharpos,ucharnum);uchar GetKey(void); //读取按键 void TaskSecond(); //秒表模式void TaskSetTime(); //时间设置模式 void TaskStopWatch();//倒计时void TaskSetAlarm(); //闹钟设置void TaskDate(); //日期uchar InKey(); //等待一个按键void Beep(); //200ms按键音//-------------------------------------------------------------------------//全局变量//-------------------------------------------------------------------------uchartm_year=13,tm_month=8,tm_day=1; //年月日uchartm_hour=10,tm_minute=15,tm_second=0; //时分秒uchartm_x10ms=0; //x10mSucharal_hour=10,al_minute=15,al=0; //时分秒uintgms;ucharmode=0;ucharbeep_delay=0;ucharLight_Set=7; //7--最亮20--暗ucharLight_Delay=60; //延时变暗ucharalarmtime=0;bitalarmsound=0;//-------------------------------------------------------------------------//主函数//-------------------------------------------------------------------------voidmain(){ T0_init(); while(1) { //显示时间 LedSet(DEG1,tm_hour/10); LedSet(DEG2,tm_hour%10); LedSet(DEG3,tm_minute/10); LedSet(DEG4,tm_minute%10); LedSet(DEG5,tm_second/10); LedSet(DEG6,tm_second%10); LedSet(6,0xE0); switch(GetKey()) { case0x01: //mode Beep(); TaskSecond(); //秒表模式 //TaskStopWatch(); //倒计时模式 TaskSetAlarm();//设置闹钟 TaskDate(); //日期模式 break; case0x02: //Set Beep(); TaskSetTime(); //进入时间设置模式 break; case0x04: //up break; case0x08: //down break; default: ; } }}//-------------------------------------------------------------------------//LED动态扫描//-------------------------------------------------------------------------ucharcodeled_code[]={3,207,42,74,198,82,18,203,2,66, ~0x7D,0,0,0,0,0,0,0,0,0,255,~0xC8,0xFE};//10-A.....21-L,22--uchar led_buf[8];uchar led_scan_step=0;voidLedScan(){ P1=0;//off if(led_scan_step<7) { P0=led_buf[led_scan_step]; P1=1<<led_scan_step; } led_scan_step++; if(led_scan_step>Light_Set)led_scan_step=0;}//-------------------------------------------------------------------------//定时器初始化 125uSfor12MhzOsc//-------------------------------------------------------------------------voidT0_init(){ TMOD=0X02; TH0=(256-125); TL0=(256-125); EA=1; ET0=1; TR0=1; }//-------------------------------------------------------------------------//定时中断//-------------------------------------------------------------------------ucharxcou=0;ucharms=0;bitbz_flag;bitbeep_en=0;voidtime0(void)interrupt1{ if(beep_en) { bz_flag=!bz_flag; P2_7=bz_flag; } xcou++; if(xcou==2) { LedScan(); //LED扫描 }elseif(xcou==8) { xcou=0; ms++; gms++; //临时计数用的 if(ms==10) { tm_x10ms++; ms=0; if(beep_delay!=0) { beep_delay--; if(beep_delay!=0) beep_en=1; else beep_en=0; } //闹钟声音 if(alarmsound) { if(tm_x10ms==1||tm_x10ms==30) beep_en=1; elseif(tm_x10ms==20||tm_x10ms==50) beep_en=0; } if(tm_x10ms==100) //秒 { tm_second++; //闹钟声音 if(alarmtime) { alarmtime--; if(alarmtime==0) { alarmsound=0; beep_en=0; } else alarmsound=1; } if(Light_Delay!=0) { Light_Delay--; if(Light_Delay==0) Light_Set=20; //变暗 else Light_Set=7; //亮度高 } tm_x10ms=0; if(tm_second==60)//分 { tm_minute++; tm_second=0; if(tm_minute==60)//时 { tm_hour++; tm_minute=0; if(tm_hour==24)//日 { tm_day++; tm_hour=0; if(tm_day>GetMDays(tm_year,tm_month)) //月 { tm_month++; tm_day=1; if(tm_month>12)//年 { tm_year++; tm_month=1; } } } } //闹钟检查 if(al==1) { if(al_hour==tm_hour) { if(al_minute==tm_minute) { alarmtime=60;//60秒闹钟 } } } } } } }}//-------------------------------------------------------------------------//返回当前日期的月份天数//-------------------------------------------------------------------------ucharcodedaylist[]={31,28,31,30,31,30,31,31,30,31,30,32};//平年各月天数ucharGetMDays(ucharcyear,ucharcmonth) { ucharrtn; rtn=daylist[cmonth-1]; if(cyear%4==0&&cmonth==2) //闰年2月 rtn=29; return(rtn); }//-------------------------------------------------------------------------//设置指定位置数码管的字符//-------------------------------------------------------------------------voidLedSet(ucharpos,ucharnum){ if(pos>=6) led_buf[pos]=num; else { led_buf[pos]=led_code[num]; }}//-------------------------------------------------------------------------//GetKey读取按键//-------------------------------------------------------------------------ucharGetKey(void){ ucharrtn=0; bitflag; flag=1; if(P3_4==0) rtn=0x01; //mode elseif(P3_5==0) rtn|=0x02; //set elseif(P3_6==0) rtn|=0x04; //up elseif(P3_7==0) rtn|=0x08; //down else flag=0; //pressed if(flag) { gms=0; while(P3_4==0||P3_5==0||P3_6==0||P3_7==0) { if(gms>1000)gms=1000; } if(gms<10) rtn=0;//抗抖过滤小于10ms的信号 else { Beep(); Light_Delay=60; } }else rtn=0; if(rtn!=0) alarmsound=0;//任意键取暖闹钟声音 return(rtn);}//-------------------------------------------------------------------------//秒表模式//-------------------------------------------------------------------------void TaskSecond(){ ucharhh=0,mm=0,ss=0,x10ms=0,ccms; uchartmp; while(1) { LedSet(DEG1,0); LedSet(DEG2,0); LedSet(DEG3,0); LedSet(DEG4,0); LedSet(DEG5,0); LedSet(DEG6,0); LedSet(6,0xD4); tmp=InKey(); //等待启动键 if(tmp==0x01) //modekeyexit { return; } elseif(tmp==0x04||tmp==0x08) //setkeystart { //运行中 gms=0; while(1) { if(gms!=0) { ccms++; gms--; if(ccms==10) { x10ms++; ccms=0; if(x10ms==100) { ss++; x10ms=0; if(ss==60) { ss=0; mm++; if(mm==60) { hh++; if(hh==100)hh=0; } } } }elseif(ccms==9) //刷新显示 { if(hh==0) { LedSet(DEG1,mm/10); LedSet(DEG2,mm%10); LedSet(DEG3,ss/10); LedSet(DEG4,ss%10); LedSet(DEG5,x10ms/10); LedSet(DEG6,x10ms%10); LedSet(6,0xD4); }else { LedSet(DEG1,hh/10); LedSet(DEG2,hh%10); LedSet(DEG3,mm/10); LedSet(DEG4,mm%10); LedSet(DEG5,ss/10); LedSet(DEG6,ss%10); LedSet(6,0xD0); } }else { switch(GetKey()) { case0x01: //mode退出 return; break; case0x02: //set暂停 while(1) { tmp=GetKey(); if(tmp==0x01) //modeexit return; elseif(tmp==0x04||tmp==0x08) //继续 break; elseif(tmp==0x02)//重置 { LedSet(DEG1,0); LedSet(DEG2,0); LedSet(DEG3,0); LedSet(DEG4,0); LedSet(DEG5,0); LedSet(DEG6,0); LedSet(6,0xD4); hh=0;mm=0;ss=0;x10ms=0;ccms =0; } } break; } } } } } } }//-------------------------------------------------------------------------//时间设置模式 //-------------------------------------------------------------------------void TaskSetTime(){ uchartmp,pos=0; uintcms=0; gms=0; while(1) { if(gms!=0) { gms--; cms++; tmp=GetKey(); if(tmp==0x01) return; elseif(tmp==0x02) { //set pos++; cms=0; if(pos==3) return; }elseif(tmp==0x04) //up { if(pos==0)//hh { tmp=tm_hour; tmp++; if(tmp>=24)tmp=0; tm_hour=tmp; }elseif(pos==1)//mm { tmp=tm_minute; tmp++; if(tmp>=60)tmp=0; tm_second=0; tm_minute=tmp; }elseif(pos==2) { tm_second=0; } cms=0; }elseif(tmp==0x08) //down { if(pos==0)//hh { tmp=tm_hour; if(tmp==0) tmp=23; else tmp--; tm_hour=tmp; }elseif(pos==1)//mm { tmp=tm_minute; if(tmp==0) tmp=59; else tmp--; tm_second=0; tm_minute=tmp; }elseif(pos==2) { tm_second=0; } cms=0; } if(cms==1) { LedSet(DEG1,tm_hour/10); LedSet(DEG2,tm_hour%10); LedSet(DEG3,tm_minute/10); LedSet(DEG4,tm_minute%10); LedSet(DEG5,tm_second/10); LedSet(DEG6,tm_second%10); LedSet(6,0xE0); }elseif(cms==500) { if(pos==0) { LedSet(DEG1,20); LedSet(DEG2,20); }elseif(pos==1) { LedSet(DEG3,20); LedSet(DEG4,20); }elseif(pos==2) { LedSet(DEG5,20); LedSet(DEG6,20); } }elseif(cms==1000) cms=0; } }} //-------------------------------------------------------------------------//等待一个按键 //-------------------------------------------------------------------------ucharInKey(void){ ucharrtn=0; while(rtn==0) { rtn=GetKey(); } return(rtn);}//-------------------------------------------------------------------------//倒计时//-------------------------------------------------------------------------void TaskStopWatch(){ uchartmp,pos=0; ucharhh=0,mm=0,ss=30,mms=0,m10ms=0; uintcms=0; while(1) { LedSet(DEG1,hh

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论