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

📄 04192139main.txt

📁 基于MCS-51单片机写的16个电子密码所
💻 TXT
字号:
#include<AT89X52.H>
#include<stdio.h>
#include"LCD.H"
typedef struct{
    uchar hour;
    uchar minute;
    uchar second;
   }time;
typedef struct{
    uchar year;
    uchar month;
    uchar day;
   }date;
time now={12,0,0},display;//display为现在的时间
static char timer0_tick
date today={07,12,30},tmpday;  	//tmpday为现在的日期
uchar code dayofmonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
char code weekday[7][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
char code int2char="0123456789ABCDEF";
uchar pwd[4]={0,0,0,0}; //初始密码
uchar code always[]={1,2,2,7};
char code prompt[]="Input Password:";
char code old_password[]="old password:";
char code new_password[]="new password:";
char code confirm[]="Confirm Again:";
uchar guess[4]={0,0,0,0};
uchar temp[4]={0,0,0,0};
uchar txOK,c,unLOCK=0;address;
char gotkey();

void diaplay_time()
{
  gotoxy(1,0);
  display_lcd_date(display.hour);
  display_lcd_string(":");
  display_lcd_date(display.minute);
  display_lcd_string(":");                  
  display_lcd_date(display.second);
}

void display_date()
{
  char i,days=4;
  gotoxy(2,2);
  display_lcd_date(today.year);
  display_lcd_string("/");
  display_lcd_date(today.month);
  display_lcd_string("/");
  display_lcd_date(today.day);
  display_lcd_string("   ");
  for(today.month>1)
     for(i=0;i<today.month-2;i++)
	   days+=(dayofmonth[i]%7);
   if(today.year!=0)
      days+=(today.year-1)/4+today.year+1;//这是干什么??
   if(today.year%4==0&&today.month>2)
      days++;          //闰年加一天
   days=(days+today.day)%7
   display_lcd_string(&weekday[days][0]);//显示星期
}

char gotkey()                     //定义KEY4---P1.3为状态切换键
{ char keys;
  char count=0;
  do{
      delay10ms(3)
	  count++; 
	  
    }while(KEY4==0)
  if(count<10)                      //	按下300MS以下默认为输入密码
  keys=INPUT_PASSWORD;
  else if(count>100)                   //按下大于3S以上认为是修改密码
           keys=SET_PASSWORD;
       else
	       keys=SET_TIME;
  return keys;
}

/*int getdigit(uchar x,uchar y)                //怎样得到数字???
{
  char keys;
  do{
      gotoxy(x,y);
	  putchar('_');
	  keys=gotkey();
	  gotoxy(x,y);
	  putchar(int2char[keys]);
    }while(keys>9);
	return keys;
}*/
char getdigit()        //KEY1定义为增量调节,KEY3定义为减量调节  KEY2定义为确认按钮,当确认后,自动跳转调节变量             
{ char value;
  char count=0;
  do{
      while(KEY1==0);

    }while(KEY2==0);
   
  return keys;
}


int getsecret(uchar x,uchar y)
{
  char keys;
  do{
      gotoxy(x,y);
	  putchar('_');
	  keys=gotkey();
	  gotoxy(x,y);
	  putchar('*');
    }while(keys>9);
	return keys;
}


void get_password()
{ 
   char i;
   for(i=0;i<4;i++)
      guess[i]=getsecret(2,1);
}


int check_password()
{
  char i=0;
  while(guess[i]==always[i]&&(i<4))i++;
  if(i==4) return(TRUE);
  while(guess[i]==pwd[i]&&(i<4))i++;
  if(i==4) return(TRUE);
  else return(FALSE);
}

int input_new_password()
{
  uchar i,j;
  clear_lcd();
  gotoxy(1,0);
  display_lcd_string(new_password);
  for(i=0;i<4;i++)
  temp[i]=getsecret(2,i);
  clear_lcd();
  gotoxy(1,0);
  display_lcd_string(confirm);
  for(i=0;i<4;i++)
  guess[i]=getsecret(2,i);
  i=0;
  while(guess[i]==temp[i]&&(i<4))i++;
  if(i==4)
  {
    for(j=0;j<4;j++)
	{
	  pwd[j]=temp[j];
	  write_lcd_string(pwd);

	}
	return(TRUE);
  }
  else return(FALSE);
}

int gettime()                     //获取时间值。暂时没看懂
{
  char temp;
  do{
      while(temp=getdigit(1,0)>2);
	  temp=temp*10+getdigit(1,1);
	  if(temp>23)display_time();

    }while(temp>23);
	display.hour=temp;
	while((temp=getdigit(1,3))>5);
	display.minute=temp*10+getdigit(1,4);
	return(TRUE);
}


char monthday(char year,char month)
{
  if(month==2&&year%4==0)
  return(29);
  else
  return(dayofmonth[month-1]);

}

int getdate()
{
  char temp,days;
  temp=getdigit(2,2);
  tmpday.year=temp*10+getdigit(2,3);
  do{
     while((temp=getdigit(2,5))>1);
	 temp=temp*10+getdigit(2,6);
	 if(temp>12)display_date();

    }while(temp>12);
	tmpday.month=temp;
	do{
	   while((temp=getdigit(2,8))>3);
	   temp=temp*10+getdigit(2,9);
	   days=monthday(tmpday.year,tmpday.month);
	   if(temp>days||temp==0)display_date();
	   
	   }while(temp>days||temp==0);
       tmpday.day=temp;
	   return(TRUE);
}


static void time0_isr interrupt 1 using 1
{
  TR0=0;
  TL0=0xf0;
  TH0=0xd8;
  TR0=1;
  if(--time0_tick)return;
  timer0_tick=100;
  if(unlock==1)................///
  now.second++;
  if(now.second==60)
  {
    now.second=0;
	now.minute++;
	if(now.minute==60)
	{
	  now.minute=0;
	  now.hour++;
	  if(now.hour==24)
	  {
	    now.hour=0;
		today.day++;
		if(today.day>monthday(today.year,today.month))
		{
		  today.day=1;
		  today.month++;
		  if(today.month==13)
		  today.month=1;
		  today.year++;
		}
	  }
     if(operation==DEFAULT)display_date();

	}
  }
  if(operation==SET_TIME)return;
  display=now;
  if(operation==DEFAULT)dispklay_time();

}


static void timer0_init();
{
  EA=0;
  TR0=0;
  TMOD=0x01;
  TL0=0xf0;
  TH0=0xd8;      //定时10ms
  PT0=0;
  ET0=1;
  TR0=1;
  EA=1;
}

void main()    //主函数
{
  char keys;   //键值变量
  int i;
  LOCK=1;               //定义P0.3为LOCK,控制SSR
  txOK=1;
  init_lcd();
  clear_lcd();
  gotoxy(2,0);
  display_lcd_string("20");
  display=now;
  display_time();
  display_date();
  EA=1;
  timer0_init();
  IT0=1;
  //EX0=1;
  //gotoxy(1,8);
 // for(i=0;i<4;i++)
 // pwd[i]=read_byte[i];
  do{
       keys=gotkey();
	   switch(keys){
	                  case SET_TIME :
					      operation=SET_TIME;
						  if(gettime()) 
						     now=display;
						  if(getdate())  
						     {today=tmpday;display_date();}
					      operation=0;
						  break;
					  case INPUT_PASSWORD :
					      clear_lcd();
						  gotoxy(1,0);
						  display_lcd_string(prompt);
						  operation=INPUT_PASSWORD;
						  get_password();
						  clear_lcd();
						  gotoxy(1,10);
						  if(check_password())
						  {
						    lock=0;
							display_lcd_string("RIGHT");

						  }
						  else
						  {
						    display_lcd_string("WRONG!");
							operation=0;
							gotoxy(2,0);
							display_lcd_string("20");
							display_time();
							display_date();
							break;
					   case SET_PASSWORD :
					        clear_lcd();
							gotoxy(1,0);
							display_lcd_string(old_password);
							operation=SET_PASSWORD;
							get_password();
							if(check_password())
							   if(input_new_password())
							   {
							     clear_lcd();
								 gotoxy(1,10);
								 display_lcd_string("OK");

							   }
							   else
							   {
							     clear_lcd();
								 gotoxy(1,10);
								 display_lcd_string("FAILURE!!!");

							   }
						   else
							 {
							   clear_lcd();
							 }
                             operation=0;
							 gotoxy(2,0);
							 display_lcd_string("20");
							 display_time();
							 display_date();
							 break;

						  
						 
	               }

    }while(1);
}



⌨️ 快捷键说明

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