Traffic Control System EG6142
Traffic Control System EG6142
ENGINEERING
February 2016
3|Page
Table of Contents
1. Question Paper……………………………………..…………..….....................…...........5
2. Hardware Demonstrator....................................................................................................6
3. Task......................................................................................................................................7
4. Marking Criteria........………………...........……..................……………......…………..7
5. Summary............................….………………….....................…………………………...8
6. Introduction........................................................................................................................8
7. Specifications.................……………….....................……………………………………8
15. Code..........................................…………..........……......................………….……..…...20
16. Conclusion.........................................................................................................................30
17. Reference.....................................………………….….....................…………………....31
18. Bibliography............……………………..............……………………………................31
4|Page
School of Architecture,
Computing and Engineering
Student:
Module: DSP+Embedded Systems EG6142
Semester: A Year: 3
Lecturer: N. Snailum Academic Year: 2016/17
Assignment: Assignment 1 Submission Date:
Total 100
Student Comments
Lecturer Comments
Date:
5|Page
School of Architecture,
Computing and Engineering
Assignment 1 Specification
Tasks
6|Page
o Document the code carefully, and demonstrate it.
Marking Criteria: This assignment will be marked with consideration to the following:
The laboratorial hardware used for traffic light code to be run, has been shown in figure 2.
The vertical LEDs representing traffic flow on High Street. On other hand the horizontal
LEDs representing the traffic flow on Avenue. LED 14, 15 and 16 are traffic lights for
Avenue and LED 17, 18, 19 are traffic lights for High Street. LED 20 and 21 represents
traffic lights for Pedestrian Crossing.
7|Page
Summary:
The purpose of this assignment is to create a SEA table, State
Transition Diagram (STD) and Flow Chart, and then to build a programme code for traffic
lights. In this particular assignment, the implementation of the software design and working
principals of U.K traffic lights will be examined, studied and software will be built to display
a simulation of the traffic lights controlling two roads and a pedestrian crossing. The code
will be written in C language and run in PIC micro controller.
Introduction:
With the invention of automobile, where it made human’s life very easy,
also it became essential to have a proper and smooth system for automobile users. Accidents
on the roads and traffic jams were the two biggest problems faced by automobile users. To
overcome these problems traffic light’s idea was proposed. In order to reduce accidents and
fasten the flow of cars on the roads, traffic lights were designed to regulate and smooth the
traffic. Good care should be taken in consideration when writing software for traffic lights, a
small logic mistake can cause serious accident and/or heavy congestion on the roads.
This report will show the making of a SEA table, STD, Flow Chart and a programme code
for traffic lights.
This code will be implemented on microcontroller then. The microcontroller used for the
project in this assignment is PIC18F7J11. And it falls in Mid-Range 8-bit MCUs category
and has 20 pins with a flash memory of 4096 words.. The compiler used in the project to
programme the microchip is MPLABX and the hardware is Pickit3 (Ahmed, 2015).
This assignment is about a traffic lights. The assignment is to build the hardware with
PIC18F7J11 microchip. By using PIC18F7J11 microchip a hardware circuit to be built on
breadboard and the Pickit programme has to be used to download code in to PIC18F7J11
microcontroller.
Specifications:
Once the traffic lights board powered on, all lights on the board will turn on for 3 seconds.
After that the normal traffic light system will start work until the power is on. In the traffic
light system, first of all the Avenue green light will show up and red lights for High St and
Pedestrian crossing will show up. At this stage the traffic at avenue will flow smoothly for
8|Page
few seconds, in software this has been addressed by the completion of 4 loops of leds (on/off
in sequence) on Avenue. After traffic has finished flowing (4 loops complete), traffic light on
Avenue will turn amber and traffic will stop. On the same time red and amber lights on High
Street will turn on, all three lights will stay on for 2 seconds and then Avenue light will
change to red and High Street light will change to green.
If the Pedestrian button (RB0) is pressed while Avenue traffic is flowing, it will wait for the
Avenue traffic to complete its loop before the transition of lights occurs. Once the Avenue
traffic flow loop completed then the traffic light on Avenue will turn amber and traffic will
stop, it will stay amber for two seconds. On the same time Pedestrian crossing’s green light
will turn on, but no change for High Street light it will stay red.
After the green light on pedestrian crossing turns on, seven segment displays will start
displaying countdown on the screen. Pedestrians can start walking until countdown completes
and a warning ‘S’ shows on Seven Segment display to warn the pedestrians to stop walking.
The same procedure will repeat if pedestrian button (RB0) is pressed while High Street traffic
is flowing. The only difference is the lights changes, in this case the traffic light on High
Street will turn amber and traffic will stop, it will stay amber for two seconds. On the same
time Pedestrian crossing’s green light will turn on, but no change for Avenue light it will stay
red.
If the pedestrian button was pressed while Avenue traffic was flowing then, High Street
traffic will start, after the completion of the pedestrian countdown. And if the pedestrian
button was pressed while High Street traffic was flowing then, Avenue traffic will start, after
the completion of the pedestrian countdown.
If the pedestrian button was pressed while High Street traffic was flowing then after the
completion of pedestrian countdown and the warning sign of ‘S’ showed up, pedestrian’s red
light will turn on which will stay on. The Avenue’s amber and red light will also turn on
which will stay on for 2 second and then turn off and the green light on Avenue will show up
and the traffic on Avenue will to start. High Street light will stay red for this whole period.
Similar happens if the pedestrian button was pressed while Avenue traffic was flowing. The
only difference is the transition of traffic lights on High Street. Once it completes then traffic
on High Street will start. In this case Avenue’s red light will stay on for this whole period.
9|Page
State Event Action:
Designing a program or writing a code for software, before
starting writing any code it is important to think about and plan its structure. Therefore SEA
(State Event Action), STD (State Transition Diagram) and Flow Chart are the useful tools to
make the program easy to understand. It is good practice to spend some time and plan about
that program by writing S.E.A (State Event Action) table and Flow Chart in first stage. In this
report we need to write SEA, STD, and Flow Chart for Traffic Light System. First of all we
need a SEA table. A SEA table for this purpose can be seen in table 1. The whole program
has been divided in few states and according to that, events and actions are allocated to them.
10 | P a g e
2 seconds elapsed Go to High St
11 | P a g e
6 seconds elapsed ‘A’ displays on Seven Segment
7 seconds elapsed ‘9’ displays on Seven Segment
8 seconds elapsed ‘8’ displays on Seven Segment
9 seconds elapsed ‘7’ displays on Seven Segment
10 seconds elapsed ‘6’ displays on Seven Segment
11 seconds elapsed ‘5’ displays on Seven Segment
12 seconds elapsed ‘4’ displays on Seven Segment
12 | P a g e
6 seconds elapsed ‘A’ displays on Seven Segment
7 seconds elapsed ‘9’ displays on Seven Segment
8 seconds elapsed ‘8’ displays on Seven Segment
9 seconds elapsed ‘7’ displays on Seven Segment
10 seconds elapsed ‘6’ displays on Seven Segment
11 seconds elapsed ‘5’ displays on Seven Segment
12 seconds elapsed ‘4’ displays on Seven Segment
13 seconds elapsed ‘3’ displays on Seven Segment
14 seconds elapsed ‘2’ displays on Seven Segment
15 seconds elapsed ‘1’ displays on Seven Segment
16 seconds elapsed ‘0’ displays on Seven Segment
17 seconds elapsed ‘S’ displays on Seven Segment
18 seconds elapsed Seven Segment display off
19 seconds elapsed ‘S’ displays on Seven Segment
20 seconds elapsed Go to Pedestrian 2 Stop
Pedestrian 2 Stop Pedestrian red on
Avenue amber & red on
High St red on
2 seconds elapsed Go to Avenue
13 | P a g e
Power on
Go to Initialise High St Traffic Flow Stop
Initialise
All Leds ON
4 loops elapsed
5 seconds elapsed 2 seconds elapsed
Go to High St Stop
Go to Avenue ON Go to Avenue ON
Avenue Traffic
High St Traffic
Flow ON 4 loops elapsed 2 seconds elapsed
Avenue Traffic Flow ON
Go to Ave Stop Flow Stop Go to High St ON
Go to Pedestrian 1
Pedestrian 2 ON
Pedestrian 1 ON
Countdown Start
Countdown Start
2 seconds elapsed
Go to High St ON
Countdown Stopped
2 seconds elapsed
Countdown Stopped
Go to Pedestrian 2 Stop
Go to Avenue ON
Go to Pedestrian 1 Stop
Figure 1:
3 State Transition Diagram for Traffic Lights
14 | P a g e
Full Traffic Light Flow Chart: In order to avoid hassle Flow Charts are created to give a better
understanding of the programme without reading the code and it also helps the client or other people to
understand who are not able to understand programming language. The Flow chart of the game is as follows.
YES
NO
NO YES
15 | P a g e
Avenue ON & Stopping Flow Chart
NO
YES
NO YES
16 | P a g e
High St ON & Stopping Flow Chart
NO
YES
NO YES
17 | P a g e
Pedestrian 1 Crossing & Stopping Flow Chart
NO
YES
18 | P a g e
Pedestrian 2 Crossing & Stopping Flow Chart
NO
YES
19 | P a g e
Code:
And the final outcome of the whole assignment is this precious code which took a lot of
value able time and effort to be written down in this form. This is a complete code and tested on
the traffic light hardware in laboratory. All the functionalities in this code have been explained in
comment’s section.
/*
* File: main.c
* Author: Naseer Ahmed
*
* Demonstrates some of the traffic board features
* version H
* 06/02/ 2017
*/
#include <stdio.h>
#include <stdlib.h>
#include <p18f87j11.h>
#include <xc.h>
#include "config.h"
#include "lcdE.h"
#include "SPI_trafficE.h"
void Initialise(void)
{
// some of this not required for demo
// configure ADC
WDTCONbits.ADSHR = 1;
ANCON0 = 0xFF;
ANCON1 = 0xFE; //make RA0 analog input
WDTCONbits.ADSHR = 0;
ADCON0 = 0;
ADCON0bits.ADON = 1;
ADCON1 = 0b00111010;
//configure LCD
20 | P a g e
LCDInit();
InitSPI();
//initialise SPI(traffic)
SPI1A_data = 0x00; //all on for testing
SPI1B_data = 0x00; //all on for testing
InitPortA_SPI1();
InitPortB_SPI1();
WritePortA_SPI1(SPI1A_data);
WritePortB_SPI1(SPI1B_data);
ADRESH = 0;
//variables
int secs;
int button2;
int Button1;
int WRONGPIN;
unsigned short long i;
char d[] = " Avenue Flow ON ";
char b[] = " High St Flow ON ";
char d1[] = "Avenue Flow STOP ";
char b1[] = "HighSt Flow Stop ";
char P[] = "Pedestrian Walk ";
char P1[] = "Pedestrian Stop ";
21 | P a g e
char test[] = "No Smocking.... ";
char syst[] = "U.K Light System ";
void main(void){
Initialise();
LCDClear();
count = 0;
while (1)
{
PORTD = count;
count++;
Delay10KTCYx(20); // delay
switch (simpleState)
{
22 | P a g e
SPI1A_data = 0xFF; // LED 10 (necessary to show
the flow of traffic)
WritePortA_SPI1(SPI1A_data);
SPI1B_data = 0xF0; // LED 1,2,3,4 (necessary to
show the flow of traffic)
WritePortB_SPI1(SPI1B_data);
Delay10KTCYx(100);
SPI1B_data = 0xFF; // LED 04 OFF (necessary to
show the flow of traffic)
WritePortB_SPI1(SPI1B_data);
SPI1B_data = 0xF8; // LED 1,2,3 (necessary to
show the flow of traffic)
WritePortB_SPI1(SPI1B_data);
SPI1A_data = 0xF7; // LED 11 (Indicates flow of
Traffic)
WritePortA_SPI1(SPI1A_data);
Delay10KTCYx(100);
SPI1A_data = 0xEF; // LED 12 (Indicates flow of
Traffic)
WritePortA_SPI1(SPI1A_data);
Delay10KTCYx(100);
SPI1A_data = 0xDF; // led 13 (Indicates flow of
Traffic)
WritePortA_SPI1(SPI1A_data);
Delay10KTCYx(100);
} //end of for
break;
LCDClear();
for (i = 0; i<16; i++)
d_write(d1[i]);
//PORTD = 0x8c;
SPI2A_data = 0x92; // Show 'S' on Seven Segment display
for Stop
WritePortA_SPI2(SPI2A_data);
23 | P a g e
WritePortA_SPI1(SPI1A_data);
WritePortB_SPI1(SPI1B_data);
Delay10KTCYx(100); //delay
Delay10KTCYx(100); //delay
}
break;
// PORTD = 0x86;
SPI2A_data = 0x89; // 'H'
WritePortA_SPI2(SPI2A_data); // show ‘H’ on Seven Segment
24 | P a g e
WritePortB_SPI1(SPI1B_data);
Delay10KTCYx(80);
SPI1B_data = 0xbf; // LED 07 (Indicates flow of
Traffic on High Street)
WritePortB_SPI1(SPI1B_data);
Delay10KTCYx(80);
break;
Delay10KTCYx(100);
if (count3>3)
{
simpleState = Avenue;
}
break;
25 | P a g e
LCDClear(); //LCD = clear
for (i = 0; i<16; i++)
d_write(P[i]); //LCD = Pedestrian Walk
26 | P a g e
SPI2A_data = 0xC0; //shows '0' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0x92; //shows 'S' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0xFF; // Seven Segment = OFF
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(100);
SPI2A_data = 0x92; //shows 'S' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0xFF; // Seven Segment = OFF
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(100);
SPI2A_data = 0x92; //shows 'S' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0xFF; // 'Seven Segment = OFF'
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(100);
SPI2A_data = 0x92; //shows 'S' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
break;
Delay10KTCYx(100);
if (count3>3)
{
27 | P a g e
simpleState = HighST;
}
break;
PORTD = 0x20;
SPI2B_data = 0xAE; // LED 14,18,20 (High St = amber, Avenue = red)
WritePortB_SPI2(SPI2B_data);
Delay10KTCYx(225);
SPI2B_data = 0x76; // 14,17,21 (Pedestrian = green, rest = red)
WritePortB_SPI2(SPI2B_data);
Delay10KTCYx(225);
28 | P a g e
Delay10KTCYx(1000);
SPI2A_data = 0xF9; //shows '1' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0xC0; //shows '0' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0x92; //shows 'S' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0xFF; // Seven Segment = OFF
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(100);
SPI2A_data = 0x92; //shows 'S' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0xFF; // Seven Segment = OFF
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(100);
SPI2A_data = 0x92; //shows 'S' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
SPI2A_data = 0xFF; // 'Seven Segment = OFF'
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(100);
SPI2A_data = 0x92; //shows 'S' on Seven Segment
WritePortA_SPI2(SPI2A_data);
Delay10KTCYx(1000);
break;
case PedestrianStop2:
PORTD = 0x88;
Delay10KTCYx(100);
29 | P a g e
}
if (count3>3)
{
simpleState = Avenue;
}
break;
Conclusion:
The entire task has been completed and the following have been concluded from
this assignment. It has been observed that when creating State Event Action (SEA) table one
should take small event into consideration which results in any action because those actions then
results in change of states. It is very important to identify all the states in the start as without
having knowledge of all states it would be extremely difficult to create events and their actions.
All the events and their resultant actions have been noted in SEA table
State Transition Diagram (STD) is the pictorial view of SEA table. SEA table must be finished
before starting work on State Transition Diagram; SEA table is a kind of protection pre-step
before starting work on STD, without pre-step things can get worse. One more thing was
concluded that it is quite difficult or say impossible to include all the events and actions in State
Transition Diagram, so a reader should be aware to consider State Event Action table for full
information on events and actions. And consider State Transition Diagram to examine the core
events which makes transition of one state to another state
Flow Chart is the most important part of any program writing, Flow chart gives full information
of different stages occurs in the making of a program. A nice flow chart is extremely beneficial
not only for the programmer who is writing the program but also for the non technical client who
might not have any technical knowledge of coding. In this assignment it was noticed that the
flow chart made life easy for programmer, it is essential to have a good flow chart ready which
can make it easy to write a complete program. And for those clients who are unable to read the
code can easily get a pictorial view of the program functionality from Flow Chart.
30 | P a g e
Reference:
Inc, F.D. (2015) LabVolt series by Festo didactic. Available at: http://www.labvolt.com/
(Accessed: 5 February 2017).
31 | P a g e