Demo entry 6686030

单片机定时闹钟

   

Submitted by anonymous on Dec 22, 2017 at 07:36
Language: C. Code size: 6.9 kB.

#include<reg51.h> 
#define uint unsigned int
#define uchar unsigned char 
sbit  key1=P1^0; 
sbit  key2=P1^1;  
sbit  key3=P1^2;
sbit  key4=P1^3;   
sbit lcden=P2^2;  
sbit lcdrs=P2^0;  
sbit lcdrw=P2^1;  
sbit jdp=P2^3;
sbit  led=P2^4;
sbit  sound=P2^5;    
int
a,b,i,min,hour,minge,minshi,hourge,hourshi,amin,ahour,aminge,aminshi,ahourge,ahourshi,sec,secshi,secge;
uchar code table1[]={'0','1','2','3','4','5','6','7','8','9'};
uchar code table2[]="Time:00:00";
uchar code Alarm_1[]="Alarm:00:00";
uchar code nu1[]="                ";
uchar num1,num2;
bit flag=0;
void delay(unsigned int xms)//延时程序
{
 uint i,j;  
 for(i=xms;i>0;i--)
     for(j=1;j>0;j--);
}
void write_com(uchar com)   //LCD命令控制,写指令函数
{
 delay(5);
 lcdrs=0;
 P0=com;  
 delay(5); 
 lcden=1;  
 delay(5); 
 lcden=0; 
}
void write_data(uchar date)     //LCD数据控制,写数据函数
{
 delay(5);  
 lcdrs=1;
 P0=date; 
 delay(5);  
 lcden=1;  
 delay(5);   
 lcden=0;
}
void time()    //设置时间函数
{
 while(1)
 {
  if(key1==0)
  {
   delay(10);   
   if(key1==0)
	{
	 if(hour==23)      //"时"设置
	  hour=0;
	 else
	  hour++;
	 hourge=hour%10;
	 hourshi=hour/10; 
	 write_com(0x0f);
	 delay(2);          
	 write_com(0x80+6);
	 write_data(table1[hourge]);  //"时"个位写入LCD第1行第6位
	 delay(5);      
	 write_com(0x80+5);      
	 delay(2);
	 write_data(table1[hourshi]);//"时"十位写入LCD第1行第5位
	 while(!key1);
	}
  }
  if(key2==0)              //闹钟"分"设置  
  {   
   delay(10);   
   if(key2==0)
   {     
    if(min==59)           
     min=0;          
    else
     min++;      
    minge=amin%10;     
    minshi=amin/10;     
    write_com(0x0f);     
    write_com(0x80+9);    
    write_data(table1[minge]);     
    delay(1);
    write_com(0x80+8);     
    write_data(table1[minshi]);     
    delay(1);    
    while(!key2);
   }
  }
  if(key3==0)   
  {     
   delay(10);    
   if(key3==0)    
   {         
    write_com(0x0c);
	TR1=1;
    while(!key3);
    break;                  //设置结束,此时LCD第二行无数据显示,只有第一行有时间显示
    }
   }
  }
}
void alarm()  //键盘扫描
{
 while(1)
 {
  if(key1==0)        
  {  
   delay(10);   
   if(key1==0)
   {
    if(ahour==23)
	  ahour=0;
	else
	  ahour++;
	ahourge=ahour%10;
	ahourshi=ahour/10;
	write_com(0x0f);
	write_com(0x80+0x40+8);
	write_data(':');
	write_com(0x80+0x40+7);
	write_data(table1[ahourge]);
	delay(1);
	write_com(0x80+0x40+6);
	write_data(table1[ahourshi]);
	delay(1);
	while(!key1);
	}
  }
  if(key2==0)
  {
  delay(10);
  if(key2==0)
  {
   if(amin==59)
    amin=0;
   else
    amin++;
   aminge=amin%10;    
   aminshi=amin/10;   
   write_com(0x0f); 
   write_com(0x80+0x40+10);  
   write_data(table1[aminge]);    
   delay(1);     
   write_com(0x80+0x40+9);
   //delay(2);    
   write_data(table1[aminshi]);
   delay(1);
   while(!key2);
   }
  }
  if(key3==0)
  {
   flag=~flag;
   delay(10);
   if(key3==0)
   {
    write_com(0x0c);
	write_com(0x80+0x40);
	for(i=0;i<11;i++)
	write_data(nu1[i]);
	while(!key3);
	break;
	}
   }
  }
}
void keyscan()
{
 if(key1==0)
 {
  delay(10);
  if(key1==0)
  {
   while(!key1);
   time();
  }
 }
 else if(key2==0)
 {
  delay(10);
  if(key2==0)
  {
   while(!key2)
   {
    ahourge=ahour%10;    
    ahourshi=ahour/10;   
    write_com(0x80+0x40+7);    
    write_data(table1[ahourge]);       
    delay(5);
    write_com(0x80+0x40+6);    
    delay(2);     
    write_data(table1[ahourshi]);
    aminge=amin%10;
    aminshi=amin/10;
    write_com(0x80+0x40+10);
    write_data(table1[aminge]);
    delay(5);
    write_com(0x80+0x40+9);
    delay(2);
    write_data(table1[aminshi]);
    write_com(0x80+0x40);
    for(i=0;i<11;++i)
    write_data(Alarm_1[i]);                //显示闹钟设置的 时间
    }
    write_com(0x80+0x40);     
    for(i=0;i<11;i++)
    write_data(nu1[i]);//key2松开后,LCD只有第一行显示时间,闹钟设置时间不显示
   }
  }
 else if(key3==0)    
 {   
  delay(10);  
  if(key3==0)
  {
   while(!key3);              //表示闹钟时间设置退出    
   ahourge=ahour%10;       
   ahourshi=ahour/10;
   write_com(0x80+0x40+7);    
   write_data(table1[ahourge]);    
   delay(5);     
   write_com(0x80+0x40+6);
   delay(2);     
   write_data(table1[ahourshi]);    
   aminge=amin%10;    
   aminshi=amin/10;    
   write_com(0x80+0x40+10);    
   write_data(table1[aminge]);
   delay(5);     
   write_com(0x80+0x40+9);    
   delay(2);     
   write_data(table1[aminshi]);    
   write_com(0x80+0x40);
   for(i=0;i<11;++i)    
   write_data(Alarm_1[i]);    
   alarm();
   }
  }
 else if(key4==0)    //以下进行闹铃开关的设置
	  delay(5);   
      if(key4==0)
	  {
	   while(!key4);    
	   a++;     
	   if(a%2==0)      //闹钟"on"
	   {
		for(b=0;b<6;b++)
		{
		 sound=~sound;       //蜂鸣器发出3次"哗"的响 声      
		 delay(200);
	    }
		sound=1;                     //蜂鸣器发出1次"哗 "的响声     
	   }
       else if(a%2==1)     //闹钟"off" 
	   {
		sound=0;                     //蜂鸣器发出1次"哗 "的响声     
		delay(300);     
		sound=1;         
		delay(300);
		}
		sound=1;
	 }
}
void lcd_init()  //LCD初始化
{
  lcden=0;  
  lcdrw=0;   
  write_com(0x38);   //设置8位格式,2行,5×7
  write_com(0x0c);   //整体显示,关光标,不闪烁  
  write_com(0x06);   //设定输入方式,增量不转移  
  write_com(0x01);   //清除屏幕显示
  delay(2);  
  write_com(0x80);
	for(i=0;i<16;i++) 
	{
	 write_data(table2[i]);  //写入显示时间数据     
	 delay(1);
	}
}
void judge()
{
 if(sec==60)
 {
  min++;   
  sec=0; 
 }
 if(min==60)
 {
  hour++;  
  min=0;
 }
 if(hour==24)    
 hour=0;
}
void init() //C51初始化函数
{
 key1=1;key2=1;key3=1;key4=1;sec=0;a=0;sound=1;amin=1;   
 TMOD=0x10;         //定时计数器T1工作在方式1   
 TL1=0xB0;
 TH1=0x3C;          //计数初值为15536  
 EA=1;  
 ET1=1;   
 TR1=1;
}
void equa()//music
{
 if(amin==min&&ahour==hour&&a%2==0)   //显示时间与闹钟设置时间完全一样且闹钟"on" 
 {
  jdp=0;
  sound=~sound;
 }
 else jdp=1;
}
void led1() interrupt 3  //LED1中断函数
{
 TL1=0xB0;  
 TH1=0x3C;  
 if(flag)  
 equa();     
 num1++;
 num2++;  
 if(num2>=20)  
 {
  sec++;  
  num2=0;  
  judge();
 }
 if(num1==10)  //计时10次50ms,即500ms
 {
  num1=0;   
  led=~led;
 }
}
void main()  //主函数
{
 lcd_init();  //LCD初始化函数  
 init();      //51初始化函数  
 while(1)
 {
  keyscan(); //调用扫描函数    
  minge=min%10;    
  minshi=min/10;    
  write_com(0x80+9);
  write_data(table1[minge]);    
  delay(5);     
  write_com(0x80+8);
  write_data(table1[minshi]);    
  hourge=hour%10;    
  hourshi=hour/10;    
  write_com(0x80+6);
  write_data(table1[hourge]);
  delay(5);     
  write_com(0x80+5);    
  write_data(table1[hourshi]);    
  write_com(0x80+7);
  write_data(':');    
  delay(5);     
  secge=sec%10;  //扩展秒表功能    
  secshi=sec/10;
  write_com(0x80+12);    
  write_data(table1[secge]);    
  delay(5);     
  write_com(0x80+11);    
  write_data(table1[secshi]);
  }
 } 

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).