-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraffic_light.c
More file actions
435 lines (383 loc) · 11.2 KB
/
Copy pathTraffic_light.c
File metadata and controls
435 lines (383 loc) · 11.2 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include<16f887.h>
#fuses intrc_io
#use delay (clock=8M)
//button
#define MODE pin_c0
#define RED pin_c1
#define ALL_RED pin_c2
#define GREEN pin_c3
//button
#define RIGHT14 pin_c4
#define LEFT14 pin_c5
#define RIGHT23 pin_c6
#define LEFT23 pin_c7
//led
#define RED14 pin_b0
#define YELLOW14 pin_b1
#define GREEN14 pin_b2
//led
#define RED23 pin_b3
#define YELLOW23 pin_b4
#define GREEN23 pin_b5
//led
#define TURN_RIGHT14 pin_b6
#define TURN_LEFT14 pin_b7
#define TURN_LEFT23 pin_e0
#define TURN_RIGHT23 pin_e1
//time
#define sec_red14 15
#define sec_yellow14 4
#define sec_green14 11
//time
#define sec_red23 15 //always equal to sec_green14 + sec_yellow14
#define sec_yellow23 4
#define sec_green23 6
//declare variable
const unsigned int8 m7d[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int16 bdn_tmr1=0, sec=0; //bdn: interrupt variable
int8 count_14=0, count_23=0, sel=0, light=0;
int1 right_14=0, left_14=0, right_23=0, left_23=0;
///////////////////////////////////////////////////////////////////////////////interrupt
#int_timer1
void interrupt_timer1()
{
bdn_tmr1++;
set_timer1(3036);
if(sec == sec_green14 + sec_yellow14 + sec_red14) sec=0;
}
///////////////////////////////////////////////////////////////////////////////select mode
void select_mode()
{
if(input(MODE) == 0)
{
delay_ms(20);
if(input(MODE) == 0)
{
if(sel < 2)
{
sel++;
output_low(GREEN14); output_low(YELLOW14); output_low(RED14);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_low(TURN_RIGHT14); output_low(TURN_RIGHT23);
output_low(GREEN23); output_low(YELLOW23); output_low(RED23);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_low(TURN_RIGHT14); output_low(TURN_RIGHT23);
}
else
{
sel=0;
}
while(input(MODE) == 0);
}
}
}
///////////////////////////////////////////////////////////////////////////////mode0
void traffic_light()
{
if (bdn_tmr1 >= 4)
{
bdn_tmr1 = 0;
sec++;
if (sec <= sec_green14) //GREEN14, RED23
{
output_high(GREEN14); output_low(YELLOW14); output_low(RED14);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
output_low(GREEN23); output_low(YELLOW23); output_high(RED23);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
count_14 = sec_green14 - sec;
count_23 = sec_red23 - sec;
}//sec=10
else if (sec <= sec_green14 + sec_yellow14) //YELLOW14, RED23
{
output_low(GREEN14); output_high(YELLOW14); output_low(RED14);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
output_low(GREEN23); output_low(YELLOW23); output_high(RED23);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
count_14 = sec_green14 + sec_yellow14 - sec;
count_23 = sec_red23 - sec;
}//sec=12
else if (sec <= sec_green14 + sec_yellow14 + sec_green23) //RED14, GREEN23 + TURN_LEFT23
{
output_low(GREEN14); output_low(YELLOW14); output_high(RED14);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
output_high(GREEN23); output_low(YELLOW23); output_low(RED23);
output_low(TURN_LEFT14); output_high(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
count_14 = sec_green14 + sec_yellow14 + sec_red14 - sec;
count_23 = sec_green14 + sec_yellow14 + sec_green23 - sec;
}//sec=18
else if (sec <= sec_green14 + sec_yellow14 + sec_green23 + sec_yellow23) //RED14, YELLOW23
{
output_low(GREEN14); output_low(YELLOW14); output_high(RED14);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
output_low(GREEN23); output_high(YELLOW23); output_low(RED23);
output_low(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
count_14 = sec_green14 + sec_yellow14 + sec_red14 - sec;
count_23 = sec_green14 + sec_yellow14 + sec_green23 + sec_yellow23 - sec;
}//sec=20
else if (sec <= sec_green14 + sec_yellow14 + sec_red14) //RED14, RED23, TURN_LEFT14
{
output_low(GREEN14); output_low(YELLOW14); output_high(RED14);
output_high(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
output_low(GREEN23); output_low(YELLOW23); output_high(RED23);
output_high(TURN_LEFT14); output_low(TURN_LEFT23); output_high(TURN_RIGHT14); output_high(TURN_RIGHT23);
count_14 = sec_green14 + sec_yellow14 + sec_red14 - sec;
count_23 = sec_green14 + sec_yellow14 + sec_red14 + sec_red23 - sec;
}
}
}
//-------------------------7seg area 1-----------------------------------------
void led7seg_1(int8 count, int1 ctrl1_0, int1 ctrl1_1)
{
int8 tens = count / 10;//hang chuc
int8 ones = count % 10;// hang don vi
output_a(m7d[tens]);
output_low(ctrl1_0);
delay_ms(1);
output_high(ctrl1_0);
output_a(m7d[ones]);
output_low(ctrl1_1);
delay_ms(1);
output_high(ctrl1_1);
}
//---------------------------7seg area 2---------------------------------------
void led7seg_2(int8 count, int1 ctrl2_0, int1 ctrl2_1)
{
int8 tens = count / 10;//hang chuc
int8 ones = count % 10;// hang don vi
output_a(m7d[tens]);
output_low(ctrl2_0);
delay_ms(1);
output_high(ctrl2_0);
output_a(m7d[ones]);
output_low(ctrl2_1);
delay_ms(1);
output_high(ctrl2_1);
}
//-----------------------------7seg area 3-------------------------------------
void led7seg_3(int8 count, int1 ctrl3_0, int1 ctrl3_1)
{
int8 tens = count / 10;//hang chuc
int8 ones = count % 10;// hang don vi
output_a(m7d[tens]);
output_low(ctrl3_0);
delay_ms(1);
output_high(ctrl3_0);
output_a(m7d[ones]);
output_low(ctrl3_1);
delay_ms(1);
output_high(ctrl3_1);
}
//---------------------------7seg area 4---------------------------------------
void led7seg_4(int8 count, int1 ctrl4_0, int1 ctrl4_1)
{
int8 tens = count / 10;//hang chuc
int8 ones = count % 10;// hang don vi
output_a(m7d[tens]);
output_low(ctrl4_0);
delay_ms(1);
output_high(ctrl4_0);
output_a(m7d[ones]);
output_low(ctrl4_1);
delay_ms(1);
output_high(ctrl4_1);
}
///////////////////////////////////////////////////////////////////////////////mode1
void adjust_mode()
{
//---------------------------BTN RED, GREEN, ALL-RED --------------------------
if(input(RED) == 0)
{
delay_ms(20);
if(input(RED) == 0)
{
light=1;
output_b(0);
while(input(RED) == 0);
}
}
if(input(GREEN) == 0)
{
delay_ms(20);
if(input(GREEN) == 0)
{
light=2;
output_b(0);
while(input(GREEN) == 0);
}
}
if(input(ALL_RED) == 0)
{
delay_ms(20);
if(input(ALL_RED) == 0)
{
light=3;
output_b(0);
while(input(ALL_RED) == 0);
}
}
switch(light)
{
case 1:
output_high(RED14);
output_high(GREEN23);
break;
case 2:
output_high(GREEN14);
output_high(RED23);
break;
case 3:
output_high(RED14);
output_high(RED23);
break;
}
//-----------------------------BTN RIGHT14, LEFT14-----------------------------
if(input(RIGHT14) == 0)
{
delay_ms(20);
if(input(RIGHT14) == 0)
{
if(right_14 < 1)
{
right_14++;
}
else
{
right_14=0;
}
while(input(RIGHT14) == 0);
}
}
switch(right_14)
{
case 0:
output_low(TURN_RIGHT14);
break;
case 1:
output_high(TURN_RIGHT14);
break;
}
if(input(LEFT14) == 0)
{
delay_ms(20);
if(input(LEFT14) == 0)
{
if(left_14 < 1)
{
left_14++;
}
else
{
left_14=0;
}
while(input(LEFT14) == 0);
}
}
switch(left_14)
{
case 0:
output_low(TURN_LEFT14);
break;
case 1:
output_high(TURN_LEFT14);
break;
}
//-----------------------------BTN RIGHT23, LEFT23-----------------------------
if(input(RIGHT23) == 0)
{
delay_ms(20);
if(input(RIGHT23) == 0)
{
if(right_23 < 1)
{
right_23++;
}
else
{
right_23=0;
}
while(input(RIGHT23) == 0);
}
}
switch(right_23)
{
case 0:
output_low(TURN_RIGHT23);
break;
case 1:
output_high(TURN_RIGHT23);
break;
}
if(input(LEFT23) == 0)
{
delay_ms(20);
if(input(LEFT23) == 0)
{
if(left_23 < 1)
{
left_23++;
}
else
{
left_23=0;
}
while(input(LEFT23) == 0);
}
}
switch(left_23)
{
case 0:
output_low(TURN_LEFT23);
break;
case 1:
output_high(TURN_LEFT23);
break;
}
}
///////////////////////////////////////////////////////////////////////////////mode2
void yellow_toggle()
{
if(bdn_tmr1 >=2 )
{
bdn_tmr1 = 0;
output_toggle(YELLOW14);
output_toggle(YELLOW23);
}
}
///////////////////////////////////////////////////////////////////////////////main
void main()
{
set_tris_a(0);//7seg
set_tris_b(0);//led
set_tris_d(0);//control
set_tris_e(0);//led
set_tris_c(0xff);//button
//timing 250ms
setup_timer_1(t1_internal | t1_div_by_8);
set_timer1(3036);
enable_interrupts(int_timer1);
enable_interrupts(global);
while(true)
{
select_mode();
if(sel == 0)
{
traffic_light();
led7seg_1(count_14,pin_d0,pin_d1);
led7seg_4(count_14,pin_d6,pin_d7);
led7seg_2(count_23,pin_d2,pin_d3);
led7seg_3(count_23,pin_d4,pin_d5);
}
else if(sel == 1)
{
//turn off 7seg
output_d(0xff);
output_a(0xff);
adjust_mode();
}
else if(sel == 2)
{
//turn off 7seg
output_d(0xff);
output_a(0xff);
yellow_toggle();
}
}
}