单片机系统作息时间控制器程序#includereg52.h#includeintrins.h#defineTHCO0xee#defineTLCO0x0#defineu8unsignedcharunsignedcharcodeDuan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//共阳极数码管,0-9段码表unsignedcharData_Buffer[6]={0,0,0,0,0,0};//6位数码管unsignedcharHour=6,Min=49,Sec=50;//实时时间unsignedcharN_Hour=6,N_Min=50;//闹钟时间unsignedcharM_Min=0,M_Sec=0;M_Hour=0;//秒表时间unsignedcharRing_time=5;//响铃长度unsignedcharMode=0;//按键次数//MODE,0:实时时钟;1:修改小时;2:修改分钟,3:修改闹钟小时,4:修改闹钟分钟,5:启停闹钟u8Tim[][2]={{6,50},{7,0},{7,25},{8,20},{8,30},{9,15},{9,25},{10,10},{10,30},{11,15},{11,25},{12,10},{13,20},{13,30},{14,15},{14,25},{15,10},{16,30},{17,0},{17,50},{18,0},{21,30},{23,0}};bitZ_Ring=0;//整点报时标志bitN_Ring=0;//启动闹铃标志,1:启动bitBrush_flag=0;//更新数码管标志sbitP10=P1^0;//四个数码管的位码口定义sbitP11=P1^1;sbitP12=P1^2;sbitP13=P1^3;sbitP14=P1^4;sbitP15=P1^5;bitqq=0;sbitK_Mode=P2^0;//按键定义sbitK_UP=P2^1;sbitK_DN=P2^2;sbitK_Net=P3^2;sbitLED1=P2^3;//四个指示灯sbitLED2=P2^4;sbitLED3=P2^5;sbitLED4=P2^6;sbitClock_Ring=P3^0;//蜂鸣器的位码口定义staticunsignedcharBit=0;//静态变量,退出程序后,值保留staticunsignedcharcount=0,K_count=0,M_count;staticunsignedchartime_n=0;voidmiaobiao();//秒表函数staticunsignedcharMB=0;//秒表开启标志staticunsignedcharMBFW=0;//秒表复位voidring_play();//声音播放函数voidring_delay(unsignedcharx);//声音延时voidZ_ring();//整点报时函数voidZring();//整点报时/*************************************************************/voidring_delay(unsignedcharn)//声音延时125*n毫秒{unsignedchari=125,j;do{do{for(j=0;j230;j++)_nop_();}while(--i);}while(--n);}/*************************************************************/voidring_play()//声音播放{Clock_Ring=0;ring_delay(1);Clock_Ring=1;}/*************************************************************/voidZring()//整点报时{if(Z_Ring==1)//整点报时标志位{ring_delay(4);ring_play();//播放音乐一次Z_Ring=0;}}/*************************************************************/voidring()//闹钟{inti=0;for(i=0;i=23;i++){if(Tim[i][0]==Hour&&Tim[i][1]==Min&&Sec=10)//闹钟相等N_Ring=1;//biaozhiwei}if(N_Ring==1)//闹铃启动{ring_delay(4);ring_play();//播放音乐一次N_Ring=0;}}/*************************************************************/voidlook()//数码管显示{if(Brush_flag==1)//更新数码管显示{Brush_flag=0;if(Mode=2)//实时时间{Data_Buffer[0]=Hour/10;Data_Buffer[1]=Hour%10;Data_Buffer[2]=Min/10;Data_Buffer[3]=Min%10;Data_Buffer[4]=Sec/10;Data_Buffer[5]=Sec%10;}elseif(Mode=4)//闹钟时间{Data_Buffer[0]=N_Hour/10;Data_Buffer[1]=N_Hour%10;Data_Buffer[2]=N_Min/10;Data_Buffer[3]=N_Min%10;Data_Buffer[4]=M_Sec/10;Data_Buffer[5]=M_Sec%10;}elseif(Mode=5)//秒表时间{Data_Buffer[0]=M_Hour/10;Data_Buffer[1]=M_Hour%10;Data_Buffer[2]=M_Min/10;Data_Buffer[3]=M_Min%10;Data_Buffer[4]=M_Sec/10;Data_Buffer[5]=M_Sec%10;}}}/*************************************************************/voidmain()//主函数{TMOD=0x11;//定时器0初始化TH0=THCO;TL0=TLCO;Clock_Ring=1;TR0=1;ET0=1;EA=1;//开中断TR1=0;ET1=1;while(1){Zring();ring();look();if(Mode==5){miaobiao();}}}/*************************************************************/voidjisuan()//时间计算{count++;if(count=200)//秒计时,定时器定时5ms,计200次为一秒{Brush_flag=1;//更新数码管显示count=0;Sec++;if(Sec=60){Sec=0;Min++;if(Min=60)//时间计时{Min=0;Hour++;if(Hour=24)Hour=0;}}}}/*************************************************************/voiddel()//显示位操作与小数点处理{Bit++;if(Bit=6)Bit=0;P1|=0xff;//先关位码P0=Duan[Data_Buffer[Bit]];//开段码if(count100&&Bit==1)P0|=0x80;//0.5S中间小数点亮,之后灭,不断循环if(count100&&Bit==3)P0|=0x80;switch(Bit)//送位码{case0:P10=0;break;case1:P11=0;break;case2:P12=0;break;case3:P13=0;break;case4:P14=0;break;case5:P15=0;break;}}/*************************************************************/voidkey()//键盘处理{if(K_Mode==0||K_UP==0||K_DN==0||K_Net==0)//有键按下{K_count++;if(K_count=30)//消抖处理{if(N_Ring==1)//闹铃启动{N_Ring=0;return;}K_count=0;Brush_flag=1;if(K_Mode==0)//修改Mode,{Mode++;if(Mode=6)Mode=0;if(Mode==0){LED1=1;LED2=1;LED3=1;LED4=1;}//实时时间elseif(Mode==1){LED1=0;}//修改小时elseif(Mode==2){LED1=1;LED2=0;}//修改分钟elseif(Mode==3){LED2=1;LED3=0;}//修改小时elseif(Mode==4){LED3=1;LED4=0;}//修改分钟elseif(Mode==5){LED4=0;LED3=0;}//秒表}elseif(K_UP==0){if(Mode==1)//实时时间小时加{Hour++;if(Hour=24)Hour=0;}elseif(Mode==2)//实时时间分钟加{Min++;if(Min=60)Min=0;}elseif(Mode==3)//闹钟时间小时加{N_Hour++;if(N_Hour=24)N_Hour=0;}elseif(Mode==4)//闹钟时间分钟加{N_Min++;if(N_Min=60)N_Min=0;}elseif(Mode==5){MBFW=1;}}elseif(K_DN==0){if(Mode==1)//小时减{if(Hour==0)Hour=23;elseHour--;}elseif(Mode==2)//分钟减{if(Min==0)Min=59;elseMin--;}elseif(Mode==3)//小时减{if(N_Hour==0)N_Hour=23;elseN_Hour--;}elseif(Mode==4)//分钟减{if(N_Min==0)N_Min=59;elseN_Min--;}elseif(Mode==5){MB=!MB;}}if(Mode==3||Mode==4){Tim[qq][0]=N_Hour;Tim[qq][1]=N_Min;}}}elseK_count=0;}/*************************************************************/voidtimer0()interrupt1//中断{TH0=THCO;TL0=TLCO;jisuan();del();key();}/*************************************************************/voidmiaobiao()//秒表函数{if(MBFW==1){M_count=0;M_Sec=0;Brush_flag=1;M_Min=0;Brush_flag=1;M_Hour=0;Brush_flag=1;MBFW=0;}else{if(MB==1){M_count++;if(M_count=20){M_count=0;M_Sec++;Brush_flag=1;if(M_Sec=100){M_Sec=0;M_Min++;Brush_flag=1;//更新数码管显示}if(M_Min=60){M_Min=0;M_Hour++;Brush_flag=1;if(M_Hour=60){M_Hour=0;Brush_flag=1;M_Min=0;Brush