基于51单片机的可由按键切换12\24小时制的电子时钟proteus仿真

仿真电路图:

部分源代码:

#include
#define uchar unsigned char 
#define uint unsigned int
sbit lcdrs=P3^5;  
sbit lcden=P3^4;
sbit s1=P3^0;   
sbit s2=P3^1;	
sbit s3=P3^2;	
sbit s4=P3^3;	
sbit beep=P2^2; 
sbit led=P2^3;  
uchar count,s1num,s4num;
char year,month,day,week,miao,shi,fen,pshi;
uchar code table[]=" 20  -  -   ";
void delay(uint z)  
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}
void zhuanhuan()
{
	if(shi==0)
	    pshi=12;
	if(shi<=12&&shi>=1)
	    pshi=shi;
    if(shi>12)
	    pshi=shi-12;					   
}					   					   
void di()
{
	beep=0;
	delay(300);
	beep=1;
	delay(300);
}
void write_com(uchar com)
{
	lcdrs=0;
	lcden=0;
	P0=com;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;	
}
void write_date(uchar date)
{
	lcdrs=1;
	lcden=0;
	P0=date;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;	
}
void _shi()	
{
   if(shi>=12)
   {
	 write_com(0x80+0x40+1);
	 write_date('P');
	 write_date('M');
   }
   else
   {
	 write_com(0x80+0x40+1);
	 write_date('A');
	 write_date('M');
   }
}
void write_ymd(uchar add1,uchar date1)
{
	uchar shi,ge;
	shi=date1/10;
	ge=date1%10;
	write_com(0x80+add1);
	write_date(0x30+shi);
	write_date(0x30+ge); 
}
void write_sfm(uchar add,uchar date)
{
	uchar shi,ge;
	shi=date/10;
	ge=date%10;
	write_com(0x80+0x40+add);
	write_date(0x30+shi);
	write_date(0x30+ge); 
}
void write_week(char week)	  
{
	write_com(0x80+12);
	switch(week)
	{
		case 1:	write_date('M');delay(5);
				write_date('O');delay(5);
				write_date('N');
				break;
		case 2:	write_date('T');delay(5);
				write_date('U');delay(5);
				write_date('E');
				break;
		case 3:	write_date('W');delay(5);
				write_date('E');delay(5);
				write_date('D');
				break;
		case 4:	write_date('T');delay(5);
				write_date('H');delay(5);
				write_date('U');
				break;
		case 5:	write_date('F');delay(5);
				write_date('R');delay(5);
				write_date('I');
				break;
		case 6:	write_date('S');delay(5);
				write_date('A');delay(5);
				write_date('T');
				break;
		case 7:	write_date('S');delay(5);
				write_date('U');delay(5);
				write_date('N');
				break;
	}
}
void writeym()
{
   day=1;
   month++;
   if(month==13)
   {
	 month=1;
	 year++;
	 if(year==100)
	    year=0;
	 write_ymd(3,year);
	}
	write_ymd(6,month);
}
void init()
{
    uchar num;
	lcden=0;
	year=13;
	month=6;
	day=21;
	week=5;
	shi=13; 
	fen=59;
	miao=58;
	count=0;
	s1num=0;
	s4num=0;
	write_com(0x38);
	write_com(0x0c);
	write_com(0x06);
	write_com(0x01);
	write_com(0x80);
	for(num=0;num<15;num++)
	{
		write_date(table[num]);
		delay(5);
	}
		delay(5);
		write_com(0x80+0x40+6);
		write_date(':');
		delay(5);
		write_com(0x80+0x40+9);
		write_date(':');
		delay(5);
	write_week(week);
	write_ymd(3,year);
	write_ymd(6,month);
	write_ymd(9,day);
	write_sfm(10,miao);
	write_sfm(7,fen);
	write_sfm(4,shi);
	TMOD=0x01;
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	EA=1;	
	ET0=1;	
	TR0=1;	
}
void keyscan()
{
	if(s1==0)	
	{
		delay(5);
		if(s1==0)
		{	
		    s1num++;
			while(!s1);	
			if(s1num==1)
			{
			    TR0=0;  
				write_com(0x80+3);
				write_com(0x0f); 
			}
			if(s1num==2)
			{
				write_com(0x80+6);
			}
			if(s1num==3)
			{
				write_com(0x80+9);
			}
			if(s1num==4)
			{
				write_com(0x80+12);
			}
			if(s1num==7)
			{
				write_com(0x80+0x40+10);
			}
			if(s1num==6)
			{
				write_com(0x80+0x40+7);
			}
			if(s1num==5)
			{
				write_com(0x80+0x40+4);
			}
			if(s1num==8)
			{
				s1num=0;
				write_com(0x0c);
				TR0=1;	
			}		
		}
	}
	if(s1num!=0)
	{
		if(s2==0)
		{
			delay(5);
			if(s2==0)
			{
				while(!s2);
				if(s1num==1)
				{
					year++; 
					if(year==100)
						year=0;
					write_ymd(3,year);
					write_com(0x80+3);
				}
				if(s1num==2)
				{
					month++;
					if(month==13)
						month=1;
					write_ymd(6,month);
					write_com(0x80+6);
				}
				if(s1num==3)
				{
					day++;
					if(year%4==0&&month==2)
					{
					    if(day==30)
						day=1;
					 }
					 if(year%4!=0&&month==2)
					{
					    if(day==29)
						day=1;
					 }
					 if(month!=2&&month!=4&&month!=6&&month!=9&&month!=11)
					 {
					    if(day==32)
						day=1;
					 }
					 if(month==4||month==6||month==9||month==11)
					 {	 
						if(day==31)
						day=1;
					 }
					write_ymd(9,day);
					write_com(0x80+9);
				}
				if(s1num==4)
				{
					week++;
					if(week==8)
						week=1;
					write_week(week);
					write_com(0x80+12);
				}
				if(s1num==7)
				{
					miao++; 
					if(miao==60)
						miao=0;
					write_sfm(10,miao);
					write_com(0x80+0x40+10);
				}
				if(s1num==6)
				{
					fen++;
					if(fen==60)
						fen=0;
					write_sfm(7,fen);
					write_com(0x80+0x40+7);
				}
				if(s1num==5)
				{					
					shi++;
					if(shi==24)
		            {
					   shi=0;
					}
					   if(s4num==0)
					   {					 					      
					      write_sfm(4,shi);
					      write_com(0x80+0x40+4);
					   }					   
					   if(s4num==1)
					   {
					   	  zhuanhuan();
					      _shi();
					      write_sfm(4,pshi);
					      write_com(0x80+0x40+4);
				       }					
		     	 }
			}
		}
	}
		if(s3==0)

链接:https://pan.baidu.com/s/1rjwJjcgsq29Moun2Z2BBzg
提取码:1234

你可能感兴趣的:(资料分享,51单片机,proteus,单片机)