O_基于51单片机的双核GPS电子密码锁代码_毕设


O_嵌入式专题目录



STCLockSCode.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "Header.h"

uchar Lat_ROM[]={"3614.3948"};
uchar Long_ROM[]={"12026.3911"};

void main()
{
ATY_Init();

Clear_Ram();
Show_Line(X2,"Searching GPS...");
//Show_Line(X2,Lat_ROM);

while(1)
{
parseGpsBuffer();
printGpsBuffer();
if(Save_Data.isShowData)
{
UartPrintf("\r\nSuccess!");
//Uart_End();
break;
}
}
/* Electronic fence */
if(((int)Lat_ROM[5]-(int)Save_Data.latitude[5])>1 || ((int)Long_ROM[6]-(int)Save_Data.longitude[6])>1)
{
Show_Line(X2,"Location Wrong!");
DelayMs(3000);
}
else
Judge_Password();
}

Header.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef _HEADER_H_
#define _HEADER_H_

#include <reg52.h>
#include "90.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <intrins.h> //_nop_()

#include "Delay.h"
#include "Alarm.h"
#include "Infrared.h"
#include "KeyControl.h"
#include "UART.h"
#include "AT24C02.h"
#include "Index.h"
#include "DHT11.h"
#include "JudgePassword.h"
#include "GPS.h"
#include "Infrared.h"

void ATY_Init()
{
P42=0;
Relay=1;
Uart_Init();
Delay_ms(10);
clrStruct();
//SP=0x5f;
Init_Lcd();
CloseLock=1;
OpenLock=0;
RR_DATA=0;
F_Scan=1;
F_Input=1;
F_Success=1;
F_Fail=1;
}


/*
sbit Alarm = P1^0; //Alarm

sbit CS = P1^1; //LCD
sbit STD = P1^2;
sbit SCLK = P1^3;
sbit PSB = P0^0;
sbit RST = P0^1;

sbit SCL = P1^4; //24C02 Serial clock
sbit SDA = P1^5; //24C02 Serial data

sbit CloseLock = P1^6;
sbit OpenLock = P1^7;

Keyboard P2 //Keyboard

GPS UART P3^0; //GPS

sbit Relay = P3^1; //Relay

sbit RR_DATA = P3^2; //Infrared

sbit Data = P3^3; //DHT11

sbit F_Scan = P3^4; //Finger
sbit F_Input = P3^5;
sbit F_Success = P3^6;
sbit F_Fail = P3^7;

*/

#endif

90.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef __STC89C5xRC_RDP_H_
#define __STC89C5xRC_RDP_H_

/////////////////////////////////////////////////

/* The following is STC additional SFR */

/* sfr AUXR = 0x8e; */
/* sfr AUXR1 = 0xa2; */
/* sfr IPH = 0xb7; */

sfr P4 = 0xe8;
sbit P46 = P4^6;
sbit P45 = P4^5; //ISP下载需勾选"ALE脚用作P4.5口"
sbit P44 = P4^4;
sbit P43 = P4^3;
sbit P42 = P4^2;
sbit P41 = P4^1;
sbit P40 = P4^0;

sfr XICON = 0xc0;

sfr WDT_CONTR = 0xe1;

sfr ISP_DATA = 0xe2;
sfr ISP_ADDRH = 0xe3;
sfr ISP_ADDRL = 0xe4;
sfr ISP_CMD = 0xe5;
sfr ISP_TRIG = 0xe6;
sfr ISP_CONTR = 0xe7;

/* Above is STC additional SFR */

/*--------------------------------------------------------------------------
REG51F.H

Header file for 8xC31/51, 80C51Fx, 80C51Rx+
Copyright (c) 1988-1999 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.

Modification according to DataSheet from April 1999
- SFR's AUXR and AUXR1 added for 80C51Rx+ derivatives
--------------------------------------------------------------------------*/

/* BYTE Registers */
sfr P0 = 0x80;
sbit P00 = P0^0;
sbit P01 = P0^1;
sbit P02 = P0^2;
sbit P03 = P0^3;
sbit P04 = P0^4;
sbit P05 = P0^5;
sbit P06 = P0^6;
sbit P07 = P0^7;
sfr P1 = 0x90;
sbit P10 = P1^0;
sbit P11 = P1^1;
sbit P12 = P1^2;
sbit P13 = P1^3;
sbit P14 = P1^4;
sbit P15 = P1^5;
sbit P16 = P1^6;
sbit P17 = P1^7;
sfr P2 = 0xA0;
sbit P20 = P2^0;
sbit P21 = P2^1;
sbit P22 = P2^2;
sbit P23 = P2^3;
sbit P24 = P2^4;
sbit P25 = P2^5;
sbit P26 = P2^6;
sbit P27 = P2^7;
sfr P3 = 0xB0;
sbit P30 = P3^0;
sbit P31 = P3^1;
sbit P32 = P3^2;
sbit P33 = P3^3;
sbit P34 = P3^4;
sbit P35 = P3^5;
sbit P36 = P3^6;
sbit P37 = P3^7;
sfr PSW = 0xD0;
sfr ACC = 0xE0;
sfr B = 0xF0;
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TL1 = 0x8B;
sfr TH0 = 0x8C;
sfr TH1 = 0x8D;
sfr IE = 0xA8;
sfr IP = 0xB8;
sfr SCON = 0x98;
sfr SBUF = 0x99;

/* 80C51Fx/Rx Extensions */
sfr AUXR = 0x8E;
sfr AUXR1 = 0xA2;
sfr SADDR = 0xA9;
sfr IPH = 0xB7;
sfr SADEN = 0xB9;
sfr T2CON = 0xC8;
sfr T2MOD = 0xC9;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2 = 0xCC;
sfr TH2 = 0xCD;

/* PCA SFR
sfr CCON = 0xD8;
sfr CMOD = 0xD9;
sfr CCAPM0 = 0xDA;
sfr CCAPM1 = 0xDB;
sfr CCAPM2 = 0xDC;
sfr CCAPM3 = 0xDD;
sfr CCAPM4 = 0xDE;
sfr CL = 0xE9;
sfr CCAP0L = 0xEA;
sfr CCAP1L = 0xEB;
sfr CCAP2L = 0xEC;
sfr CCAP3L = 0xED;
sfr CCAP4L = 0xEE;
sfr CH = 0xF9;
sfr CCAP0H = 0xFA;
sfr CCAP1H = 0xFB;
sfr CCAP2H = 0xFC;
sfr CCAP3H = 0xFD;
sfr CCAP4H = 0xFE;
*/

/* BIT Registers */
/* PSW */
sbit CY = PSW^7;
sbit AC = PSW^6;
sbit F0 = PSW^5;
sbit RS1 = PSW^4;
sbit RS0 = PSW^3;
sbit OV = PSW^2;
sbit F1 = PSW^1;
sbit P = PSW^0;

/* TCON */
sbit TF1 = TCON^7;
sbit TR1 = TCON^6;
sbit TF0 = TCON^5;
sbit TR0 = TCON^4;
sbit IE1 = TCON^3;
sbit IT1 = TCON^2;
sbit IE0 = TCON^1;
sbit IT0 = TCON^0;

/* IE */
sbit EA = IE^7;
sbit EC = IE^6;
sbit ET2 = IE^5;
sbit ES = IE^4;
sbit ET1 = IE^3;
sbit EX1 = IE^2;
sbit ET0 = IE^1;
sbit EX0 = IE^0;

/* IP */
/* sbit PPC = IP^6;*/
sbit PT2 = IP^5;
sbit PS = IP^4;
sbit PT1 = IP^3;
sbit PX1 = IP^2;
sbit PT0 = IP^1;
sbit PX0 = IP^0;

/* P3 */
sbit RD = P3^7;
sbit WR = P3^6;
sbit T1 = P3^5;
sbit T0 = P3^4;
sbit INT1 = P3^3;
sbit INT0 = P3^2;
sbit TXD = P3^1;
sbit RXD = P3^0;

/* SCON */
sbit SM0 = SCON^7; // alternatively "FE"
sbit FE = SCON^7;
sbit SM1 = SCON^6;
sbit SM2 = SCON^5;
sbit REN = SCON^4;
sbit TB8 = SCON^3;
sbit RB8 = SCON^2;
sbit TI = SCON^1;
sbit RI = SCON^0;

/* P1 */
/* PCA
sbit CEX4 = P1^7;
sbit CEX3 = P1^6;
sbit CEX2 = P1^5;
sbit CEX1 = P1^4;
sbit CEX0 = P1^3;
sbit ECI = P1^2;
*/

sbit T2EX = P1^1;
sbit T2 = P1^0;

/* T2CON */
sbit TF2 = T2CON^7;
sbit EXF2 = T2CON^6;
sbit RCLK = T2CON^5;
sbit TCLK = T2CON^4;
sbit EXEN2 = T2CON^3;
sbit TR2 = T2CON^2;
sbit C_T2 = T2CON^1;
sbit CP_RL2= T2CON^0;

/* CCON */
/* PCA
sbit CF = CCON^7;
sbit CR = CCON^6;

sbit CCF4 = CCON^4;
sbit CCF3 = CCON^3;
sbit CCF2 = CCON^2;
sbit CCF1 = CCON^1;
sbit CCF0 = CCON^0;
*/

/////////////////////////////////////////////////

#endif

Delay.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef _DELAY_H_
#define _DELAY_H_

#include "Header.h"
#define uint unsigned int
#define uchar unsigned char

/*-----Delay-----*/
void DelayUs(uint us) //Delay time us
{
while(us--);
}
void DelayMs(uint ms) //Delay time ms
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<15;j++)
DelayUs(1);
}

void DHT11_delay_us(uchar n)
{
while(--n);
}
void DHT11_delay_ms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}

void Delay_10Us(uchar us) //Delay for infrared
{
while(us--);
}

#endif

Alarm.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef _ALARM_H_
#define _ALARM_H_

#include "Header.h"

sbit Alarm = P1^0; //Alarm Pin

uchar Alarm_Switch=0;

/*-----One Buzz-----*/
void OneAlarm()
{
Alarm=0;
DelayMs(Alarm_Switch);
Alarm=1;
}
/*-----Two Buzz-----*/
void TwoAlarm()
{
Alarm=0;
DelayMs(Alarm_Switch);
Alarm=1;
DelayMs(120);
Alarm=0;
DelayMs(Alarm_Switch);
Alarm=1;
}

#endif

Infrared.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef _INFRARED_H_
#define _INFRARED_H_

#include "Header.h"

sbit RR_DATA = P3^2;
uchar RR_Value[4]=0;
uchar RR_Time=0;
uchar RR_Check=0;

void UART_Send_Byte(uchar c)
{
SBUF=c;
while(TI==0);
TI=0;
}
void UART_Send_String(uchar *str)
{
while(*str!='\0')
{
UART_Send_Byte(*str++);
}
}

void RR_Init(void)
{
IT0=1;
EX0=1;
EA=1;
RR_DATA=1;
}
void RR_Read() interrupt 0
{
uchar j,k;
uint err;
RR_Time=0;
Delay_10Us(700);
if(RR_DATA==0)
{
err=1000;
while((RR_DATA==0)&&(err>0))
{
Delay_10Us(1);
err--;
}
if(RR_DATA==1)
{
err=500;
while((RR_DATA==1)&&(err>0))
{
Delay_10Us(1);
err--;
}
for(k=0;k<4;k++)
{
for(j=0;j<8;j++)
{
err=60;
while((RR_DATA==0)&&(err>0))
{
Delay_10Us(1);
err--;
}
err=500;
while((RR_DATA==1)&&(err>0))
{
Delay_10Us(10);
RR_Time++;
err--;
if(RR_Time>30)
{
return;
}
}
RR_Value[k]>>=1;
if(RR_Time>=8)
{
RR_Value[k]|=0x80;
}
RR_Time=0;
}
}
}
if(RR_Value[2]==~RR_Value[3])
{
RR_Check=1;
UART_Send_String(RR_Value); //Show uart data at hex, don't use text type
UART_Send_String("\r\n\r\n");
}
else
{
RR_Check=0;
UART_Send_String("Error\r\n");
}
}
}


#endif

KeyControl.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef _KEYCONTROL_H_
#define _KEYCONTROL_H_

#include "Header.h"

uchar code Keyboard_Control[]={0xfe,0xfd,0xfb,0xf7}; //List of keyboard scanning
uchar RowCol=0;

/*-----Scan the key and return the value-----*/
uchar Key_Check()
{
uchar Row,Col,i;
P2=0xf0;
RR_Init();
if((P2&0xf0)!=0xf0)
{
DelayMs(10);
if((P2&0xf0)!=0xf0)
{
Row=P2^0xf0;
i=0;
P2=Keyboard_Control[i];
while(i<4)
{
if((P2&0xf0)!=0xf0)
{
Col=~(P2&0xff);
break;
}
else
{
i++;
P2=Keyboard_Control[i];
}
}
}
else
{
RowCol=0;
return 0;
}
while((P2&0xf0)!=0xf0);
OneAlarm();
//P0=Row|Col;
RowCol=Row|Col;
return RowCol;
}
else if(RR_Check)
{
RR_Check=0;
switch(RR_Value[2])
{
case 0x01: RowCol=0x11;break;
case 0x02: RowCol=0x21;break;
case 0x03: RowCol=0x41;break;
case 0x04: RowCol=0x12;break;
case 0x05: RowCol=0x22;break;
case 0x06: RowCol=0x42;break;
case 0x07: RowCol=0x14;break;
case 0x08: RowCol=0x24;break;
case 0x09: RowCol=0x44;break;
case 0x00: RowCol=0x28;break;
case 0x40: RowCol=0x84;break; //Cancel
case 0x0A: RowCol=0x88;break; //OK
case 0x19: RowCol=0x81;break; //A up
case 0x1D: RowCol=0x82;break; //B down
case 0x46: RowCol=0x18;break; //* left
case 0x47: RowCol=0x48;break; //# right
case 0x10:
if(Alarm_Switch)
Alarm_Switch=0;
else
Alarm_Switch=200;
break;
}
OneAlarm();
//RR_DATA=0;
return RowCol;
}
else
{
RowCol=0;
return 0;
}
}
/*-----Coding the key value to number-----*/
uchar Coding_Key(uchar RowCol)
{
uchar Coded_Key;
switch(RowCol)
{
case (0x11): Coded_Key='1';break;
case (0x21): Coded_Key='2';break;
case (0x41): Coded_Key='3';break;
case (0x81): Coded_Key='A';break;
case (0x12): Coded_Key='4';break;
case (0x22): Coded_Key='5';break;
case (0x42): Coded_Key='6';break;
case (0x82): Coded_Key='B';break;
case (0x14): Coded_Key='7';break;
case (0x24): Coded_Key='8';break;
case (0x44): Coded_Key='9';break;
case (0x84): Coded_Key='C';break;
case (0x18): Coded_Key='*';break;
case (0x28): Coded_Key='0';break;
case (0x48): Coded_Key='#';break;
case (0x88): Coded_Key='D';break;
}
return Coded_Key;
}

#endif

UART.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef __UART_H__
#define __UART_H__

#include "Header.h"

#define GPS_Buffer_Length 80
#define latitude_Length 11
#define longitude_Length 12

typedef struct SaveData
{
char GPS_Buffer[GPS_Buffer_Length];
char isGetData; //If got the GPS data
char isParseData; //If the data has analysis success
char latitude[latitude_Length]; //WeiDu
char longitude[longitude_Length]; //JingDu
char isUsefull; //If the position info is useful
char isShowData; //Has shown the data
} xdata _SaveData;


#define gpsRxBufferLength 76
char idata gpsRxBuffer[gpsRxBufferLength];
unsigned char RX_Count = 0;
_SaveData Save_Data;

void Uart_Init()
{
SCON = 0X50; //UART type 1, 8 bit UART
REN = 1; //Allow the serial port get data
PCON = 0x00; //SMOD=0; No doubling of baud rate
TMOD = 0x20; //T1 type 2, to generate baud rate
TH1 = 0xFD; //Filling initial value
TL1 = 0xFD;
TR1 = 1; //Open timer 1
EA = 1; //Open global interrupt control
ES = 1; //Open serial port interrupt
}
void Uart_End()
{
REN = 0; //Forbid the serial pin get data
TR1 = 0; //Close timer 1
ES = 0; //Close serial port interrupt
EA = 0; //Close global interrupt control
}

void UartPrintf(unsigned char *p) //Send a string
{
while(*p)
{
SBUF=*(p++);
while(TI==0);
TI=0;
}
}

void UartPrintASCII(unsigned char c) //Send a character
{
TI=0;
SBUF=c;
while(TI==0);
TI=0;
}


void RECEIVE_DATA(void) interrupt 4 using 3
{
unsigned char temp = 0;
char i = 0;
ES=0;
temp = SBUF;
RI = 0;

if(temp == '$')
{
RX_Count = 0;
}

if(RX_Count <= 5)
{
gpsRxBuffer[RX_Count++] = temp;
}
else if(gpsRxBuffer[0] == '$' &gpsRxBuffer[4] == 'M' && gpsRxBuffer[5] == 'C') //Make sure got the frame:"GPRMC/GNRMC"
{
gpsRxBuffer[RX_Count++] = temp;
if(temp == '\n')
{
memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length); //Clear buf
memcpy(Save_Data.GPS_Buffer, gpsRxBuffer, RX_Count); //Save data
Save_Data.isGetData = 1;
RX_Count = 0;
memset(gpsRxBuffer, 0, gpsRxBufferLength); //Clear buf
}

if(RX_Count >= 75)
{
RX_Count = 75;
gpsRxBuffer[RX_Count] = '\0'; //Adding an End Character
}
}
ES=1;
}

void clrStruct()
{
Save_Data.isGetData = 0;
Save_Data.isParseData = 0;
Save_Data.isUsefull = 0;
Save_Data.isShowData = 0;
memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length);
memset(Save_Data.latitude, 0, latitude_Length);
memset(Save_Data.longitude, 0, longitude_Length);
}
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}

void errorLog(int num)
{

while (1)
{
UartPrintf("ERROR");
UartPrintASCII(num+0x30);
UartPrintf("\r\n");
}
}

#endif

AT24C02.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef _AT24C02_H_
#define _AT24C02_H_

#include "Header.h"

sbit SCL = P1^4; //24C02 Serial clock
sbit SDA = P1^5; //24C02 Serial data

/*-----Do Nop-----*/
void Nop()
{
_nop_(); //About 1us per command
_nop_();
_nop_();
_nop_();
}
/*-----Start condition-----*/
void Start()
{
SDA=1;
SCL=1;
Nop();
SDA=0;
Nop();
}
/*-----Stop condition-----*/
void Stop()
{
SDA=0;
SCL=1;
Nop();
SDA=1;
Nop();
}
/*-----Acknowledgement-----*/
void Ack()
{
SDA=0;
Nop();
SCL=1;
Nop();
SCL=0;
}
/*-----DeAcknowledgement-----*/
void DeAck()
{
SDA=1;
Nop();
SCL=1;
Nop();
SCL=0;
}

/*-----Send data-----*/
void Send(uchar Data_Send)
{
uchar BitCounter=8;
uchar Temp;
do
{
Temp=Data_Send; //Save getted data to 'Temp' temporary
SCL=0;
Nop();
if((Temp&0x80)==0x80)
SDA=1;
else
SDA=0;
SCL=1;
Temp=Data_Send<<1;
Data_Send=Temp;
BitCounter--; //Data send over when Bitcounter subtracted to 0
}
while(BitCounter); //Judge whether it is completed or not
SCL=0;
}
/*-----Read one byte data and return-----*/
uchar Read()
{
uchar Data_Read=0;
uchar Temp=0;
uchar BitCounter=8;
SDA=1;
do
{
SCL=0;
Nop();
SCL=1;
Nop();
if(SDA)
Data_Read=Data_Read|0x01; //Data_Read's minimum bit is 1(|0x01,is change the minimum bit to 1)
else //If SDA = 0
Data_Read=Data_Read&0xfe; //Data_Read's minimum bit is 0(&0xfe 11111110 is change the bit position to 0)
if(BitCounter-1) //Judge the BitCounter is true or not after subtracted 1
{
Temp=Data_Read<<1;
Data_Read=Temp;
}
BitCounter--; //Getting data over while BitCounter subtract to 0
}
while(BitCounter); //Judge whether it is completed or not
return Data_Read;
}

/*-----Write data to ROM-----*/
void Write_ROM(uchar Data_Write[],uchar Address,uchar Num)
{
uchar i;
uchar *PData;
PData=Data_Write;
for(i=0;i<Num;i++)
{
Start();
Send(0xa0);
Ack();
Send(Address+i);
Ack();
Send(*(PData+i));
Ack();
Stop();
DelayMs(20);
}
}
/*-----Read data from ROM-----*/
void Read_ROM(uchar Data_Read[],uchar Address,uchar Num)
{
uchar i;
uchar *PData;
PData=Data_Read;
for(i=0;i<Num;i++)
{
Start();
Send(0xa0);
Ack();
Send(Address+i);
Ack();
Start();
Send(0xa1);
Ack();
*(PData+i)=Read();
SCL=0;
DeAck();
Stop();
}
}

#endif

Index.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef _INDEX_H_
#define _INDEX_H_

#include "Header.h"

#define X1 0x80 //X first line
#define X2 0x90 //X second line
#define X3 0x88 //X third line
#define X4 0x98 //X fourth line
#define Y 0x80
#define COMMAND 0
#define DATA 1

sbit CS = P1^1;
sbit STD = P1^2; //Data port
sbit SCLK = P1^3;
sbit PSB = P0^0; //H=Parallel; L=Serial;
sbit RST = P0^1; //Reset Signal Low effective

void WriteRead_Lcd (uchar DATA_COMMAND,uchar Content);

uchar code Index[]=
{
"I :" //The first line
" - - - - - - " //The third line
" Einstein " //The second line
"----------------" //The fource line
};


/*-----Initialization-----*/
void Init_Lcd()
{
RST=1;
PSB=0;
WriteRead_Lcd(COMMAND,0x30); //Basic command action
WriteRead_Lcd(COMMAND,0x01); //Clear screen, address pointer points to 00H
DelayUs(100);
WriteRead_Lcd(COMMAND,0x06); //Moving direction of cursor
WriteRead_Lcd(COMMAND,0x0c); //Open display, close cursor
}
/*-----Clear DDRAM-----*/
void Clear_Ram ()
{
WriteRead_Lcd(COMMAND,0x30);
WriteRead_Lcd(COMMAND,0x01);
DelayUs(180);
}
/*-----Show characters on the whole screen-----*/
void Show_Screen(uchar *Content)
{
uchar i,j;
Clear_Ram();
WriteRead_Lcd(COMMAND,X1); //Specify the first address of DDRAM
for(j=0;j<4;j++)
for(i=0;i<16;i++)
WriteRead_Lcd(DATA,Content[j*16+i]);
}
/*-----Show characters on designated line-----*/
void Show_Line(uchar Line,uchar *Content)
{
uchar i;
//Clear_Ram();
WriteRead_Lcd(COMMAND,Line);
for(i=0;i<16;i++)
WriteRead_Lcd(DATA,Content[i]);
}
/*-----Write or read DDRAM-----*/
void WriteRead_Lcd(uchar DATA_COMMAND,uchar Content)
{
uchar a,i,j;
DelayUs(50);
a=Content;
CS=1;
SCLK=0;
STD=1;
for(i=0;i<5;i++)
{
SCLK=1;
SCLK=0;
}
STD=0;
SCLK=1;
SCLK=0;
if(DATA_COMMAND)
STD=1; //read from DDRAM - data
else
STD=0; //write - COMMAND
SCLK=1;
SCLK=0;
STD=0;
SCLK=1;
SCLK=0;
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{
a=a<<1;
STD=CY; //CY->Carry
SCLK=1;
SCLK=0;
}
STD=0;
for(i=0;i<4;i++)
{
SCLK=1;
SCLK=0;
}
}
}

void Change_Screen(uchar *Num)
{
uchar Changed_Index[]=
{
"I :"
" - - - - - - "
" Einstein "
"----------------"
};
Clear_Ram();
Changed_Index[18]=Num[0];
Changed_Index[20]=Num[1];
Changed_Index[22]=Num[2];
Changed_Index[25]=Num[3];
Changed_Index[27]=Num[4];
Changed_Index[29]=Num[5];
Show_Screen(Changed_Index);
}
void Change_Password(uchar *Num)
{
uchar Changed_Password[]=
{
"C :"
" - - - - - - "
" Einstein "
"----------------"
};
Clear_Ram();
Changed_Password[18]=Num[0];
Changed_Password[20]=Num[1];
Changed_Password[22]=Num[2];
Changed_Password[25]=Num[3];
Changed_Password[27]=Num[4];
Changed_Password[29]=Num[5];
Show_Screen(Changed_Password);
}
void Show_Menu()
{
Clear_Ram();
Show_Line(X1,"Menu ");
Show_Line(X2,"1:Reset A:Finger");
Show_Line(X3,"2:Alter B:GPS ");
Show_Line(X4," C:ReLock ");
}
void Show_Finger()
{
Clear_Ram();
Show_Line(X1,"Finger ");
Show_Line(X2," 1:Scan ");
Show_Line(X3," 2:Input ");
Show_Line(X4," C:Cancel ");
}
void Show_GPS()
{
Clear_Ram();
Show_Line(X1,"GPS ");
Show_Line(X2," 1:CurrentGPS ");
Show_Line(X3," 2:SetCurrent ");
Show_Line(X4," C:Cancel ");
}
void Show_LatLong()
{
uchar LatLong[]=
{
"Latitude: "
"Longitude: "
" "
" "
};
Clear_Ram();
LatLong[39]=Save_Data.latitude[0];
LatLong[40]=Save_Data.latitude[1];
LatLong[41]=Save_Data.latitude[2];
LatLong[42]=Save_Data.latitude[3];
LatLong[43]=Save_Data.latitude[4];
LatLong[44]=Save_Data.latitude[5];
LatLong[45]=Save_Data.latitude[6];
LatLong[46]=Save_Data.latitude[7];
LatLong[47]=Save_Data.latitude[8];
LatLong[54]=Save_Data.longitude[0];
LatLong[55]=Save_Data.longitude[1];
LatLong[56]=Save_Data.longitude[2];
LatLong[57]=Save_Data.longitude[3];
LatLong[58]=Save_Data.longitude[4];
LatLong[59]=Save_Data.longitude[5];
LatLong[60]=Save_Data.longitude[6];
LatLong[61]=Save_Data.longitude[7];
LatLong[62]=Save_Data.longitude[8];
LatLong[63]=Save_Data.longitude[9];
Show_Screen(LatLong);
}

#endif

DHT11.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef _DHT11_H_
#define _DHT11_H_

#include "Header.h"

sbit Data = P3^3;

void DHT11_start()
{
Data=1;
DHT11_delay_us(2);
Data=0;
DHT11_delay_ms(20);
Data=1;
DHT11_delay_us(30);
}

uchar DHT11_rec_byte()
{
uchar i,dat=0;
for(i=0;i<8;i++)
{
while(!Data);
DHT11_delay_us(8);
dat<<=1;
if(Data==1)
dat+=1;
while(Data);
}
return dat;
}

uint DHT11_receive()
{
uchar R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
DHT11_start();
if(Data==0)
{
while(Data==0);
DHT11_delay_us(40);
R_H=DHT11_rec_byte();
R_L=DHT11_rec_byte();
T_H=DHT11_rec_byte();
T_L=DHT11_rec_byte();
revise=DHT11_rec_byte();
DHT11_delay_us(25);

if((R_H+R_L+T_H+T_L)==revise)
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
}
RH='0'+(RH/10);
if(RH>'7')
return 1;
}
return 0;
}

#endif

JudgePassword.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifndef _JUDGEPASSWORD_H_
#define _JUDGEPASSWORD_H_

#include "Header.h"

sbit CloseLock = P1^6;
sbit OpenLock = P1^7;
sbit Relay = P3^1;

sbit F_Scan = P3^4; //Finger
sbit F_Input = P3^5;
sbit F_Success = P3^6;
sbit F_Fail = P3^7;

uchar Lat[]={0};
uchar Long[]={0};

/*-----Judge password from keyboard-----*/
void Judge_Password()
{
uint N=0;
uint i;
uchar Init_ROM_Flag[]={"0"};
uchar Password_ROM[]={"26262626"};
uchar Password_Input[]={"------"};
uint Right_Wrong=0;
uint Success_Flag=0;
uint N_Reset=0;

Read_ROM(Init_ROM_Flag,0,1);
if(Init_ROM_Flag[0]!='1')
{
Init_ROM_Flag[0]='1';
Write_ROM(Init_ROM_Flag,0,1);
Write_ROM(Password_ROM,1,6);
Clear_Ram();
Show_Line(X2," 00000000 ");
DelayMs(1000);
}
else
{
Read_ROM(Password_ROM,1,6);
Clear_Ram();
Show_Line(X2," 11111111 ");
DelayMs(1000);
}
Show_Screen(Index);

while(1)
{
while(Key_Check())
{
if(N<6&&(RowCol==0x11||RowCol==0x21||RowCol==0x41||RowCol==0x12||RowCol==0x22||RowCol==0x42||RowCol==0x14||RowCol==0x24||RowCol==0x44||RowCol==0x18||RowCol==0x28||RowCol==0x48))
{
Password_Input[N]=Coding_Key(RowCol);
P0=RowCol;
N++;
Change_Screen(Password_Input);
}
else if(RowCol==0x88&&N==6) //Got 6 bit number and the button has been pressed
{
Right_Wrong=1;
for(i=0;i<N;i++)
{
if(Password_Input[i]!=Password_ROM[i])
Right_Wrong=0;
}
if(Right_Wrong)
{
for(i=0;i<6;i++) //Clear up the value just entered
Password_Input[i]='-';
N=0;
Clear_Ram();
Show_Line(X2," Welcome: ");
Show_Line(X3," Einstein ");
OneAlarm();
CloseLock=0;
OpenLock=1;
Relay=0;
DelayMs(3000);
Show_Menu();

while(1)
{
if(DHT11_receive())
{
TwoAlarm();
goto Key_Cancel;
}
Key_Check();
if(RowCol==0x11) //Press 1 to reset the password
{
if(!Success_Flag) //If never reset
{
for(i=0;i<6;i=i+2)
Password_ROM[i]='2';
for(i=1;i<6;i=i+2)
Password_ROM[i]='6';
Write_ROM(Password_ROM,1,6);
Clear_Ram();
Show_Line(X2," Reset Success ");
Show_Line(X3," 2 6 2 6 2 6 ");
Success_Flag=1;
OneAlarm();
DelayMs(5000);
Show_Menu();

}
else if(Success_Flag) //If reset
{
Show_Menu();
//break;
}
}
else if(RowCol==0x21) //Press 2 to change the password
{
Success_Flag=0; //Set reset flag to unreset
Change_Password(Password_Input);
while(1)
{
Key_Check();
if(RowCol==0x11||RowCol==0x21||RowCol==0x41||RowCol==0x12||RowCol==0x22||RowCol==0x42||RowCol==0x14||RowCol==0x24||RowCol==0x44||RowCol==0x18||RowCol==0x28||RowCol==0x48)
{
Password_ROM[N_Reset]=Coding_Key(RowCol);
Password_Input[N_Reset]=Coding_Key(RowCol);
N_Reset++;
Change_Password(Password_Input);
}
else if(RowCol==0x88&&N_Reset==6)
{
Write_ROM(Password_ROM,1,6);
N_Reset=0;
Clear_Ram();
Show_Line(X2," Change Success "); //Change password success
OneAlarm();
DelayMs(2000);
Show_Menu();
break;
}
else if(RowCol==0x84)
{
N_Reset=0;
for(i=0;i<6;i++)
Password_Input[i]='-';
Show_Menu();
break;
}
}
}
else if(RowCol==0x81) //Press A to enter the fingerprint page
{
Show_Finger();
while(1)
{
if(DHT11_receive())
{
TwoAlarm();
goto Key_Cancel;
}
Key_Check();
if(RowCol==0x11)
{
Clear_Ram();
Show_Line(X2," Scanning ");
F_Scan=0;
DelayMs(200);
F_Scan=1;
while(F_Success && F_Fail);
OneAlarm();

if(!F_Success && F_Fail)
{
Clear_Ram();
Show_Line(X2," Success! ");
DelayMs(2000);
Clear_Ram();
Show_Finger();
}
else if(F_Success && !F_Fail)
{
Clear_Ram();
Show_Line(X2," Fail!! ");
DelayMs(2000);
Clear_Ram();
Show_Finger();

}
}
else if(RowCol==0x21)
{
Clear_Ram();
Show_Line(X2," InputF ");
F_Input=0;
DelayMs(200);
F_Input=1;
while(F_Success && F_Fail);
OneAlarm();

if(!F_Success && F_Fail)
{
Clear_Ram();
Show_Line(X2," Success! ");
DelayMs(2000);
Clear_Ram();
Show_Finger();
}
else if(F_Success && !F_Fail)
{
Clear_Ram();
Show_Line(X2," Fail!! ");
DelayMs(2000);
Clear_Ram();
Show_Finger();
}
}
else if(RowCol==0x84)
{
Show_Menu();
break;
}
}
}
else if(RowCol==0x82) //Press B to enter the GPS page
{
if(DHT11_receive())
{
TwoAlarm();
goto Key_Cancel;
}
Show_GPS();
while(1)
{
Key_Check();
if(RowCol==0x11)
{
Clear_Ram();
Show_LatLong();
}
if(RowCol==0x21)
{
Clear_Ram();
Show_Line(X2," 0000B2 ");
DelayMs(2000);
Clear_Ram();
Show_GPS();
}
else if(RowCol==0x84)
{
Show_Menu();
break;
}
}
}
else if(RowCol==0x84) //Press C to jump to the end C
goto Key_Cancel;
}
}
else
{
Clear_Ram();
Show_Line(X2,"Password Wrong!");
TwoAlarm();
DelayMs(3000);
for(i=0;i<6;i++)
Password_Input[i]='-';
N=0;
Clear_Ram();
Change_Screen(Password_Input);

}
}
else if(RowCol==0x84) //Press C to cancel input or reclock
{
Key_Cancel: for(i=0;i<6;i++)
Password_Input[i]='-';
N=0;
Clear_Ram();
Change_Screen(Password_Input);
CloseLock=1;
OpenLock=0;
Relay=1;
}
}
}
}

#endif

GPS.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef __GPS_H__
#define __GPS_H__

#include "Header.h"

void printGpsBuffer()
{
if (Save_Data.isParseData)
{
Save_Data.isParseData = 0;
if(Save_Data.isUsefull)
{
Save_Data.isUsefull = 0;
UartPrintf("Save_Data.latitude = ");
UartPrintf(Save_Data.latitude);
UartPrintf("\r\n");

UartPrintf("Save_Data.longitude = ");
UartPrintf(Save_Data.longitude);
UartPrintf("\r\n");

Save_Data.isShowData=1;
}
else
{
UartPrintf("GPS DATA is not usefull!\r\n");
}
}
}

void parseGpsBuffer()
{
char *subString;
char *subStringNext;
char i = 0;
if (Save_Data.isGetData)
{
Save_Data.isGetData = 0;
UartPrintf("**************\r\n");
UartPrintf(Save_Data.GPS_Buffer);

for (i = 0 ; i <= 6 ; i++)
{
if (i == 0)
{
if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)
errorLog(1); //Analysis wrong
}
else
{
subString++;
if ((subStringNext = strstr(subString, ",")) != NULL)
{
char usefullBuffer[2];
switch(i)
{
//case 1:memcpy(Save_Data.UTCTime, subString, subStringNext - subString);break; //Get UTC time
case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break; //Get status code
case 3:memcpy(Save_Data.latitude, subString, subStringNext - subString);break; //Get latitude
//case 4:memcpy(Save_Data.N_S, subString, subStringNext - subString);break; //Get N/S
case 5:memcpy(Save_Data.longitude, subString, subStringNext - subString);break; //Get longitude
//case 6:memcpy(Save_Data.E_W, subString, subStringNext - subString);break; //Get E/W
default:break;
}

subString = subStringNext;
Save_Data.isParseData = 1;
if(usefullBuffer[0] == 'A')
Save_Data.isUsefull = 1;
else if(usefullBuffer[0] == 'V')
Save_Data.isUsefull = 0;
}
else
{
errorLog(2); //Analysis wrong
}
}
}
}
}

#endif