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

📄 电子钟.c

📁 用单片机制作的电子钟。程序编写用的是C语言。
💻 C
字号:
#include<reg51.h>
unsigned char hour=0,min=0,sec=0,t_hour=0,t_min=0,set=0,t_sec=0;
unsigned char deda=0;
unsigned int count;
bit flag0_5=0;
bit o_f=0;
sbit P23=P2^3;sbit P17=P1^7;sbit P07=P0^7;sbit P35=P3^5;sbit P27=P2^7;
sbit P20=P2^0;sbit P06=P0^6;sbit P25=P2^5;sbit P12=P1^2;sbit P21=P2^1;
sbit P22=P2^2;sbit P26=P2^6;sbit P30=P3^0;sbit P31=P3^1;sbit P32=P3^2;
sbit P36=P3^6;sbit P24=P2^4;sbit P05=P0^5;sbit P34=P3^4;sbit P04=P0^4;
void initial_time();
void Delay(unsigned int i);
void scankey();

void xianshi();
void runtime();
void c_xianshi();
void ctime_adj();
void clock();
unsigned char code seg[]={0x05,0xdd,0x46,0x54,0x9c,0x34,0x24,0x5d,0x04,0x14};


void initial_time()     //定时器初始化函数.
{
 TMOD=0x11;
TH1=(65536-4598)/256;
 TL1=(65536-4598)%256;             //定时5MS
 TH0=0xfe;              //定时5MS
 TL0=0xb0;
 TR1=1;
 TR0=0;
 EA=1;
 ET1=1;
 PT1=1;
 PT0=0;
 }

void Delay(unsigned int i)  //延时函数
{unsigned int j;
 for(;i>0;i--)
 {
  for(j=0;j<125;j++)
  {;}
 }
}
/**键盘扫描**/
void scankey()             
{
 Delay(1);
 if(P07==0){set++;ET0=1;TR0=1;Delay(10);ET0=0;TR0=0;}
 if(set==3) set=0;
 F:if(P07==0) goto F;
 }


/****走时显示*****/
void xianshi()                   
{unsigned char tem;
  tem=hour/10;
  P3=0x20;                       
  P1=seg[tem];
  Delay(1);
  P3=0x00;
  tem=hour%10;
  P3=0x80;
  P1=seg[tem];
  Delay(1);
  if(flag0_5==0) P12=0;else P12=1;Delay(1);
  P3=0x00;
  tem=min/10;
  P3=0x02;
  P1=seg[tem];
  Delay(1);
  P3=0x00;
  tem=min%10;
  P3=0x04;
  P1=seg[tem];
  Delay(1);
  if(flag0_5==0) P12=0;else P12=1;Delay(1);
  P3=0x00;
  tem=sec/10;
  P3=0x08;
  P1=seg[tem];
  Delay(1);
  P3=0x00;
  tem=sec%10;
  P3=0x10;
  P1=seg[tem];
  Delay(1);
   if(o_f==0)  P12=1;else P12=0; Delay(1); 
  P3=0x00;
     
 }
/*闹铃函数*/
void didi()
{
 if(t_hour==hour&&t_min==min)
 {
 if(o_f==1)
 {
  count++;
  if(count==50){ET0=1;TR0=1;}
  if(count==60){ET0=0;TR0=0;}
  if(count==75){ET0=1;TR0=1;}
  if(count==85){ET0=0;TR0=0;count=0;}
  
 }
  
}
}
/***走时函数****/
void runtime()               
{didi();
 xianshi();                                    
}


/**调整时间**/
void time_adj()                
{
 unsigned int i;
 if(P06==0)Delay(1);    
 if(P06==0){hour++;ET0=1;TR0=1;Delay(10);ET0=0;TR0=0;}      
 if(hour==24) hour=0;
for(i=0;i<10;i++)                   
{xianshi();
}                                  
 if(P05==0)Delay(1);    
 if(P05==0){min++; ET0=1;TR0=1;Delay(10);ET0=0;TR0=0;}   
 if(min==60) min=0;
for(i=0;i<10;i++)                   
{xianshi();
}
}
/**闹钟时间显示**/
void c_xianshi()          
{
 P3=0x20;
 P1=seg[t_hour/10];
 Delay(1);
 P3=0x00;
 P3=0x80;
 P1=seg[t_hour%10];
 Delay(1);
 P3=0x00;
 P3=0x02;
 P1=seg[t_min/10];
 Delay(1);
 P3=0x00;
 P3=0x04;
 P1=seg[t_min%10];
 Delay(1);
 P3=0x00;
 P3=0x08;
 P1=seg[0];
 Delay(1);
 P3=0x00;
 P3=0x10;
 P1=seg[0];
 Delay(1);
 P3=0x00;
}
/**调整闹钟时间.**/
void ctime_adj()           
{unsigned int i;
 if(P06==0)
 Delay(1);
 if(P06==0){t_hour++;ET0=1;TR0=1;Delay(10);ET0=0;TR0=0;}
 if(t_hour==24) t_hour=0;
 for(i=0;i<10;i++)
 {
  c_xianshi();
  }

 if(P05==0)
 Delay(1);
 if(P05==0){t_min++;ET0=1;TR0=1;Delay(10);ET0=0;TR0=0;}
if(t_min==60)t_min=0;
 for(i=0;i<10;i++)
 {
  c_xianshi();
  }
} 


/****主函数****/
void main()               
{initial_time();
while(1)
{ 
 if(P07==0) scankey();
 if(P04==0)
  {Delay(3);
   if(P04==0)
  {o_f=~o_f;ET0=1;TR0=1;Delay(10);ET0=0;TR0=0;}
  }
  while(P04==0);
 switch(set)
 {
  case 0:runtime();break;
  case 1:time_adj();break;
  case 2:ctime_adj();break;
 }
}
}
 
/**定时器0中断函数,获得时间.**/
void timer1() interrupt 3    
{
 TH1=(65536-4598)/256;
 TL1=(65536-4598)%256;
 deda++;
 if(deda<=100) flag0_5=0; else flag0_5=1;
 if(deda>=200) {sec++;deda=0;}        //注意::::deda不一定等于200.
 if(sec==60)   {min++;sec=0;}
 if(min==60)   {hour++;min=0;}
 if(hour==24)  {hour=0;}
} 
/*响铃中断*/
void timer0() interrupt 1
{
 TH0=0xfe;
 TL0=0xb0;
 P20=~P20;
}


⌨️ 快捷键说明

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