基于C51单片机的定时闹钟(2)

基于C51单片机的定时闹钟

上电后设置定时时间,按键1选择设置的是小时分钟还是秒钟
按键2对其进行具体的数字设置
一次选择完成之后就默认进入计时模式
达到计时时间后响铃
按键3可以关闭响铃
基于C51单片机的定时闹钟(2)_第1张图片
基于C51单片机的定时闹钟(2)_第2张图片

代码

#include
#include
#include
#include
#define uint unsigned int
#define uchar unsigned char

uint num,A_num,x=0;
uint h,m,s;
uint h1=0,m1=0,s1=0,sign=1; 

uchar code Zifu[]="0123456789";
 
sbit beep = P2^3;

sbit LCD_EN = P3^4;
sbit LCD_RS = P3^5;

sbit duan = P2^6;
sbit wei = P2^7;

sbit key_A = P3^6;
sbit key_B = P3^7;

void delay_1ms(uchar x){
     
	uchar i,j;
	for(j=0;j<x;j++)
		for(i=0;i<110;i++);
}

void write_command(uchar command){
     
	LCD_RS = 0;
	LCD_EN = 0;
	P0 = command;
	delay_1ms(2);
	LCD_EN = 1;	 		//EN ÓÉ1 -- 0 Íê³É ÓÐЧÊý¾Ý µÄ Ëø´æ
	delay_1ms(2);
	LCD_EN = 0;
}
 
void write_data(uchar yes){
     
	LCD_RS = 1;
	LCD_EN = 0;
	P0 = yes;
	delay_1ms(2);
	LCD_EN = 1;
	delay_1ms(2);
	LCD_EN = 0;
}
 
void lcd_post(int X,int Y){
     			//0,1±íÕ÷ µÚÒ»ÐÐ ºÍ µÚ¶þÐÐ
	write_command(0x80+X*(0x40)+Y);
}
 
void init(){
     
	h=m=s=0;
	num=A_num=0;
 
	LCD_EN=0;
	write_command(0x38);
	write_command(0x0c);
	//write_command(0x06); //д×Ö·ûºóµØÖ·Ö¸Õë¼ÓÒ»
	write_command(0x01);
 
	TMOD = 0x02;
	TH0 = 6;
	TL0 = 6;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
 
/*ʱ·ÖÃë*/	
	lcd_post(0,0); write_data(Zifu[h/10]);
	lcd_post(0,1); write_data(Zifu[h%10]);
 
	lcd_post(0,2); write_data(':');
 
	lcd_post(0,3); write_data(Zifu[m/10]);
	lcd_post(0,4); write_data(Zifu[m%10]);
 
	lcd_post(0,5); write_data(':');
 
	lcd_post(0,6); write_data(Zifu[s/10]);
	lcd_post(0,7); write_data(Zifu[s%10]);
 

	lcd_post(1,5); write_data('a');
	lcd_post(1,6); write_data('l');
	lcd_post(1,7); write_data('a');
	lcd_post(1,8); write_data('r');
	lcd_post(1,9); write_data('m');
 
	lcd_post(1,10); write_data(' ');
	lcd_post(1,11); write_data('c');
 
	lcd_post(1,12); write_data('l');
 
	lcd_post(1,13); write_data('o');
	lcd_post(1,14); write_data('c');
	lcd_post(1,15); write_data('k');
}
 


void ling()
{
     
	unsigned char a;
	for(a=0;a<500;a++)
	{
     
		
	if((P1&0x80)==0)
	{
     
	 break;
	}
	
	lcd_post(1,5); write_data('l');
	lcd_post(1,6); write_data('i');
	lcd_post(1,7); write_data('n');
	lcd_post(1,8); write_data('g');
	lcd_post(1,9); write_data('!');
	lcd_post(1,10); write_data(' ');
	lcd_post(1,11); write_data('l');
	lcd_post(1,12); write_data('i');
	lcd_post(1,13); write_data('n');
	lcd_post(1,14); write_data('g');
	lcd_post(1,15); write_data('!');
	beep=0;  
	
	delay_1ms(500);
	lcd_post(1,5); write_data(' ');
	lcd_post(1,6); write_data(' ');
	lcd_post(1,7); write_data(' ');
	lcd_post(1,8); write_data(' ');
	lcd_post(1,9); write_data(' ');
	lcd_post(1,10); write_data(' ');
	lcd_post(1,11); write_data(' ');
	lcd_post(1,12); write_data(' ');
	lcd_post(1,13); write_data(' ');
	lcd_post(1,14); write_data(' ');
	lcd_post(1,15); write_data(' ');
	beep=1;
	
	delay_1ms(500);
	}
	
	
	lcd_post(1,5); write_data('a');
	lcd_post(1,6); write_data('l');
	lcd_post(1,7); write_data('a');
	lcd_post(1,8); write_data('r');
	lcd_post(1,9); write_data('m');
	lcd_post(1,10); write_data(' ');
	lcd_post(1,11); write_data('c');
	lcd_post(1,12); write_data('l');
	lcd_post(1,13); write_data('o');
	lcd_post(1,14); write_data('c');
	lcd_post(1,15); write_data('k');

}

void keyscan(){
     
	if(key_A==0){
     
		delay_1ms(3);
		if(key_A==0){
     
			A_num++;	
			switch(A_num){
     
				case 1:							  //ʱ
					TR0=0;						  
					lcd_post(0,1);
					write_command(0x0f);
					break;
				case 2:lcd_post(0,4);break;		  //·Ö
				case 3:lcd_post(0,7);break;		  //Ãë
				
				case 4:
					A_num=0;
					write_command(0x0c);
					TR0=1;
				  sign = 0;
					break;
			}		
		}
		while(!key_A);
	}
	if(A_num!=0){
     
		if(key_B==0){
     
			delay_1ms(3);
			if(key_B==0){
     	
				
				switch(A_num){
     
				case 1:
					h1=(++h1)%24;
					lcd_post(0,0); write_data(Zifu[h1/10]);
					lcd_post(0,1); write_data(Zifu[h1%10]);
					break;		  //ʱ
				case 2:
					m1=(++m1)%60;
					lcd_post(0,3); write_data(Zifu[m1/10]);
					lcd_post(0,4); write_data(Zifu[m1%10]);
					break;		  //·Ö
				case 3:
					s1=(++s1)%60;
					lcd_post(0,6); write_data(Zifu[s1/10]);
					lcd_post(0,7); write_data(Zifu[s1%10]);
					break;		  //Ãë
				case 4:sign = 0;break;
				}write_command(0x10);		
			}
			while(!key_B);
		}
	}
	
}
 
void main()
{
      
	
  int clk1 =0;
	int clk2 =0;
  there:
	beep=0;
	init();
	//there:
	
	
	 h1=m1=s1=0;
	while(sign)
	{
     
		keyscan();
	}
	
	clk1 = 0;
  clk2 = h1*3600+m1*60+s1;
	init();
	while(1){
     
		  
		if(num==3686){
     
			num=0;
			  s++;
   			clk1++;
				if(clk1 == clk2)
				{
     
				  ling();
					sign = 1;
					goto there;
					
				}
			if(s==60){
     
				s=0;
				m++;
				
				if(m==60){
     
					m=0;
					h++;
					if(h==24)h=0;
					lcd_post(0,0); write_data(Zifu[h/10]);
					lcd_post(0,1); write_data(Zifu[h%10]);
				}
				  lcd_post(0,3); write_data(Zifu[m/10]);
			   	lcd_post(0,4); write_data(Zifu[m%10]);
		  	}
		    	lcd_post(0,6); write_data(Zifu[s/10]);
			    lcd_post(0,7); write_data(Zifu[s%10]);
	    	}
		
	      }
}
 
void T0_time() interrupt 1
{
     
	num++;
}

你可能感兴趣的:(专业课从踩坑到住在坑里,单片机)