Demo entry 6681950

C

   

Submitted by anonymous on Dec 07, 2017 at 07:37
Language: C++. Code size: 7.8 kB.

#include<reg52.h>
sbit rs=P3^5;
sbit lcden=P3^4;
sbit weila=P2^7;
sbit dula=P2^6;
sbit snum1=P3^0;//暂停光标闪烁,smove控制位置
sbit snum2=P3^1;//加1键
sbit snum3=P3^2;//减1键
sbit snum4=P3^3;//启动键
sbit beep=P2^3;
sbit rd=P3^7;
unsigned char smove,month=12,day=7;
int year=2017;
char sec=50,hour=23,min=59;week=3;//与数组时间相一致
unsigned char count; 
unsigned char code table[]=" 2017-12-07 TUR";
unsigned char code weeks[][3]={"MON","TUE","WEN","TUR","FRI","SAT","SUN"};//星期几的表单
unsigned char code table1[]="    23:59:50";
void delay(unsigned char z)
{
	unsigned char x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}
void write_com(unsigned char com)
{
	rs=0;
	lcden=0;
	P0=com;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;
}
void write_date(unsigned char date)
{
	rs=1;
	lcden=0;
	P0=date;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;
}
void write_sfm(unsigned char address,unsigned char date)//显示两位数字
{
	unsigned char shi,ge;
	shi=date/10;
	ge=date%10;
	write_com(0x80+address);
	write_date(0x30+shi);
  write_date(0x30+ge);
}
void write_sfm1(unsigned char address,int date)//用来显示年
{
	unsigned char qian,bai,shi,ge;
	qian=date/1000;
	bai=date/100%10;
	shi=date/10%10;
	ge=date%10;
	write_com(0x80+address);
	write_date(0x30+qian);
	write_date(0x30+bai);
	write_date(0x30+shi);
  write_date(0x30+ge);
}
/*用来显示周*/
void write_sfm2(unsigned char address,char week)
{
	char i;
	write_com(0x80+address);
	for(i=0;i<3;i++)
	 {
		 write_date(weeks[week][i]);
		 delay(5);
	 }
	}                                                                                                                 
void init()
{
	unsigned char i;
	lcden=0;
	weila=0;
	dula=0;
	TMOD=0x01;
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	EA=1;
	ET0=1;
	TR0=1;
	write_com(0x38);//不检测忙指令
	write_com(0x0c);//显示开但光标不闪烁
	write_com(0x06);//使显示光标不移动
	write_com(0x01);//显示清屏
	write_com(0x80);//第一行光标显示
  for(i=0;i<15;i++)
	{
		write_date(table[i]);
		delay(5);
	}
	write_com(0x80+0x40);//第二行光标显示
	for(i=0;i<12;i++)
	{
		write_date(table1[i]);
		delay(5);
	}
}
void keyscan()
{
	rd=0;
	if(snum1==0)	
	{
		delay(5);
		if(snum1==0)
		{	
			smove++;
			while(!snum1);
			if(smove==1)
			{
				TR0=0;
				write_com(0x80+0x40+11);//秒光标闪烁
				write_com(0x0f);
			}
		}
			if(smove==2)
			{
				write_com(0x80+0x40+8);//分光标闪烁
			}
			if(smove==3)
			{
				write_com(0x80+0x40+5);//时光标闪烁
			}
			if(smove==4)
			{
				write_com(0x80+10);//日光标闪烁
			}
      if(smove==5)
      {
				write_com(0x80+7);//月光标闪烁
			}	
      if(smove==6)
      {
				write_com(0x80+4);//年光标闪烁
			}	
      if(smove==7)
      {
				write_com(0x80+14);//周光标闪烁
			}
      if(smove==8)
      {
				smove=1;
				write_com(0x80+0x40+11);//返回到第一个地方
			}				
		}
		/*退出调节时间*/
		if(snum4==0)	
	{
		delay(5);
		if(snum4==0)
		{	
			while(!snum4);
			smove=0;
      if(smove==0)
			{
				write_com(0x0c);
				TR0=1;
			}				
		 }
	}
	/*以上结束*/
	if(smove!=0)
	{
		if(snum2==0)
		{
			delay(5);
			if(snum2==0)
			{
				while(!snum2);
				/*实现秒加1*/
				if(smove==1)
				{
					sec++;
					if(sec==60)
						sec=0;
					write_sfm(0x40+10,sec);
					write_com(0x80+0x40+11);
			  }
				/*实现分加1*/
				if(smove==2)
				{
					min++;
					if(min==60)
						min=0;
					write_sfm(0x40+7,min);
					write_com(0x80+0x40+8);
				}
				/*实现小时加1*/
				if(smove==3)
				{
					hour++;
					if(hour==24)
					   hour=0;
					write_sfm(0x40+4,hour);
					write_com(0x80+0x40+5);
				}
				/*实现日加1*/
				if(smove==4)
				{
					day++;
					if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
					{
						if(day>=32)
							day=1;
					}
					else if(month==4||month==6||month==9|month==11)
					{
						if(day>=31)
							day=1;
					}
					if((year%4==0)&&(year%100!=0)||year%400==0)
				{ 
					if(month==2)
	       if(day>=30)
	       { 
   	      day=1;
	       }
			  }
        else 
				{
          if(month==2)	
	          if(day>=29)
	          {
		         day=1;
	          }
				}
					write_sfm(9,day);
					write_com(0x80+10);
				}
				/*实现月加1*/
				if(smove==5)
				{   
					month++;	
					if(month==13)
					   month=1;
					write_sfm(6,month);
					write_com(0x80+7);
				}
				/*实现年加1*/
				if(smove==6)
				{
					year++;
					if(year==9999)
					   year=0;
					write_sfm1(1,year);
					write_com(0x80+4);
				}                            
        /*实现周加1*/
        if(smove==7)
				{
					week++;
					if(week==7)
					   week=0;
					write_sfm2(12,week);
					write_com(0x80+14);
				}			                           
		   }
	   }
		/*实现减功能*/
		if(snum3==0)
		{
			delay(5);
			if(snum3==0)
			{
				while(!snum3);
				/*实现秒减1*/
				if(smove==1)
				{
					sec--;
					if(sec<0)
						sec=59;
					write_sfm(0x40+10,sec);
					write_com(0x80+0x40+11);
				}
				/*实现分减1*/
				if(smove==2)
				{
					min--;
					if(min<0)
						min=59;
					write_sfm(0x40+7,min);
					write_com(0x80+0x40+8);
				}
				/*实现时减1*/
				if(smove==3)
				{
					hour--;
					if(hour<0)
						hour=23;
					write_sfm(0x40+4,hour);
					write_com(0x80+0x40+5);
				}
				/*实现日减1*/
				if(smove==4)
				{
					day--;
					if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
					{
						if(day<1)
							day=31;
					}
					else if(month==4||month==6||month==9|month==11)
					{
						if(day<1)
							day=30;
					}
					if((year%4==0)&&(year%100!=0)||year%400==0)
				{ 
					if(month==2)
	       if(day<1)
	       {  
   	      day=29;
	       }
			  }
        else 
				{
          if(month==2)	
	          if(day<1)
	          {
		         day=28;
	          }
				}
					write_sfm(9,day);
					write_com(0x80+10);
				}
				/*实现月减1*/
				if(smove==5)
				{
					month--;
					if(month<1)
						month=12;
					write_sfm(6,month);
					write_com(0x80+7);
				}
				/*实现年减1*/
				if(smove==6)
				{
					year--;
					if(year<0)
						year=9999;
					write_sfm1(1,year);
					write_com(0x80+4);
				}																
				/*实现周减1*/
				if(smove==7)
				{
					week--;
					if(week<0)
						week=6;
					write_sfm2(12,week);
					write_com(0x80+14);
				}   							 	 	 	 	 	 		 			
			}
		}
	 }
}
void main()
{
	init();
	while(1)
	{
		keyscan();
	}
}
void timer0() interrupt 1
{
	char i=0;
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	count++;
	if(count==18)
	{
		count=0;
		sec++;
	  if(sec==60)
	 {
		sec=0;
		min++;
		if(min==60)
		{
			min=0;
			hour++;
			beep=0;
		  for(i=0;i<10;i++)
			{
				delay(4000);
			}	
			beep=1;
			if(hour==24)
			{
				hour=0;
				week++;
				if(week==7)
					week=0;
			  write_sfm2(12,week);
				day++;
				if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        if(day>=32)
	      {
	        month++;
	        day=1;
	      }
        if(month==4||month==6||month==9|month==11)
        if(day>=31)
	      {
	        month++;
	        day=1;
	      } 
				if((year%4==0)&&(year%100!=0)||year%400==0)
				{ if(month==2)
	       if(day>=30)
	       {
	       	month++;   
   	      day=1;
	       }
			  }
        else 
				{
          if(month==2)	
	          if(day>=29)
	          {
	           month++;
		         day=1;
	          }
				}
           if(month==13)
           {
						 month=1; 
						 year++;
						 if(year==9999)
							 year=0;
						 write_sfm1(1,year);
					 }
        write_sfm(6,month);					 
				write_sfm(9,day);
			}
			write_sfm(0x40+4,hour);
			
		 }
		  write_sfm(0x40+7,min);
	  }
	    write_sfm(0x40+10,sec);
}
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).