Digital Electronics Assignment: Traffic Light controller
Subject code: DEL216D
Group#: 27
Assignment supervisor: JJ Spies
Surname &           Student          Signature
initials            number
Mafu T.S            221377435
Moyo M.J            223930760
Ngwenya T           220720845
Pule R.T.E          224859376
 Digital Electronics Assignment: Traffic Light controller.................................1
 Introduction...................................................................................................2
 Objectives.....................................................................................................2
 Material used................................................................................................3
 Procedure(PIC sim IDE).................................................................................4
 Procedure (Proteus)......................................................................................6
 Calculations..................................................................................................7
 Flowchart: Short delay..................................................................................8
 Flowchart: Long delay...................................................................................9
 Flowchart : main traffic light system controller...........................................10
 PROTEUS (schematic diagram)...................................................................16
 Observations...............................................................................................20
 Challenges..................................................................................................21
 Conclusion...................................................................................................21
 References..................................................................................................22
Introduction
Look to the left, look to the right then look to the left again. A
monotonous song that is sung by our teachers and parents before
you know how to cross the road as a pedestrian. A different tune
is sung for the motorists and their passengers when they are
operating the roads. The universal, red means stop, amber means
get ready, but do not proceed and green means. The evolution of
traffic lights has been astonishing to witness with an introduction
to the passenger button to make the road safer and better for
both pedestrians and motorists on the road.
Objectives
 Design and implement a traffic light controller using a
  PIC16F627 microcontroller.
 Make the traffic controller alternate between three states
  continuously, which is red, amber and green using LED's
 Understand and implement a time delay function for each of
  the given states according to the specifications given.
 Implement an interrupter function for the pedestrian button
  using a pushbutton connected to one of the GPIO pins
 Design a schematic diagram(A proteus circuit) that
  implements the above functionality using PIC16F276
  microcontroller
 Write a well commented code in assembler language the PIC
  sim IDE, to ensure that the microcontroller implements the
  forementioned functions.
 Simulate my code thoroughly using PIC sim IDE, to ensure
  that it meets all the specifications mentioned above
Material used
  1.   PIC sim IDE
  2.   Proteus
  3.   2 red LED
  4.   1 Yellow LED
  5.   2 Green LED
  6.   Connect wires
  7.   Push switch.
  8.   Proteus
Procedure(PIC sim IDE)
Procedure of the PIC:
1 Setup the bank or port
We moved from bank 0 to bank 1 using the status register and set
the first bit to 1. We loaded our input and output into a tris which
is TRISB. Our outputs are RBO, RB1, RB2, RB3, RB4 and my inputs
are RB5, RB6, RB7, after loading our inputs and outputs l cleared
the bit to return to bank 0 by using bit clear file.
2 Main
   We turned the red Led for cars to be by loading the 8bits
    (000010010) and set the pedestrian red using the above-
    mentioned byte loaded to port b since in the main we deal
    with the port.
   We called a ten second delay before the yellow LED turned
    on. We named the delay as "long_delay" .
   We turned the yellow LED to be on after ten seconds by
    moving and loading 8bit (000001010) and set the pedestrian
    red using the above-mentioned byte. We loaded to port b
    since in the main we deal with the port which is RB1 and RB3
    as my outputs.
   We called the delay of three seconds before the green led
    turn on, we named the delay as "short_delay".
   We turned the green LED to be on after the three seconds
    delay by moving the and loading 8bit (00001100) and turned
    the red led for pedestrians to be on to port b. Which is RB2
    and RB3.
   We called a ten second delay before it can turn on the yellow
    LED and we named this delay, ' long_delay'.
   We jumped to another code using go to real for it to check
    whether the button is pressed or not by the pedestrian as to
    interrupt the traffic light for the vehicles. It will go to main to
    repeat the same sequence until the button is pressed.
  
Real
We must test whether the button is pressed on or not. We will use
the bit test file and skip if set which will test whether the bit is one
symbolling whether the button is pressed or not. If the button is
pressed, skip the code line and execute the third line, which will
go to ped.
Ped
   It will turn the green led for pedestrians which is RB4 and
    turn on the red led for the traffic light on, to stop the cars
    and allow pedestrians to move from point A to point B by
    moving the 8bit (00010001) to port b.
   We call the delay of ten seconds before it can turn the
    pedestrian led to red and the traffic led to green. we named
    the delay long_delay.
   It will turn the red led for pedestrians, which is RB4 and turn
    on the green LED for the traffic light on, to stop the cars and
    allow pedestrians to move from point A to point B by moving
    the 8bit (00001100) to port b. .
   After the delay we go back to main the repeat the same
    process.
Long_delay
In the long delay we used a 3-loop delay since our number of
cycles is in the range of 12 cycles to 50594817 cycles
We have three literal values for the loop which are M, l, k.
We loaded the literal value m to the count1 in the memory
location of 020h.We equate 020h to count. We loaded the literal
value of l in count2 in the memory location of 021h which we
equate count2 to 021h and we loaded the literal value of k to the
memory location of 022h which we equate to count3 to
022h.Inside my loop l used the decrement file and skip if zero.
Short delay
In the short delay, I used a 3-loop delay since its cycles are 12 to
50594817.
We have three literal values for the loop which m, l, k.
We loaded the literal values m to countS1 in the memory location
of 023h.We equate 023h to countS1. We loaded the literal value
of l in countS2 in the memory location of 024h which we equate
countS2 to 024h and we loaded the literal value of k to the
memory location of 025h which we equate to countS3 to
025h.Inside my loop l used the decrement file and skip if zero.
Inside my loop l used the decrement file and skipped if zero.
Procedure (Proteus)
  1.   Connect   pin 6 to the first red LED
  2.   Connect   the first red LED to yellow LED
  3.   Connect   the yellow led to pin 7
  4.   Connect   both the first red LED and the yellow LED to ground
  5.   Connect   pin 8 to the first green LED
  6.   Connect   the first green LED to ground
  7.   Connect   pin 9 to the second green LED
  8.   Connect   the second green LED to second red LED
  9. Connect both the second green and red LED's to ground
  10.     Connect the red LED to pin 10
  11.     Connect pin 11 to the push switch, connect the push
     switch to the ground.
Calculations
10 seconds delay
Time delay = (4 x TIC)/4Mhz
10 sec = (4 x TIC)/4MHz
10 sec x (4 x 106 )   = 4TIC
TIC = 106
TIC = 10 000 000
Estimated TIC range value = (10000000)1/3
= 215.44
Our values for the literals m,l and k which we found by
estimation/approximation are:
M = 149
L = 135
K = 165
TIC3LOOPDELAY = 1 + 4M + 4ML + 3KLM
            = 1 + 4(149) + 4(149) (135) + 3(165) (135)(149)
            =10037982
            10 sec = 10
3 seconds delay
Time delay = (4 x TIC)/4Mhz
3 sec = (4 x TIC)/4MHz
3 sec x (4 x 106 )   = 4TIC
TIC = 3000000
Estimated TIC range value = (3000000)1/3
                     = 144.22
Our values for the literals m,l and k which we found by
estimation/approximation are:
M = 97
L = 125
K = 81
TIC3LOOPDELAY = 1 + 4M + 4ML + 3KLM
           = 1 + 4(97) + 4(97)(125) + 3(97)(125)(81)
           =2995264
Flowchart: Short delay
                    START
                  Load literal
                   countS1
                  Load literal
                   countS2
                  Load literal
                   countS3
                  CountS1--
                          If
                     countS1=
                     =0
                    CountS2--
                         If
                     countS2=
                        =0
                   CountS3--
                         If
                     countS3=
                       =00
                      Exit
Flowchart: Long delay
                        Start
                Load literal count1
                   Load literal
                     count2
                   Load literal
                     count3
                     Count1--
                           If
                        count1=
                          =0
                     Count2--
                           If
                        count2=
                          =0
                        Count3--
                           If
                        count3=
                          =0
                         Exit
Flowchart : main traffic light system controller
      Start
                                   Button
 Turn On Red LED
                                  Pressed?
  Call Long_delay.
    Long_Delay
Turn On Yellow LED
                                             Turn On RED LED
   Short_Delay
 Call Short_delay.
                                                Long_Delay
                                              Call Long_delay
Turn On Green LED
                                             Turn on Greed LED
  Long_Delay
 Call Long_delay.
                                               Call
                                                 Long_Delay
                                                    Long_delay
                                                     Exit
CODE
;group 27, student nunbers: 221377435, 223930760,224859376
;this code Will work as a traffic light,the code flash on LED's on RB0,RB1,RB2.
the code will continue to flash alternately on port RB3 and RB4 when the
code gets to RB5
;10 seconds between long delays and 3 seconds between short delays
;INTRC 4MHz
count1 EQU 020h
count2 EQU 021h
count3 EQU 022h
countS1 EQU 023h
countS2 EQU 024h
countS3 EQU 025h
       org 00h
       goto setup
       org h'04'
       retfie
setup:
       bsf STATUS,RP0
       movlw        b'11100000'
       movwf TRISB
       bcf STATUS,RP0
        goto Main
Main:
        movlw       b'00001001'            ;red light and set RB0 as an output
        movwf       PORTB
        call   long_delay     ;call a delay of 10 sec
        movlw b'000001010'          ;yellow light and set RB1 to output
        movwf       PORTB
        call   short_delay    ; call for delay of 3sec
        movlw       b'00001100'            ;green light and set RB2 to output
        movwf       PORTB
        call   long_delay     ;call for a delay for 10 sec
           movlw    b'000001010'           ;yellow light and set RB1 to output
        movwf       PORTB
        call   short_delay
        goto Real
        goto Main
Real:
        btfss PORTB,5
        goto Main
        goto Ped
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;This routine is called between 2 LED'S(red and yellow) which are set on port
RB0 and RB1,and between two other LED's(green and yellow) which are set
up on port RB2 and RB1
;total delays is 10 seconds before it moves to the next LED
long_delay:
        movlw             d'90'
        movwf             count1
label_L:
        movlw             d'85'
        movwf             count2
label_M:
        movlw             d'110'
        movwf             count3
loop:
        decfsz            count3,f
        goto loop
        decfsz            count2,f
        goto label_M
        decfsz            count1,f
        goto label_L
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
This routine is called between 2 LED'S(red and yellow) which are set on port
RB0 and RB1
;total delays is 10 seconds before it moves to the next LED
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;This routine is called between 2 LED'S(red and yellow) which are set on port
RB0 and RB1
;total delays is 10 seconds before it moves to the next LED
short_delay:
        movlw             d'57'
        movwf             countS1
label_1:
        movlw             d'97'
        movwf             countS2
label_2:
        movlw             d'31'
        movwf             countS3
loop2:
        decfsz            countS3,f
        goto loop2
        decfsz            countS2,f
        goto label_2
        decfsz            countS1,f
        goto label_1
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Ped:
        movlw              b'00010001'
        movwf              PORTB
        call long_delay
        movlw              b'00001100'
        movwf              PORTB
               call        long_delay
        goto Main
PROTEUS (schematic diagram)
These are traffic lights before the pedestrian button is pushed.
These are traffic lights after the pedestrian button is pushed.
This is figure depicts the traffic lights before they are activated.
The 3 LED’S on the upper hemisphere are the normal traffic
lights.The 2 LED’s on the lower hemisphere are the pedestrian
lights.
Project and results
This is a traffic regulator to regulate the traffic in an interstation of cars and
to allow the pedestrian to cross without any confusion between the motorist
and the pedestrian.
The traffic lights the improve safety on the intersection and enforce and
provide clear signal and communication between the drivers and the
pedestrian.
Traffic efficiency is improved as there is a good flow of traffic and aim to
optimize the transportation network mostly during the rush hour when
workers are going to work and coming from work. Also helps with students
and children crossing a busy road.
level                  Pedestrian             cars
Safety improvement     best                   good
Traffic reg            Slightly good          good
Efficiency             moderate               Slightly good
Observations
We constructed a traffic light that has 3 LEDs for motor traffic and
2 LEDs for pedestrians. Whilst constructing the circuit we
encountered a problem with the delay. The values that we
calculated for the m, l and k were giving us the longer delay of
more than10 seconds, with the guessed values guided by the
range of our calculations it was giving us the correct values.
Challenges Faced
We encountered a challenge with our delay values:
Long Delay
The values we calculated in our calculation are giving us the
incorrect delay wanted in the practical. For example, calculated
values are m=149, l=135, k=165. We get a delay of 40 seconds.
When we use the guess values guided by the range of the
calculated values we get a correct delay. For example, m=90,
l=85, k=110.we get a delay of 10 secs.
Short delay
The values we calculated in our calculation are giving us the
incorrect delay wanted in the practical. For example, calculated
values are m=97, l=125, k=81. We get a delay of more than 3
seconds.
When we use the guess values guided by the range of the
calculated values we get a correct delay. For example, m=57,
l=97, k=31.we get a delay of 3 secs.
Conclusion
A PORT corresponds to a specific memory location, and
transmitting data to a port essentially entails sending data to a
Special Function Register (SFR). The difference is that that the
data written to the SFR linked with the I/O port becomes
accessible via the physical pins of the PIC package. The primary
disparity in transferring data to a PORT versus any other SFR lies
in configuring the pins on the PORTS as either input or output.
Also, when dealing with timing loops, you must know that each
instruction requires a specific number of instruction cycles for
execution. The pace at which these instructions are processed
relies on the frequency of the oscillator in use. A higher oscillator
frequency results in faster instruction execution.
References
Ashraf Almadhoun (2020). Introduction to PIC Microcontroller and
Its Architecture.
www.youtube.com. (n.d.). How Do Traffic Signals Work? [online]
Available at: https://youtu.be/DP62ogEZgkI?si=kkopQW-
enHcJ1RhW [Accessed 16 Apr. 2024].
www.youtube.com. (n.d.). Single traffic light simulation in
Multisim using PIC Microcontroller PIC16f84A (Assembly
Language). [online] Available at:
https://www.youtube.com/watch?v=wbJv8oyKNU8 [Accessed 16
Apr. 2024].