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

📄 shizhong.c

📁 带温度显示的电子钟
💻 C
字号:
////////////////////////////////////////////////////////////////////////////////
#include <reg52.h>
#include <ds18b20.h>
                                 //共阴代码
unsigned char cathcode[21] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67,
                             0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xe7,0x40};
unsigned char nf[]={0x71,0x37,0x00};
unsigned char second,minute,hour;
unsigned int  c_second,c_minute,t;
unsigned char k,n,temp;
unsigned char flag_A=0,toolong=0,flash=0,th=0,tm=0,on_or_off; 

sbit buttona=P0^0;	            //定义各个为变量
sbit buttonb=P0^1;
sbit buttonc=P0^2;
sbit buttond=P0^3;
/*----------------------------------------------*/
void delay(unsigned char k)		//延时函数
{ 
 unsigned char j;
 while(k--)
   for (j=0;j<125;j++)
      ;
}

/*-------------------shansuoxianshi-------------*/
void flashdisplaytimer()
{
 if(flag_A==4)
    {
     if(++flash<8)
       { 
	  P1=0;
        P1=cathcode[th/10];
        P2=0;delay(1);P1=0;
        P1=cathcode[th%10+10];
	  P2=1;delay(1);
	 }
     }
 else
	 {
        P1=0;
        P1=cathcode[th/10];
        P2=0;delay(1);P1=0;
        P1=cathcode[th%10+10];
	  P2=1;delay(1);
	 }
 if(flag_A==5)
    {
     if(++flash<5)
       {
	  P1=cathcode[tm/10];
        P2=2;delay(1);P1=0;
        P1=cathcode[tm%10+10]; 
        P2=3;delay(1);P1=0;
	 }
    }
 else
       {
	  P1=cathcode[tm/10];
        P2=2;delay(1);P1=0;
        P1=cathcode[tm%10+10]; 
        P2=3;delay(1);P1=0;
	  }
 
   
     
          
	  P1=0x40;							   
        P2=4;delay(1);P1=0;


	  if(flag_A==6)
    {
     if(++flash<5)
       {
	  P1=0x3f;
	  P2=5;delay(1);P1=0;
	  P1=nf[on_or_off];
        P2=6;delay(1);P1=0;
	  if(on_or_off==1);
	  else
        {P1=nf[on_or_off]; 
         P2=7;delay(1);P1=0;}	          
	 }
    }
 else
       {
	  P1=0x3f;
	  P2=5;delay(1);P1=0;
	  P1=nf[on_or_off];
        P2=6;delay(1);P1=0;
        if(on_or_off==1);
	  else
        {P1=nf[on_or_off]; 
         P2=7;delay(1);P1=0;}		  
     	  }
 if(flash>20)flash=0;

}
 
/*-------------------shansuoxianshi-------------*/
void flashdisplay()
{
 if(flag_A==1)
    {
     if(++flash<8)
       { 
	  P1=0;
        P1=cathcode[hour/10];
        P2=0;delay(1);P1=0;
        P1=cathcode[hour%10+10];
	  P2=1;delay(1);
	 }
     }
 else
	 {
        P1=0;
        P1=cathcode[hour/10];
        P2=0;delay(1);P1=0;
        P1=cathcode[hour%10+10];
	  P2=1;delay(1);
	 }
 if(flag_A==2)
    {
     if(++flash<5)
       {
	  P1=cathcode[minute/10];
        P2=2;delay(1);P1=0;
        P1=cathcode[minute%10+10]; 
        P2=3;delay(1);P1=0;
	 }
    }
 else
       {
	  P1=cathcode[minute/10];
        P2=2;delay(1);P1=0;
        P1=cathcode[minute%10+10]; 
        P2=3;delay(1);P1=0;
	  }
 if(flag_A==3)
    {
     if(++flash<8)
       {   
	  P1=cathcode[second/10];							   
        P2=4;delay(1);P1=0;
        P1=cathcode[second%10+10];
        P2=5;delay(1);P1=0;
	 }
    }
 else
       {
	  P1=cathcode[second/10];							   
        P2=4;delay(1);P1=0;
        P1=cathcode[second%10+10];
        P2=5;delay(1);P1=0;
	 }
 if(tempflag==0)
    {P1=cathcode[temp/10];
     P2=6;delay(1);P1=0;}
 else
	{P1=cathcode[20];
	 P2=6;delay(1);P1=0;}
 P1=cathcode[temp%10];
 P2=7;
 delay(1);
 if(flash>20)flash=0;

}


/*------------------------------------------------------------------------------*/
void display()				//显示程序八位数码管。六位温度
{unsigned char i=5;	 		//两位显示温度。
 while(i--)
 {P1=0;
  P1=cathcode[hour/10];
  P2=0;delay(1);P1=0;
  P1=cathcode[hour%10+10];
  P2=1;delay(1);P1=0;
  P1=cathcode[minute/10];
  P2=2;delay(1);P1=0;
  P1=cathcode[minute%10+10]; 
  P2=3;delay(1);P1=0;
  P1=cathcode[second/10];							   
  P2=4;delay(1);P1=0;
  P1=cathcode[second%10+10];
  P2=5;delay(1);P1=0;
  if(tempflag==0)
    {P1=cathcode[temp/10];
    P2=6;delay(1);P1=0;}
  else
	{P1=cathcode[20];
	P2=6;delay(1);P1=0;}
  P1=cathcode[temp%10];
  P2=7;
  delay(1);				  
  
  }
}
/*-------------------------------------------------------------------------*/
/*--------------------------键盘扫描函数-------------*/
void scan_a()			              //scan button A
 {
  if(buttona==0)
     {delay(10);
     	if(buttona==0)
	   if(++flag_A>6)flag_A=0;
	   toolong=0;			 //if button A effective the flag clear 
     }
     while(!buttona);			 //wait a
 }

void scan_c()                            //scan key c
 {
  if(buttonc==0)
     {delay(10);
      if(buttonc==0)
	   {toolong=0;
          if(flag_A==1)
             {if(++hour>23)hour=0;}
          else
             {if(flag_A==2)
                {if(++minute>59)minute=0;}
              else
                {if(flag_A==3)second=0;
		     else
			  {if(flag_A==4)
			      {if(++th>23)th=0;}
			   else
				   {if(flag_A==5)
				       {if(++tm>59)tm=0;}
				    else  
				        on_or_off=1;
				    }
			    }
                }
	
		}
         }
	 }
  while(!buttonc);         
}                      
 
void scan_d()
{
  if(buttond==0)
     {delay(10);
      if(buttond==0)
	     {toolong=0;
          if(flag_A==1)
             {if(--hour>100)hour=23;}
          else
             {if(flag_A==2)
                {if(--minute>100)minute=59;}
              else
                if(flag_A==3)second=30;
			 else
			  {if(flag_A==4)
			      {if(--th>100)th=23;}
			   else
				   {if(flag_A==5)
				       {if(--th>100)tm=59;}
				    else  
				        on_or_off=0;
				    }
			    }



              }
	   
		}
	 }
   while(!buttond);	
}			         
/*-------------------------------------------*/

void main ()   //主函数
{
  TMOD=0x02;
  TH0=0x06;
  TL0=0x06;
  TR0=1;
  ET0=1;
  EA=1;		   //启动定时器,方式2.开定时器中断
  n=0;
  c_second=0;
  c_minute=0;			   
  hour=23;
  minute=59;
  second=30;
               //开机显示23:59:30 

while(1)
{
     scan_a();
	 if(flag_A)
	  {scan_c();
	   scan_d();}
    if(toolong>30)flag_A=0; 
      
  
    if(flag_A==0)	
	  display();
    else
      if(flag_A<=3)
         flashdisplay();
	else
	   flashdisplaytimer();
    if(toolong>30)flag_A=0; 
    					 //温度每秒采样一次
         
         t=ReadTemperature();
	     temp=(int)(t)  ;
	   
	//if(temp>99)temp%=100;
	//if(temp<1)temp=0;
} 

}
 
void t0(void) interrupt 1 using 1		//中断函数
{ 
  c_second++;
  c_minute++;
  if(c_second==8000)
     {
	  c_second=0;
	  second++;
      toolong=0;       //调整时间标志位
	  if(second==60)
	     {minute++;
	     if(minute==60)
	      {hour++;
		    if(hour>23) 
	    	 {
			  hour=0;
		     }
		  minute=0;
		  }
		 second=0;
		 } 
	  }
}


							

⌨️ 快捷键说明

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