📄 intruder.lst
字号:
C51 COMPILER V6.21 INTRUDER 01/23/2002 18:10:47 PAGE 1
C51 COMPILER V6.21, COMPILATION OF MODULE INTRUDER
OBJECT MODULE PLACED IN Intruder.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE Intruder.c OPTIMIZE(6,SPEED) BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*------------------------------------------------------------------*-
2
3 Intruder.C (v1.00)
4
5 ------------------------------------------------------------------
6
7 Multi-state framework for intruder alarm system.
8
9 COPYRIGHT
10 ---------
11
12 This code is associated with the book:
13
14 EMBEDDED C by Michael J. Pont
15 [Pearson Education, 2002: ISBN: 0-201-79523-X].
16
17 This code is copyright (c) 2001 by Michael J. Pont.
18
19 See book for copyright details and other information.
20
21 -*------------------------------------------------------------------*/
22
23 #include "Main.H"
24 #include "Port.H"
25
26 #include "Intruder.H"
27
28 #include "Keypad.h"
29 #include "PC_O.h"
30
31 // ------ Private data type declarations ---------------------------
32
33 // Possible system states
34 typedef
35 enum {DISARMED, ARMING, ARMED, DISARMING, INTRUDER} eSystem_state;
36
37 // ------ Private function prototypes ------------------------------
38
39 bit INTRUDER_Get_Password_G(void);
40 bit INTRUDER_Check_Window_Sensors(void);
41 bit INTRUDER_Check_Door_Sensor(void);
42 void INTRUDER_Sound_Alarm(void);
43
44 // ------ Private variables ----------------------------------------
45
46 static eSystem_state System_state_G;
47
48 tWord State_call_count_G;
49
50 char Input_G[4] = {'X','X','X','X'};
51 char Password_G[4] = {'1','2','3','4'};
52
53 tByte Position_G;
54
55 bit New_state_G = 0;
C51 COMPILER V6.21 INTRUDER 01/23/2002 18:10:47 PAGE 2
56
57 bit Alarm_bit = 0;
58
59 /* --------------------------------------------------------------- */
60 void INTRUDER_Init(void)
61 {
62 1 // Set the initial system state (DISARMED)
63 1 System_state_G = DISARMED;
64 1
65 1 // Set the 'time in state' variable to 0
66 1 State_call_count_G = 0;
67 1
68 1 // Clear the keypad buffer
69 1 KEYPAD_Clear_Buffer();
70 1
71 1 // Set the 'New state' flag
72 1 New_state_G = 1;
73 1
74 1 // Set the (two) sensor pins to 'read' mode
75 1 Window_sensor_pin = 1;
76 1 Sounder_pin = 1;
77 1 }
78
79
80 /* --------------------------------------------------------------- */
81 void INTRUDER_Update(void)
82 {
83 1 // Incremented every time
84 1 if (State_call_count_G < 65534)
85 1 {
86 2 State_call_count_G++;
87 2 }
88 1
89 1 // Call every 50 ms
90 1 switch (System_state_G)
91 1 {
92 2 case DISARMED:
93 2 {
94 3 if (New_state_G)
95 3 {
96 4 PC_LINK_O_Write_String_To_Buffer("\nDisarmed");
97 4 New_state_G = 0;
98 4 }
99 3
100 3 // Make sure alarm is switched off
101 3 Sounder_pin = 1;
102 3
103 3 // Wait for correct password ...
104 3 if (INTRUDER_Get_Password_G() == 1)
105 3 {
106 4 System_state_G = ARMING;
107 4 New_state_G = 1;
108 4 State_call_count_G = 0;
109 4 break;
110 4 }
111 3
112 3 break;
113 3 }
114 2
115 2 case ARMING:
116 2 {
117 3 if (New_state_G)
C51 COMPILER V6.21 INTRUDER 01/23/2002 18:10:47 PAGE 3
118 3 {
119 4 PC_LINK_O_Write_String_To_Buffer("\nArming...");
120 4 New_state_G = 0;
121 4 }
122 3
123 3 // Remain here for 60 seconds (50 ms tick assumed)
124 3 if (++State_call_count_G > 1200)
125 3 {
126 4 System_state_G = ARMED;
127 4 New_state_G = 1;
128 4 State_call_count_G = 0;
129 4 break;
130 4 }
131 3
132 3 break;
133 3 }
134 2
135 2 case ARMED:
136 2 {
137 3 if (New_state_G)
138 3 {
139 4 PC_LINK_O_Write_String_To_Buffer("\nArmed");
140 4 New_state_G = 0;
141 4 }
142 3
143 3 // First, check the window sensors
144 3 if (INTRUDER_Check_Window_Sensors() == 1)
145 3 {
146 4 // An intruder detected
147 4 System_state_G = INTRUDER;
148 4 New_state_G = 1;
149 4 State_call_count_G = 0;
150 4 break;
151 4 }
152 3
153 3 // Next, check the door sensors
154 3 if (INTRUDER_Check_Door_Sensor() == 1)
155 3 {
156 4 // May be authorised user - go to 'Disarming' state
157 4 System_state_G = DISARMING;
158 4 New_state_G = 1;
159 4 State_call_count_G = 0;
160 4 break;
161 4 }
162 3
163 3 // Finally, check for correct password
164 3 if (INTRUDER_Get_Password_G() == 1)
165 3 {
166 4 System_state_G = DISARMED;
167 4 New_state_G = 1;
168 4 State_call_count_G = 0;
169 4 break;
170 4 }
171 3
172 3 break;
173 3 }
174 2
175 2 case DISARMING:
176 2 {
177 3 if (New_state_G)
178 3 {
179 4 PC_LINK_O_Write_String_To_Buffer("\nDisarming...");
C51 COMPILER V6.21 INTRUDER 01/23/2002 18:10:47 PAGE 4
180 4 New_state_G = 0;
181 4 }
182 3
183 3 // Remain here for 60 seconds (50 ms tick assumed)
184 3 // to allow user to enter the password
185 3 // - after time up, sound alarm
186 3 if (++State_call_count_G > 1200)
187 3 {
188 4 System_state_G = INTRUDER;
189 4 New_state_G = 1;
190 4 State_call_count_G = 0;
191 4 break;
192 4 }
193 3
194 3 // Still need to check the window sensors
195 3 if (INTRUDER_Check_Window_Sensors() == 1)
196 3 {
197 4 // An intruder detected
198 4 System_state_G = INTRUDER;
199 4 New_state_G = 1;
200 4 State_call_count_G = 0;
201 4 break;
202 4 }
203 3
204 3 // Finally, check for correct password
205 3 if (INTRUDER_Get_Password_G() == 1)
206 3 {
207 4 System_state_G = DISARMED;
208 4 New_state_G = 1;
209 4 State_call_count_G = 0;
210 4 break;
211 4 }
212 3
213 3 break;
214 3 }
215 2
216 2 case INTRUDER:
217 2 {
218 3 if (New_state_G)
219 3 {
220 4 PC_LINK_O_Write_String_To_Buffer("\n** INTRUDER! **");
221 4 New_state_G = 0;
222 4 }
223 3
224 3 // Sound the alarm!
225 3 INTRUDER_Sound_Alarm();
226 3
227 3 // Keep sounding alarm until we get correct password
228 3 if (INTRUDER_Get_Password_G() == 1)
229 3 {
230 4 System_state_G = DISARMED;
231 4 New_state_G = 1;
232 4 State_call_count_G = 0;
233 4 }
234 3
235 3 break;
236 3 }
237 2 }
238 1 }
239
240 /* --------------------------------------------------------------- */
241
C51 COMPILER V6.21 INTRUDER 01/23/2002 18:10:47 PAGE 5
242 bit INTRUDER_Get_Password_G(void)
243 {
244 1 signed char Key;
245 1 tByte Password_G_count = 0;
246 1 tByte i;
247 1
248 1 // Update the keypad buffer
249 1 KEYPAD_Update();
250 1
251 1 // Are there any new data in the keypad buffer?
252 1 if (KEYPAD_Get_Data_From_Buffer(&Key) == 0)
253 1 {
254 2 // No new data - password can't be correct
255 2 return 0;
256 2 }
257 1
258 1 // If we are here, a key has been pressed
259 1
260 1 // How long since last key was pressed?
261 1 // Must be pressed within 50 seconds (assume 50 ms 'tick')
262 1 if (State_call_count_G > 1000)
263 1 {
264 2 // More than 50 seconds since last key
265 2 // - restart the input process
266 2 State_call_count_G = 0;
267 2 Position_G = 0;
268 2 }
269 1
270 1 if (Position_G == 0)
271 1 {
272 2 PC_LINK_O_Write_Char_To_Buffer('\n');
273 2 }
274 1
275 1 PC_LINK_O_Write_Char_To_Buffer(Key);
276 1
277 1 Input_G[Position_G] = Key;
278 1
279 1 // Have we got four numbers?
280 1 if ((++Position_G) == 4)
281 1 {
282 2 Position_G = 0;
283 2 Password_G_count = 0;
284 2
285 2 // Check the password
286 2 for (i = 0; i < 4; i++)
287 2 {
288 3 if (Input_G[i] == Password_G[i])
289 3 {
290 4 Password_G_count++;
291 4 }
292 3 }
293 2 }
294 1
295 1 if (Password_G_count == 4)
296 1 {
297 2 // Password correct
298 2 return 1;
299 2 }
300 1 else
301 1 {
302 2 // Password NOT correct
303 2 return 0;
C51 COMPILER V6.21 INTRUDER 01/23/2002 18:10:47 PAGE 6
304 2 }
305 1 }
306
307 /* --------------------------------------------------------------- */
308
309 bit INTRUDER_Check_Window_Sensors(void)
310 {
311 1 // Just a single window 'sensor' here
312 1 // - easily extended
313 1 if (Window_sensor_pin == 0)
314 1 {
315 2 // Intruder detected...
316 2 PC_LINK_O_Write_String_To_Buffer("\nWindow damaged");
317 2 return 1;
318 2 }
319 1
320 1 // Default
321 1 return 0;
322 1 }
323
324 /* --------------------------------------------------------------- */
325
326 bit INTRUDER_Check_Door_Sensor(void)
327 {
328 1 // Single door sensor (access route)
329 1 if (Door_sensor_pin == 0)
330 1 {
331 2 // Someone has opened the door...
332 2 PC_LINK_O_Write_String_To_Buffer("\nDoor open");
333 2 return 1;
334 2 }
335 1
336 1 // Default
337 1 return 0;
338 1 }
339
340 /* --------------------------------------------------------------- */
341
342 void INTRUDER_Sound_Alarm(void)
343 {
344 1 if (Alarm_bit)
345 1 {
346 2 // Alarm connected to this pin
347 2 Sounder_pin = 0;
348 2 Alarm_bit = 0;
349 2 }
350 1 else
351 1 {
352 2 Sounder_pin = 1;
353 2 Alarm_bit = 1;
354 2 }
355 1 }
356
357 /*------------------------------------------------------------------*-
358 ---- END OF FILE -------------------------------------------------
359 -*------------------------------------------------------------------*/
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 461 ----
CONSTANT SIZE = 86 ----
XDATA SIZE = ---- ----
C51 COMPILER V6.21 INTRUDER 01/23/2002 18:10:47 PAGE 7
PDATA SIZE = ---- ----
DATA SIZE = 12 2
IDATA SIZE = ---- ----
BIT SIZE = 2 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -