⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zzp0.c

📁 单片机时间控制器
💻 C
字号:
#include "reg51.h"
#define  unchar  unsigned char
#define  PSEG    0x30   /*P.的代码*/
#define  N  60
unchar  code seg[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0x30,0xff,0x71,0x13};/*0-9和暗的代码*/
sbit    DISPCON=P3^2;/*显示控制*/
sbit    S1=P1^3;     /*设置键*/
sbit    S2=P1^2;     /*数字+1键*/
sbit    L1=P1^7;     /*L1,L2为秒指示*/
sbit    L2=P1^6;
sbit    S3=P1^5;     /*闹铃开设置键*/
sbit    S4=P1^4;     /*闹铃关设置键*/
sbit    light=P3^7;   /*闹铃控制*/

unchar  buf[4];      /*显示缓冲区*/
unchar  hourh;
unchar  hourl;
unchar  minuh;
unchar  minul;
unchar  second;

unchar  onhh;
unchar  onhl;
unchar  onmh;
unchar  onml;

unchar  offhh;
unchar  offhl;
unchar  offmh;
unchar  offml;

unchar  count;            /*溢出时钟计数器*/     
unchar  bdata setflag;    /*设置标志字节*/
unchar  bdata uflag;      /*设置通用字节*/
unchar  onset;       /*闹铃是否设置标志*/

sbit    hh=setflag^0;     /*设置当前设置的时间标志位*/
sbit    hl=setflag^1;
sbit    mh=setflag^2;
sbit    ml=setflag^3;
sbit    fresh=uflag^0;    /*显示刷新标志位*/
sbit    ss=uflag^1;       /*为闪烁标志*/
sbit    sec=uflag^2;      /*秒指示标志*/
sbit    onf=uflag^3;      /*闹铃标志*/
sbit    tfix=uflag^4;     /*修正指示标志*/

void system(void);        /*系统初始化函数*/
void t0_init(void);       /*T0初始化函数*/
void uart_init(void);     /*串行口传送初始化函数*/
void display(void);       /*显示函数*/
void delay20ms(void);
void settime(void);       /*时间设置函数*/
void ontime(void);        /*闹铃开设置函数*/
void offtime(void);       /*闹铃关设置函数*/
void adjust(void);        /*闹铃时间到判断函数*/
void t1_init(void);       /*T1初始化函数*/

/****************************************************/
/*                      以下为主函数                */
/****************************************************/
void main(void)
{
 system();
 t0_init();
 t1_init();
 uart_init();

 if(!S3)
   {
	   buf[0]=0x0b;
       buf[1]=0x0b;
       buf[2]=0x0d;
       buf[3]=0x0;
       display();
	   ontime();
	   onset=1;
	   tfix=0;
	   count=0;
		  
   }
 if(!S4)
 {
	   buf[0]=0x0b;
       buf[1]=0x0c;
       buf[2]=0x0c;
       buf[3]=0x0;
       display();
	   offtime();
 }

  buf[0]=0x0b;
  buf[1]=0x0b;
  buf[2]=0x0b;
  buf[3]=0x0a;
  display();
  settime();
  TR0=1;
  ET0=1;
  display();
  while(1)
  {
	  if(fresh)
	  {
	         buf[0]=minul;
             buf[1]=minuh;
             buf[2]=hourl;
             buf[3]=hourh;
	         display();
             fresh=0;
	  }
           else
           {
			 if(sec)
               {
			     sec=!sec;
	             L1=!L1;
		         L2=!L2;
			     if(onf)
				 {			       
					 light=1;
				 }
				 else
					 light=0;
		          delay20ms();
				}
		   }
  }

}

/****************************************************/
/*                  以下为显示函数                  */
/****************************************************/
void display(void)
{
	unchar out;
	DISPCON=1;
	out=seg[buf[0]];
	SBUF=out;
	while(!TI);
	TI=0;
	out=seg[buf[1]];
	out&=0xfe;
	SBUF=out;
	while(!TI);
	TI=0;
	out=seg[buf[2]];
	SBUF=out;
	while(!TI);
	TI=0;
	out=seg[buf[3]];
	SBUF=out;
	while(!TI);
	TI=0;
    DISPCON=0;
}
/****************************************************/
/*                    以下为时间走时函数            */
/****************************************************/
void timer0(void) interrupt 1 using 1
{
         unchar x;
         fresh=0;
	 sec=0; 
         TH0=0x3c;
	 TL0=0xb0;         
         count++;
	 if(tfix)
		 x=N;
	 else
		 x=10;
	 if(count!=x)
		 return;
	 else
	 {//⑤
		 tfix=0;
		 sec=1;
		 count=0;
		 second++;
		 if(second!=60)
			 return;
		 else
		 {//④
			 fresh=1;
			 second=0;
			 minul++;
			 if(minul!=10)
				{
				adjust();
				 return;
				 }
			 else
			 {//③
				 minul=0;
				 minuh++;
				 if(minuh!=6)
					{
					 adjust();
					 return;
					 }
				 else
				 {//②
					 minuh=0;
					 hourl++;
					 if(hourh==0||hourh==1)
					 {
						 if(hourl!=10)
						   {
						    adjust();
					            return;
						   }
					         else
					           {
						     hourl=0;
						     hourh++;
						     return;
					           }
					 }
					 else
					 {//①
						 if(hourl!=5)
							{ 
							 adjust();
						         return;
							 }
						 else
						 {
							 hourl=0;
							 hourh=0;
							 tfix=1;
							 return;
						 }
					  }//①
				  }//②
			   }//③
		   }//④
	 }//⑤
}

/****************************************************/
/*                   以下为闹铃判断函数             */
/****************************************************/
void adjust(void)
{
	if(!onset)
	  {
		onf=0;
	    return;
	   }
	else
	   {  
	     if(hourh==onhh&&hourl==onhl&&minuh==onmh&&minul==onml)
			onf=1;
		 else
			onf=0;
	       return;
	   }
 }
/****************************************************/
/*                 ;以下为T0初始化函数             */
/****************************************************/
void t0_init(void)
{
	TMOD=0x11;
	TH0=0x3c;
	TL0=0xb0;
}

/****************************************************/
/*                 以下为串行口传送初始化函数       */
/****************************************************/
void uart_init(void)
{
	SCON=0;

}
/****************************************************/
/*                以下为系统初始化函数              */
/****************************************************/
void system(void)
{
	IE=0;
    buf[0]=0;
    buf[1]=0;
    buf[2]=0;
    buf[3]=0;
    hourh=0;
    hourl=0;
    minuh=0;
    minul=0;
	second=0;
	count=0;
	setflag=0;
	uflag=0;
	EA=1;
	
}
/****************************************************/
/*                 以下为时间设置函数               */
/****************************************************/
void settime(void)
{
	unchar i;
	while(S1) {};
	for(i=0;i<4;i++)
		delay20ms();
	while(S1) {};
	setflag=1;
	ss=0;

	TR1=1;
	ET1=1;
	while(1)
	{
	    buf[0]=minul;
        buf[1]=minuh;
        buf[2]=hourl;
        buf[3]=hourh;
		if(ss)
		{
			switch(setflag)
			{
			case 0x01:buf[0]=0x0b;
				break;
			case 0x02:buf[1]=0x0b;
				break;
			case 0x04:buf[2]=0x0b;
				break;
			case 0x08:buf[3]=0x0b;
				break;

					
			}
		}
		display();
  sett_0: if(S1) goto sett_1;
		for(i=0;i<4;i++)
		delay20ms();
          if(S1) goto sett_1;
                				
		if(ml)
		{
			TR1=0;
			ET1=0;
			buf[0]=minul;
                        buf[1]=minuh;
                        buf[2]=hourl;
                        buf[3]=hourh;
			return;


		}
		setflag<<=1;
		continue;
sett_1: if(S2)  goto sett_0;
		for(i=0;i<4;i++)
		delay20ms();
	if(S2)  goto sett_0;
		if(!hh) goto sett_2;
		{
			hourh++;
			if(hourh==3)
				hourh=0;
		}
        continue;
sett_2:if(!hl)goto sett_3;
	   {
		   hourl++;
		   if(hourh==2) goto sett_20;
		   if(hourl==10) goto sett_21;
		   continue ;
   sett_21:hourl=0;
		   continue;
   sett_20:if(hourl==5) goto sett_22;
		   continue;
   sett_22:hourl=0;
		   continue;
	   }
sett_3:if(!mh) goto sett_4;
	   {
		   minuh++;
		   if(minuh==6) goto sett_30;
		   continue;
   sett_30:minuh=0;
		   continue;


	   }
sett_4:
       minul++;
	   if(minul==10)goto sett_40;
	   continue;
sett_40:minul=0;
	   continue;

    }
}

void ontime(void)
{
	unchar i;
	while(S1);
	for(i=0;i<4;i++)
		delay20ms();
	while(S1);
	setflag=1;
	ss=0;

	TR1=1;
	ET1=1;
	while(1)
	{
		buf[0]=onml;
        buf[1]=onmh;
        buf[2]=onhl;
        buf[3]=onhh;
		if(ss)
		{
			switch(setflag)
			{
			case 0x01:buf[0]=0x0b;
				break;
			case 0x02:buf[1]=0x0b;
				break;
			case 0x04:buf[2]=0x0b;
				break;
			case 0x08:buf[3]=0x0b;
				break;

					
			}
		}
		display();
 sett_0:	if(S1) goto sett_1;
		for(i=0;i<4;i++)
		delay20ms();
		if(S1) goto sett_1;
		if(ml)
		{
			TR1=0;
			ET1=0;
			buf[0]=onml;
            buf[1]=onmh;
            buf[2]=onhl;
            buf[3]=onhh;
			return;


		}
		setflag<<=1;
		continue;
sett_1: if(S2) goto sett_0;
		for(i=0;i<4;i++)
		delay20ms();
	if(S2) goto sett_0;
		if(!hh) goto sett_2;
		{
			onhh++;
			if(onhh!=3)
				continue;
			onhh=0;
			continue;
		}
sett_2:if(!hl)goto sett_3;
	   {
		   onhl++;
		   if(onhh==2) goto sett_20;
		   if(onhl==10) goto sett_21;
		   continue ;
   sett_21:onhl=0;
		   continue;
   sett_20:if(onhl==5) goto sett_22;
		   continue;
   sett_22:onhh=0;
		   continue;
	   }
sett_3:if(!mh) goto sett_4;
	   {
		   onmh++;
		   if(onmh==6) goto sett_30;
		   continue;
   sett_30:onmh=0;
		   continue;


	   }
sett_4:
      
	   onml++;
	   if(onml==10)goto sett_40;
	   continue;
sett_40:onml=0;
	   continue;

	 }
    
}
void offtime(void)
{
	unchar i;
	while(S1);
	for(i=0;i<4;i++)
		delay20ms();
	while(S1);
	setflag=1;
	ss=0;

	TR1=1;
	ET1=1;
	while(1)
	{
		buf[0]=offml;
        buf[1]=offmh;
        buf[2]=offhl;
        buf[3]=offhh;
		if(ss)
		{
			switch(setflag)
			{
			case 0x01:buf[0]=0x0b;
				break;
			case 0x02:buf[1]=0x0b;
				break;
			case 0x04:buf[2]=0x0b;
				break;
			case 0x08:buf[3]=0x0b;
				break;

					
			}
		}
		display();
 sett_0:	if(S1) goto sett_1;
		for(i=0;i<4;i++)
		delay20ms();
		if(S1) goto sett_1;
		if(ml)
		{
			TR1=0;
			ET1=0;
			buf[0]=offml;
            buf[1]=offmh;
            buf[2]=offhl;
            buf[3]=offhh;
			return;


		}
		setflag<<=1;
		continue;
sett_1: if(S2) goto sett_0;
		for(i=0;i<4;i++)
		delay20ms();
	if(S2) goto sett_0;
		if(!hh) goto sett_2;
		{
			offhh++;
			if(offhh!=3)
				continue;
			offhh=0;
			continue;
		}
sett_2:if(!hl)goto sett_3;
	   {
		   offhl++;
		   if(offhh==2) goto sett_20;
		   if(offhl==10) goto sett_21;
		   continue ;
   sett_21:offhl=0;
		   continue;
   sett_20:if(offhl==5) goto sett_22;
		   continue;
   sett_22:offhh=0;
		   continue;
	   }
sett_3:if(!mh) goto sett_4;
	   {
		   offmh++;
		   if(offmh==6) goto sett_30;
		   continue;
   sett_30:offmh=0;
		   continue;


	   }
sett_4:
      
	   offml++;
	   if(offml==10)goto sett_40;
	   continue;
sett_40:offml=0;
	   continue;

	 }
    
}

void delay20ms(void)
{
	unchar i,j;
	for(i=0;i<20;i++)
	   for(j=0;j<255;j++);
}
/****************************************************/
/*                秒指示闪烁函数                    */
/****************************************************/
void timer1(void)interrupt 3 using 1
{
	TH1=0x3c;
	TL1=0xb0;
	count++;
	if(count!=5)
		return;
	ss=!ss;
	count=0;

}
/****************************************************/
/*                以下为T1初始化函数                */
/****************************************************/
void t1_init(void)
{
	TMOD=0x11;
	TH1=0x3c;
	TL1=0xb0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -