0%

Pedestrian Lights Simulation using MCU8051

The following content is the lab assignment report for course WIX1003 - Computer Systems & Organization, Universiti Malaya, in which we are required to do a Pedestrian Light Simulation using MCU8051.

The PDF version report can be found here. Or you can view the report in in the bottom of this page by clicking here.

Pedestrian Lights Simulation using MCU8051

Component connection diagram

diagram

Complete code of system with an explanation on the operation

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
;ped lights	
ORG 00h
AJMP MAIN

MAIN: MOV A,#00h ;D-GREEN B-RED E-YELLOW
MOV P0,A ;set P0 as output for North (p0.4-p0.7) and West (p0.0-p0.3)
MOV P1,A ;set p1 as output for South (p1.4-p1.7) and East (p1.0-p1.3)
MOV P2,A ;set p2 as output for segment countdown
MOV DPTR,#SEG ;move table address to Data Pointer

START: MOV A,#0FFh ;D-GREEN B-RED E-YELLOW
MOV P0,A
MOV P1,A
MOV A,#00h
MOV P2,A
AJMP STATE1

;State 1 - State when North is not red
STATE1: MOV A,#0DBh
MOV P0,A ;North->Green West->Red
MOV A,#0BBh
MOV P1,A ;South->Red East->Red
MOV B,#009h ;set time for countdown
ACALL COUNT ;call a 9-second countdown for North (Green)
;
MOV A,#0EBh
MOV P0,A ;North->Yellow West->Red
MOV B,#003h ;set time for countdown
ACALL COUNT ;call a 3-second countdown for North (Yellow)
SJMP STATE2 ;jump to next state STATE2

;State 2 - State when West is not red
STATE2: MOV A,#0BDh
MOV P0,A ;North->Red West->Green
MOV A,#0BBh
MOV P1,A ;South->Red East->Red
MOV B,#009h ;set time for countdown
ACALL COUNT ;call a 9-second countdown for West (Green)

MOV A,#0BEh
MOV P0,A ;North->Red West->Yellow
MOV B,#003h ;set time for countdown
ACALL COUNT ;call a 3-second countdown for West (Yellow)
SJMP STATE3 ;jump to next state STATE3

;State 3 - State when South is not red
STATE3: MOV A,#0BBh
MOV P0,A ;North->Red West->Red
MOV A,#0DBh
MOV P1,A ;South->Green East->Red
MOV B,#009h ;set time for countdown
ACALL COUNT ;call a 9-second countdown for South (Green)

MOV A,#0EBh
MOV P1,A ;South->Yellow East->Red
MOV B,#003h ;set time for countdown
ACALL COUNT ;call a 3-second countdown for South (Yellow)
SJMP STATE4 ;jump to next state STATE4

;State 4 - State when East is not red
STATE4: MOV A,#0BBh
MOV P0,A ;North->Red West->Red
MOV A,#0BDh
MOV P1,A ;South->Red East->Green
MOV B,#009h ;set time for countdown
ACALL COUNT ;call a 9-second countdown for South (Green)
;
MOV A,#0BEh
MOV P1,A ;South->Red East->Yellow
MOV B,#003h ;set time for countdown
ACALL COUNT ;call a 3-second countdown for East (Yellow)
SJMP STATE1 ;jump to next state STATE1

;subroutine to count down
COUNT: MOV A,B ;move timeset from B to A
JZ RETURN ;A not 0, continue; else return
MOVC A,@A+DPTR ;load value from table
MOV P2,A ;to display the segment number
ACALL DELAY ;call a one-second delay
DEC B ;decrease B by 1
SJMP COUNT ;jump to count to display the next number

RETURN: RET

;subroutine to delay
DELAY: MOV R1,#001h
DELAY1: MOV R2,#0Fh
DELAY2: DJNZ R2,DELAY2
DJNZ R1,DELAY1
RET

;lookup table for 7-segment display pattern from 0-9
SEG: DB 3Fh,06h,5Bh,4Fh,66h,6Dh,7Dh,07h,7Fh,6Fh

END


Design Consideration

Analyzing

To make it easier to discuss, we name the lights by their position. North, West, South, and East.

In analyzing the requirements, we find that among four lights, there are always 3 red lights and 1 green/yellow light so we divide its process into 4 states.

State1: North changes from green to yellow. West, South, and East are red.

State2: West changes from green to yellow. North, South, and East are red.

State3: South changes from green to yellow. North, West, and East are red.

State4: East changes from green to yellow. North, West, and South are red.

In the next state after state4, it comes back to state1.

Displaying lights

Every light has three colors, so we use one digit from port to represent each color, and use 1/0 to represent if they are on or off now.

For each port has 2 digits in hexadecimal, we believe it will be clearer if we use one hexadecimal digit of each port to represent one light. And we intentionally leave one bit of the digit invalid.

We assign p0 to North and West, p1 to South and East in the following order

– p0.7 North - Invalid

– p0.6 North - Red

– p0.5 North - Green

– p0.4 North - Yellow

– p0.3 West - Invalid

– p0.2 West - Red

– p0.1 West - Green

– p0.0 West - Yellow

– p1.7 South - Invalid

– p1.6 South - Red

– p1.5 South - Green

– p1.4 South - Yellow

– p1.3 East - Invalid

– p1.2 East - Red

– p1.1 East - Green

– p1.0 East - Yellow

For the display of hexadecimal digits.

B (or 1011) means the light this hexadecimal digit represents is displaying Red.

D (or 1101) means the light this hexadecimal digit represents is displaying Green.

E (or 1110) means the light this hexadecimal digit represents is displaying Yellow.

Displaying countdown digits

We store 7-segment display pattern from 0-9 to the lookup table and use a subroutine to perform countdown. We use port 2 to display 7-segment digit.

Before calling countdown subroutine, we need to assign the countdown time to B.

In the subroutine, we first move B to A and check if A equals to 0, if not, we load value from the lookup table, display the segment number, call a one-second delay, decrease B by one and SJMP to countdown subroutine. If A equals 0, it will return.

Coding

In Main, we set p0, p1, p2 as output and move table address to Data Pointer.

In Start, we reset the lights and 7-segment display.

After start, we straightly go to state1.

Inside each state, we will first set the state of 4 lights, one green three red, and then we assign 9 to B and call the count to execute the countdown of green light from 9 to 1. Then we set the green light to yellow, then we assign 3 to B and call the count to execute the countdown of yellow light from 3 to 1.

In the end of each state, we SJMP to next state.

System Limitation

​ i. The countdown can only display 1- digit number

  1.   The system doesn’t display count down number for all 4 lights, instead, it counts down for the one light of all four lights which is not red.
  2.   To change the time duration of red, green, or yellow light, we need to change in four different places.

Report