# include & ltreg52.h & gt
# include & ltintrins.h & gt
# Define uchar unsigned characters
# Define uint unsigned integer
# define LCD_data P0
sbit sda=p3^5;
sbit scl=p3^4; //24C08 Control Port Settings
sbit LCD _ RS = p3^3; //Register Select Input
sbit LCD _ rw = p3^6; //LCD read/write control
sbit LCD _ en = p3^7; //liquid crystal enable control
sbit LCD _ PSB = p3^2; //serial/parallel mode control
sbit fm=p2^4; //Buzzer control port
sbit rs=p2^5;
sbit t _ clk = p2^0; //Real-time clock line pin//
sbit t _ io = p2^ 1; //Real-time clock data line pin//
sbit t _ rst = p2^2; //Real-time clock reset line pin//
sbit ds=p2^3;
sbit en=p2^6;
sbit zz=p2^7;
sbit fz=p3^ 1;
sbit acc0=acc^0;
sbit acc7=acc^7;
Uint temp 1, s _ temp// define an integer variable.
The floating-point f _ temp// defines a floating-point variable.
uchar time[]= "::";
uchar day[]= " 20//()";
Uchar temp0[]= "Temperature:. Degree ";
uchar num,num 1,flag,count,a,b;
uchar unlock _ I; //Decryption flag bit
uchar t[4];
uchar t 1[4];
Void delay_ms(uint z)// long delay
{
uint x,y;
for(x = z; x & gt0; x -)
for(y = 1 10; y & gt0; y-);
}
Void delay() // Short delay, about 5us.
{
; ;
}
void reshi()
{
If (RS== 1)
{ unlock _ I = 1;
}
other
{
unlock _ I = 0;
}
}
Uchar code mima[]={'0',' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9',' *';
void lcd_xieping0(uchar x,uchar y,uchar date);
Void lcd _ Xie Ping (uchar x, uchar y, uchar * str);
//********************************************************
//Start the display
//********************************************************
void kjxs()
{
uint i,j;
Lcd _ Xie Ping (0,0, "* * * * * * * * * * * * *);
Lcd _ Xie Ping (1, 0, "Welcome");
Lcd _ Xie Ping (2,0, "Password Lock System!" );
Lcd _ Xie Ping (3,0, "* * * * * * * * * * * * * * *);
delay _ ms(4000);
Lcd _ Xie Ping (0,0, "System Initialization");
Lcd _ Xie Ping (1, 0, "Please wait ...");
Lcd _ Xie Ping (2,0, "——————————";
Lcd _ Xie Ping (3,0, "");
for(j = 3; j & gt0; j -)
{
for(I = 0; I<8; i++)
{
LCD Xie Ping (3, I, "*");
delay _ ms(250);
}
Lcd _ Xie Ping (3,0, "");
}
}
//********************************************************
// 12864 display
//********************************************************
Void write_cmd(uchar command)
{
LCD _ RS = 0;
LCD _ RW = 0;
LCD _ EN = 0;
P0 = cmd
delay _ ms(5);
LCD _ EN = 1;
delay _ ms(5);
LCD _ EN = 0;
}
void write_dat(uchar dat)
{
LCD _ RS = 1;
LCD _ RW = 0;
LCD _ EN = 0;
P0 = dat
delay _ ms(5);
LCD _ EN = 1;
delay _ ms(5);
LCD _ EN = 0;
}
void lcd_xieping0(uchar x,uchar y,uchar date)
{
Switch (x)
{
Case 0: write _ cmd (0x80+y); Break;
Case1:write _ cmd (0x90+y); Break;
Case 2: write _ cmd (0x88+y); Break;
Case 3: write _ cmd (0x98+y); Break;
}
Write_dat (date);
}
Invalid lcd _ Xie Ping (uchar x, uchar y, uchar *str).
{
Switch (x)
{
Case 0: write _ cmd (0x80+y); Break;
Case1:write _ cmd (0x90+y); Break;
Case 2: write _ cmd (0x88+y); Break;
Case 3: write _ cmd (0x98+y); Break;
}
while (*str)
{
write _ dat(* str);
str++;
}
}
void lcd_init()
{
LCD _ PSB = 1; //Parallel port mode
write _ cmd(0x 30); //Basic instruction operation
delay _ ms(5);
write _ cmd(0x0C); //Turn the display cursor on and off.
delay _ ms(5);
write _ cmd(0x 0 1); //Clear the display of LCD.
delay _ ms(5);
}
//**************************************************************
//Keyboard scanning function
//**************************************************************
Uchar keyscan 1() // matrix keyboard scanning function
{
Uchar temperature;
And (! Numbers)
{ p 1 = 0x Fe; //assignment
temp = p 1; //Read back data
Temperature = Temperature & 0xf0//AND operation
if(temp! =0xf0) // Judgment
{
delay _ ms(2); //Delayed debounce
temp = p 1; //Read back data
Temperature = temperature & amp0xf0
if(temp! =0xf0)
{
Switch (temperature)//Multi-branch selection
{
Case 0x70: num =1; Break; //Jump out
Case 0xb 0:num = 2;; Break;
Case 0xd 0:num = 3;; Break;
Case 0xe0: num = 4; Break;
}
while(temp! =0xf0)
{
temp = p 1;
Temperature = temperature & amp0xf0
}//Wait for the button to be released
}
}
p 1 = 0x FD; //assignment
temp = p 1; //Read back data
Temperature = Temperature & 0xf0//AND operation
if(temp! =0xf0) // Judgment
{
delay _ ms(2); //Delayed debounce
temp = p 1; //Read back data
Temperature = temperature & amp0xf0
if(temp! =0xf0)
{
Switch (temperature)//Multi-branch selection
{
Case 0x70: num = 5; Break; //Jump out
Case 0xb 0:num = 6;; Break;
Case 0xd 0:num = 7;; Break;
Case 0xe0: num = 8; Break;
}
while(temp! =0xf0)
{
temp = p 1;
Temperature = temperature & amp0xf0
}//Wait for the button to be released
}
}
p 1 = 0x FB; //assignment
temp = p 1; //Read back data
Temperature = Temperature & 0xf0//AND operation
if(temp! =0xf0) // Judgment
{
delay _ ms(2); //Delayed debounce
temp = p 1; //Read back data
Temperature = temperature & amp0xf0
if(temp! =0xf0)
{
Switch (temperature)//Multi-branch selection
{
Case 0x70: num = 9; Break; //Jump out
case 0xb 0:num = 10; Break;
case 0xd 0:num = 1 1; Break;
Case 0xe0: num =12; Break;
}
while(temp! =0xf0)
{
temp = p 1;
Temperature = temperature & amp0xf0
}//Wait for the button to be released
}
}
}
Return (number); //Return value
}
Uchar key scan 2 ()
{
Uchar temperature;
And (! num 1)
{ p 1 = 0xf 7; //assignment
temp = p 1; //Read back data
Temperature = Temperature & 0xf0//AND operation
if(temp! =0xf0) // Judgment
{
delay _ ms(2); //Delayed debounce
temp = p 1; //Read back data
Temperature = temperature & amp0xf0
if(temp! =0xf0)
{
Switch (temperature)//Multi-branch selection
{
Case 0x70: num1=1; Break; //Jump out
case 0xb 0:num 1 = 2; Break;
case 0xd 0:num 1 = 3; Break;
case 0xe 0:num 1 = 4; Break;
}
while(temp! =0xf0)
{
temp = p 1;
Temperature = temperature & amp0xf0
}//Wait for the button to be released
}
}
}
return(num 1);
}
//**************************************************************
//DC car
//**************************************************************
void dianjiZZ()
{
EN = 1;
ZZ = 1;
FZ = 0;
}
void dianjiFZ()
{
EN = 1;
ZZ = 0;
FZ = 1;
}
void dianji_stop()
{
EN = 0;
}
//**************************************************************
// EPPROM
//**************************************************************
Void start() // start signal
{
SDA = 1;
delay();
SCL = 1;
delay();
SDA = 0;
delay();
}
Void stop() // stop signal
{
SDA = 0;
delay();
SCL = 1;
delay();
SDA = 1;
delay();
}
Void responses()// response signal
{
Ucal I;
SCL = 1;
delay();
while((SDA = = 1)& amp; & (I & lt250))
i++;
SCL = 0;
delay();
}
Void writebyte(uchar date) // Write a byte.
{
uchar i,temp
Temp = date;
for(I = 0; I<8; i++)
{
temp = temp & lt& lt 1;
SCL = 0;
delay();
SDA = CY
delay();
SCL = 1;
delay();
}
SCL = 0;
delay();
SDA = 1; //Release the bus
delay();
}
Uchar readbyte() // Read a byte.
{
Uchar I, K;
SCL = 0;
delay();
SDA = 1;
for(I = 0; I<8; i++)
{
SCL = 1;
delay();
k =(k & lt; & lt 1)| SDA;
SCL = 0;
delay();
}
delay();
Return (k);
}
Voidwrite (uchar add, uchar date)// Write a byte at an address.
{
start();
write byte(0xa 0);
respons();
write byte(add);
respons();
Writebyte (date);
respons();
stop();
}
Uchar read(uchar add) // Read a byte of an address.
{
start();
write byte(0xa 0);
respons();
write byte(add);
respons();
start();
write byte(0xa 1);
respons();
b = read byte();
respons();
stop();
Return (b);
}
//**************************************************************
//Time date function
//**************************************************************
void v_WTInputByte(uchar ucDa)
{
Ucal I;
ACC = ucDa
for(I = 8; I>0; I-)
{
T _ IO = ACC0//* is equivalent to RRC in the assembly.
t _ CLK = 1;
t _ CLK = 0;
ACC = ACC & gt& gt 1;
}
}
uchar uc_RTOutputByte(void)
{
Ucal I;
for(I = 8; I>0; I-)
{
ACC = ACC & gt& gt 1; //* Equivalent to RRC in the assembly.
ACC7 = T _ IO
t _ CLK = 1;
t _ CLK = 0;
}
Return (ACC);
}
void v_W 1302(uchar ucAddr,uchar ucDa)
{
t _ RST = 0;
t _ CLK = 0;
t _ RST = 1;
v _ WTInputByte(ucAddr); /* address, command */
v _ WTInputByte(ucDa); /* Write 1 byte data */
t _ CLK = 1;
t _ RST = 0;
}
uchar UC _ r 1302(uchar ucAddr)
{
uchar ucDa
t _ RST = 0;
t _ CLK = 0;
t _ RST = 1;
v _ WTInputByte(ucAddr); //address, command//
ucDa = UC _ RTOutputByte(); //Read 1 byte data//
t _ CLK = 1;
t _ RST = 0;
Return (UCDA);
}
void Init 1302(void)
{
v_W 1302(0x8e,0x 00); //control write WP=0
v_W 1302(0x80,0302
v_W 1302(0x90,0xa 9);
v_W 1302(0x80,0x 00); //sec
v_W 1302(0x82,0x 24); //minute
v_W 1302(0x84,0x 12); //hour
v_W 1302(0x86,0x 29); //day
v_W 1302(0x88,0x 10); //month
v_W 1302(0x8a,0x 05); //week
v_W 1302(0x8c,0x 10); //Year//
v_W 1302(0x8e,0x 80);
}
Void completion time (void)
{
uchar d;
d = UC _ r 1302(0x 87);
day[ 10]=(d & amp; 0x0f)+48;
day[9]=((d & gt; & gt4)& amp; 0x 03)+48;
d = UC _ r 1302(0x 89);
day[7]=(d & amp; 0x0f)+48;
day[6]=((d & gt; & gt4)& amp; 0x 0 1)+48;
d = UC _ r 1302(0x8b);
day[ 13]=(d & amp; 0x 07)+48;
d = UC _ r 1302(0x8d);
day[4]=(d & amp; 0x0f)+48;
day[3]=(d & gt; & gt4)+48;
d = UC _ r 1302(0x 8 1);
time[ 15]=(d & amp; 0x0f)+48;
time[ 14]=(d & gt; & gt4)+48;
d = UC _ r 1302(0x 83);
time[ 12]=(d & amp; 0x0f)+48;
time[ 1 1]=(d & gt; & gt4)+48;
d = UC _ r 1302(0x 85);
time[9]=(d & amp; 0x0f)+48;
time[8]=(d & gt; & gt4)+48;
}
//**************************************************************
//Temperature detection function
//**************************************************************
Void dsreset (void)/18b20 resets the initialization function.
{
uint I;
ds = 0;
I = 103;
while(I & gt; 0)I-;
ds = 1;
I = 4;
while(I & gt; 0)I-;
}
Bit tempreadbit(void) // read 1 bit function.
{
uint I;
bit dat
ds = 0; i++; //i++ plays the role of delay.
ds = 1; i++; i++;
Dat = ds// reading data
I = 8; while(I & gt; 0)I-;
return(dat);
}
Uchar tempread(void) // read 1 bytes.
{
uchar i,j,dat
dat = 0;
for(I = 1; I < = 8; i++)
{
j = tempreadbit();
dat =(j & lt; & lt7)|(dat & gt; & gt 1); //The lowest bit of the read data comes first, so there is only one byte in the DAT.
}
return(dat);
}
Void tempwritebyte(uchar dat) // Write one byte of data into 18B20.
{
uint I;
uchar j;
Bit testb
for(j = 1; j & lt=8; j++)
{
dat & amp0x 0 1; //Judge whether the last digit is 1 or 0.
dat = dat & gt& gt 1;
If(testb) // write 1
{
ds = 0;
i++; i++;
ds = 1;
I = 8; while(I & gt; 0)I-;
}
other
{
ds = 0; //Write 0
I = 8; while(I & gt; 0)I-;
ds = 1;
i++; i++;
}
}
}
Void temp change (void)//ds18b20 starts to collect temperature and convert it.
{
dsreset(); //Initialize. Every operation on 18B20 must be initialized first.
delay _ ms( 1);
tempwritebyte(0x cc); //write skip read ROM instruction
tempwritebyte(0x 44); //Write temperature conversion instruction
}
Void get_temp() // Read the temperature data stored in the register.
{
uchar a,b;
dsreset(); //initialization
delay _ ms( 1);
tempwritebyte(0x cc); //write skip read ROM instruction
tempwritebyte(0x be); //Write and read instructions
a = tempread(); //Read the lower 8 bits
b = tempread(); //Read the upper 8 bits
temp 1 = b;
temp 1 & lt; & lt=8; //The combination of two bytes is 1 word.
temp 1 = temp 1 | a;
f _ temp = temp 1 * 0.0625; //For 12 bit resolution bits, the temperature in the register is 0.0625.
}
//**************************************************************
//Decryption function
//**************************************************************
Invalid unlock ()
{
Uchar is here, me;
If (Quantity ==0)
{
Lcd _ Xie Ping (0,0, "* * password lock system * *");
Lcd _ Xie Ping (1, 0, "—————————";
Lcd _ Xie Ping (2,0, "Please enter the password:");
Lcd _ Xie Ping (3,0, "");
for(I = 0; I<4; i++)
{
t 1[I]= keyscan 1();
LCD Xie Ping (3, I, "*");
num = 0;
}//Enter the password
}
in = keyscan 1();
If(in== 12)//in- Determines the key flag bit.
{
in = 0;
num = 0;
if((t 1[0]= = t[0])& amp; & amp(t 1[ 1]= = t[ 1])& amp; & amp(t 1[2]= = t[2])& amp; & amp(t 1[3]==t[3])
{
flag = 1; //Decryption success flag bit
//unlock _ I = 1;
a = 0; //Function key mark
Lcd _ Xie Ping (0,0, "* * password lock system * *");
Lcd _ Xie Ping (1, 0, "—————————";
Lcd _ Xie Ping (2,0, "Password is correct!" );
Lcd _ Xie Ping (3,0, "Your identity has been confirmed");
delay _ ms( 1500);
Lcd _ Xie Ping (1, 0, "———————";
Lcd _ Xie Ping (2,0, "Function I Unlocked");
Lcd _ Xie Ping (3,0, "change the password twice");
}
other
{
flag = 0;
count++;
if(count==3)
{
count = 0;
num = 1;
Lcd _ Xie Ping (1, 0, "—————————";
Lcd _ Xie Ping (2,0, "Your chance is used up");
Lcd _ Xie Ping (3,0, "Sorry * * can't enter");
FM = 0;
delay _ ms( 1000);
FM = 1;
}
}
}
}
//**************************************************************
//Modify password function
//**************************************************************
void xiugaimima()
{ uchar i,j,l,im,IB;
uchar T2[4];
uchar T3[4];
num = 0;
Lcd _ Xie Ping (1, 0, "———————";
Lcd _ Xie Ping (2,0, "Please enter a new password:");
Lcd _ Xie Ping (3,0, "");
for(I = 0; I<4; i++)
{
T2[I]= keyscan 1();
lcd_xieping0(3,I,mima[num]);
num = 0;
}
im = keyscan 1();
If(im== 12)//im, in, ib, these two are the signs that determine the key.
{
im = 0;
num = 0;
Lcd _ Xie Ping (1, 0, "———————";
Lcd _ Xie Ping (2,0, "Please enter the new password again");
Lcd _ Xie Ping (3,0, "");
for(I = 0; I<4; i++)
{
T3[I]= keyscan 1();
lcd_xieping0(3,I,mima[num]);
num = 0;
}
}
IB = keyscan 1();
if(ib== 12)
{
IB = 0;
num = 0;
if(T2[0]= = T3[0]& amp; & ampT2[ 1]= T3[ 1]& amp; & ampT2[2]= = T3[2]& amp; & ampt2[3]==t3[3])
{
t[0]= T3[0];
t[ 1]= T3[ 1];
t[2]= T3[2];
t[3]= T3[3];
Lcd _ Xie Ping (1, 0, "———————";
Lcd _ Xie Ping (2,0, "Congratulations!" );
Lcd _ Xie Ping (3,0, "Password modified successfully");
flag = 0;
for(j = 0; j & lt4; j++)
{
l = j+ 1;
write(l,t[j]);
delay _ ms( 10);
}//24C08 Write data
delay _ ms( 1000);
}
other
{
Lcd _ Xie Ping (2,0, "The passwords entered twice are different");
Lcd _ Xie Ping (3,0, "Password modification failed");
flag = 1;
delay _ ms(500);
}
}
}
//**************************************************************
//Display function
//**************************************************************
Invalid stone present ()
{
donetime();
temp change();
get _ temp();
s _ temp = f _ temp * 100;
temp 0[7]=(s _ temp/ 1000)+48;
temp 0[8]=(s _ temp % 1000/ 100)+48;
temp 0[ 10]=(s _ temp % 100/ 10)+48;
temp 0[ 1 1]=(s _ temp % 10)+48;
Lcd _ Xie Ping (0,0, "* * password lock system * *");
Lcd _ Xie Ping (1, 0, temp0);
Lcd _ Xie Ping (2,0, day);
Lcd _ Xie Ping (3,0, time);
num = 0;
}
//**************************************************************
//Unlock function
//**************************************************************
Void unlock ()
{
Ucal I;
Lcd _ Xie Ping (2,0, "Unlock ...");
LCD _ Xie Ping (3,0, "-Wait patiently-);
for(I = 3; I>0; I-)
{
FM = 0;
delay _ ms( 100);
FM = 1;
delay _ ms( 100);
flag = 0;
}
dianjiZZ();
delay _ ms( 10000);
Dian Ji _ stop();
LCD _ Xie Ping (2,0, "-end of unlocking process-");
Lcd _ Xie Ping (3,0, "Please open the door");
delay _ ms(5000);
dianjiFZ();
delay _ ms( 10000);
Dian Ji _ stop();
flag = 0;
}
//**************************************************************
//main function
//**************************************************************
void main()
{
uchar m;
unlock _ I = 1;
LCD _ init(); //Liquid crystal initialization
//init 1302();
kjxs(); //Start the display
for(m = 0; m & lt4; m++)
{
t[m]= read(m+ 1);
delay _ ms( 10);
}//24C08 Read data
while( 1)
{
reshi();
If (! Unlock _i)
{
unlock(); //Decryption function
}
other
{
Shi Xian (); //Time, date and temperature display function
}
if(flag== 1)
{
num 1 = 0;
a = keyscan 2();
If (a== 1)
{
Caso (); //Unlock function
}
If (a==2)
{
xiugaimima(); //Modify password function
}
}
}
}