1

Lesson 3. Traffic Lights.

If you have ever tried to design a ‘simple’ set of traffic lights then you will appreciate how much circuitry is required. An oscillator circuit, counters and logic decode circuitry.

The microcontroller circuit is a much better solution even for this ‘simple’ arrangement. The circuit is shown in Fig.3.1

Fig.3.1 Traffic Lights Circuit.

When using the development kit connect the PORTB outputs to the relevant LEDs. Connect the two links to include the oscillator.

A truth table of the operation of the lights is probably a better aid to a solution rather than a flowchart.

Traffic Light Truth Table.

Time / B7 / B6 / B5 / B4 / B3 / B2 / B1 / B0
R1 / A1 / G1 / R2 / A2 / G2
2sec / 0 / 0 / 1 / 0 / 0 / 1 / 0 / 0
2sec / 0 / 0 / 1 / 1 / 0 / 1 / 0 / 0
5sec / 0 / 0 / 0 / 0 / 1 / 1 / 0 / 0
2sec / 0 / 0 / 0 / 1 / 0 / 1 / 0 / 0
2sec / 0 / 0 / 1 / 0 / 0 / 1 / 0 / 0
2sec / 0 / 0 / 1 / 0 / 0 / 1 / 1 / 0
5sec / 0 / 0 / 1 / 0 / 0 / 0 / 0 / 1
2sec / 0 / 0 / 1 / 0 / 0 / 0 / 1 / 0
REPEAT

Program Listing for the Traffic Lights.

;TRAFFIC.ASM

; EQUATES SECTION

TMR0 EQU 1 ;means TMR0 is file 1.

STATUS EQU 3 ;means STATUS is file 3.

PORTA EQU 5 ;means PORTA is file 5.

PORTB EQU 6 ;means PORTB is file 6.

ZEROBIT EQU 2 ;means ZEROBIT is bit 2.

ADCON0 EQU 1FH ;A/D Configuration reg.0

ADCON1 EQU 9FH ;A/D Configuration reg.1

ADRES EQU 1EH ;A/D Result register.

CARRY EQU 0 ;CARRY IS BIT 0.

TRISAEQU85H

TRISBEQU86H

OPTION_R EQU81H

OSCCONEQU8FH;Oscillator control register.

COUNT EQU 20H ;means COUNT is file 20H, a register to count events.

;*********************************************************

LIST P=16F818 ;we are using the 16F818.

ORG 0 ;the start address in memory is 0

GOTO START ;goto start!

;*********************************************************

; Configuration Bits

__CONFIG H'3F10' ;sets INTRC-A6 is port I/O, WDT off, PUT on, MCLR tied to VDD A5 is I/O

;BOD off, LVP disabled, EE protect disabled, Flash Program Write disabled,

;Background Debugger Mode disabled, CCP function on B2, Code Protection ;disabled.

;*****************************************************

;SUBROUTINE SECTION.

; 0.1 second delay, actually 0.099968s

DELAYP1 CLRF TMR0 ;START TMR0.

LOOPB MOVF TMR0,W ;READ TMR0 INTO W.

SUBLW .3 ;TIME - 3

BTFSS STATUS,ZEROBIT ; Check TIME-W = 0

GOTO LOOPB ;Time is not = 3.

NOP;add extra delay

NOP

RETLW 0 ;Time is 3, return.

; 0.5 second delay.

DELAYP5 MOVLW.5

MOVWFCOUNT

LOOPCCALLDELAYP1

DECFSZCOUNT

GOTOLOOPC

RETLW0

; 1 second delay.

DELAY1 MOVLW.10

MOVWFCOUNT

LOOPACALLDELAYP1

DECFSZCOUNT

GOTOLOOPA

RETLW0

; 2 second delay.

DELAY2 MOVLW.20

MOVWFCOUNT

LOOPDCALLDELAYP1

DECFSZCOUNT

GOTOLOOPD

RETLW0

; 5 second delay.

DELAY5 MOVLW.50

MOVWFCOUNT

LOOPECALLDELAYP1

DECFSZCOUNT

GOTOLOOPE

RETLW0

;*********************************************************

;Configuration Section

START BSF STATUS,5 ;Turns to Bank1.

MOVLW B'11111111' ;PORTA is I/P

MOVWFTRISA

MOVLW B'00000110' ;PORTA IS DIGITAL

MOVWF ADCON1

MOVLW B'00000000'

MOVWFTRISB ;PORTB is OUTPUT

MOVLW B'00000000'

MOVWFOSCCON ;oscillator 31.25kHz

MOVLW B'00000111' ;Prescaler is /256

MOVWFOPTION_R ;TIMER is 1/32 secs.

BCF STATUS,5 ;Return to Bank0.

CLRF PORTA ;Clears PortA.

CLRF PORTB ;Clears PortB.

;*********************************************************

;Program starts now.

BEGINMOVLWB’00100100’;R1, R2 on.

MOVWFPORTB

CALLDELAY2;Wait 2 Seconds.

MOVLWB’00110100’;R1, A1, R2 on.

MOVWFPORTB

CALLDELAY2;Wait 2 Seconds.

MOVLWB’00001100’;G1, R2 on.

MOVWFPORTB

CALLDELAY5;Wait 5 Seconds.

MOVLWB’00010100’;A1, R2 on.

MOVWFPORTB

CALLDELAY2;Wait 2 Seconds.

MOVLWB’00100100’;R1, R2 on.

MOVWFPORTB

CALLDELAY2;Wait 2 Seconds.

MOVLWB’00100110’;R1, R2, A2 on.

MOVWFPORTB

CALLDELAY2;Wait 2 Seconds.

MOVLWB’00100001’;R1, G2 on.

MOVWFPORTB

CALLDELAY5;Wait 5 Seconds.

MOVLWB’00100010’;R1, A2 on.

MOVWFPORTB

CALLDELAY2;Wait 2 Seconds.

GOTO BEGIN

END

How Does It Work.

In the previous example FLASHER.ASM we turned one LED on and off. We could have used the two commands BSF and BCF to turn several outputs on and off, but a much better way has been used with the TRAFFIC.ASM program.

The basic difference is the introduction of two more commands:

  • MOVLW MOVe the Literal (a number) into the Working register.
  • MOVWF MOVe the Working register to the File.

The data, in this example, binary numbers, are moved to W and then to the file which is the output PORTB to switch the LEDs on and off. Unfortunately the data cannot be placed in PORTB with only one instruction it has to go via the W register.

So:

MOVLWB’00100100’clears(off) B7 and B6, sets (on) B5, clears B4,B3, sets B2 and clears B1, B0 in the W register. B0 is the right hand bit and B7 is the left hand bit

MOVWFPORTB moves the data from the W register to PORTB to turn the relevant LEDs on and off.

All 8 outputs are turned on/off with these 2 instructions.

CALL DELAY2 waits 2 seconds before continuing with the

next operation. DELAY2 has been added to the subroutine section.

The W register.

The W or working register is the most important register in the micro. It is in the W register were all the calculations and logical manipulations such as addition, subtraction, and-ing, or-ing etc., are done.

The W register shunts data around like a telephone exchange re-routes telephone calls. In order to move data from locationA to locationB, the data has to be moved from locationA to W and then from W to locationB

NB. If the three lines in the TRAFFIC.ASM program are repeated then any pattern and any delay can be used to sequence the lights – you can make your own disco lights!