Single chip electronic password lock with password change

Publisher:才富五车330Latest update time:2016-11-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

4*4 keyboard input, can change password, 24C02 is used to store password. I use 6 digits. You can also use more digits by changing the corresponding variables. It's very simple. I have written the initial password 2 3 4 6 7 0 in 24C02 in advance. Debugging is successful and can be used directly. STC89C51RC single chip microcomputer, universal board welding
    // The data {2,3,4,6,7,0} has been written to 24C02 on the experimental board
#include
#include
#define uchar unsigned char
#define uint unsigned int
char code SKEY[16]={4,8,11,15,3,7,10,14,2,6,0,13,1,5,9,12};
char code act[4]={0xfe,0xfd,0xfb,0xf7};
char data rd[6]={1,9,8,5,1,1};
char ct=0;
sbit contel=P2^1;
sbit cin=P0^3;
sbit set=P0^4;
sbit wrong=P0^5;  
sbit SDA=P2^6;
sbit SCL=P2^7;
bdata char com_data;
sbit mos_bit=com_data^7;
sbit low_bit=com_data^0;
void delay_iic(int n);
uchar rd_24c01(char a);
void wr_24c01(char a,char b); 
void wtsec(); 
/**************************************/
void delay_iic(int n)
{
int
for(i= 1;i}
void delay(unsigned int k)
{
unsigned int data i,j;
for(i=0;i{
for(j=0;j<121;j++)
{;}
}
}
//************************************
void start()
{
SDA=1;_nop_();_nop_();_nop_();_nop_();
SCL=1;_nop_();_nop_();_nop_();_nop_(); SDA
=0;_nop_();_nop_();_nop_();_nop_( ) ;
_();_nop_();_nop_ ();_nop_(); SCL = 1 ;_nop_();_nop_();_nop_();
_ nop_(); _nop_() ;_nop_() ; SCL=0;_nop_();_nop_();_nop_();_nop_(); } //****************************** void shift8(char a) { data uchar i; com_data=a; for(i=0;i<8;i++) { SDA=mos_bit; SCL=1;_nop_();_nop_();_nop_();_nop_();_nop_();_ nop_(); SCL=0;_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); com_data=com_data*2; } } //****************** uchar rd_24c01(char a) { data uchar i,command; SDA=1;_nop_();_nop_();_nop_();_no p_(); SCL=0;_nop_();_nop_();_nop_();_nop_(); start(); command=160; shift8(command); ack(); shift8(a); ack(); start(); command=161; shift8(command); ack(); SDA=1;_nop_();_nop_();_nop_();_nop_(); for(i=0;i<8;i++) {













































com_data=com_data*2;
SCL=1;_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
low_bit=SDA;
SCL=0;_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();
}
stop();
return(com_data);
}
//******************
void wr_24c01(char a,char b)
{
data uchar command;
_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=1;_nop_();_nop_();_nop_();_nop_();
SCL=0;_nop_();_nop_();_nop_();_nop_();
start();
command=160;
shift8(command);
ack();
shift8(a);
ack();
shift8(b);
ack();
stop();
_nop_();_nop_();_nop_();_nop_();_nop_();
}
//Keyboard Scan
char scan_key(void)
{
char i,j,in,ini=0,inj=0,temp,del;
bit find=0;
 P1=0x0f; 
 temp=P1;
 if(temp!=0x0f) //The key is pressed Release 
for(del=10;del>0;del--); //Keyboard debounce delay
if(temp!=0x0f)
{
 for(i=0;i<4;i++)
 {
 P1=act;
 in =P1;
 in=in>>4;
 in=in|0xf0;
  for(j=0;j<4;j++)
  {
    if(act[j]==in)
    {find=1;
    inj=j;ini= i;
    }
  }
 }
}
 if(find==0)return -1;
 return (ini*4+inj);
}
//Input function
void put_secret()
{
char wrt[6];
char et,ft;
char wt,x=1; 
char * p,*t;
p=rd;
t=wrt;
for(wt=0;wt<6;) 
{
 et=scan_key();
if(et!=-1)
{
wrt[wt]=SKEY[et];
wt=wt+x;
cin=0;
delay(200);
cin=1;
}
}
ct=0;
for (ft=0;ft<6;)
{
if(wrt==rd)
{ct=ct+x ; }
ft=ft+x;
}
if(ct==6)
 contel=1;
else 
wrong=0;
}
 void wtsec()
  {
char t[6];
char e,f,i=10;
char w,x =1; 
char *m,*n;
m=rd;
n=t;
for(w=0;w<6;) 
{
 e=scan_key();
if(e!=-1)
{
t[w]=SKEY[e] ;
w=w+x;
cin=0;
delay(200);
cin=1;
}       
}
for (f=0;f<6;)
{
wr_24c01(i,t[f]);
delay_iic(250);
rd [f]=rd_24c01(i);
delay_iic(250);
f=f+x;
i=i+x;
}       
  }
//==================== ========
void main(void)
{
char c,f, key_val,x=1,ii=10;
contel=0;
wrong=1;
cin=1;
set=0; 
delay(2000);
for (f=0;f<6;)
{
rd[ f]=rd_24c01(ii);
delay_iic(250);
f=f+x;
ii=ii+x;
}
set=1;
delay(1000);
 while(1)
 {  
 c=scan_key();
   if(c! =-1) 
   key_val=SKEY[c];
   switch(key_val)  
    {  
   case 12:cin=0;delay(1000);cin=1;put_secret();break ;
      case 13:ct=0;wrong=1;contel =0;cin=1;set=1;break;  
      case 14:if(ct==6){set=0;wtsec();set=1;} set=1;ct=0;break;
      default:break ;
}
}
}

Reference address:Single chip electronic password lock with password change

Previous article:Application of eeprom inside STC89C52 MCU
Next article:7219 chip drives LED electronic clock

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号