# PIC16F88 Traffic light Problem

Status
Not open for further replies.

#### shaft10

##### Newbie level 3
Hi Guys

I need some help / guidance if possible. I have a small project for my college studies which involves programing a PIC16F88 microcontroller to operate a set of traffic lights including a push button activated pedestrian crossing. So far I have the traffic lights working, theres two sets so when set is on Green the other is Red and vice versa. My problem is now I'm not really sure where the pedestrian crossing should sit in the program. I'm new to this so please be patient. Many thanks in advance for any help.

Shaft

#### wp100

Hi,

You really need to show the program code you have already done, or at least let us know what language you are using.

You have various options for reading the switch, you can do it direct from within your main program loop or via PortB which has two types of Interrupt.

As you are just beginning I would think avoiding the interrupts will be the easier route for now.

Connect your switch with one end connected to 0v and the other to an input pin with a 10k pull up resistor.
Read its status regularly from within your main program loop, when it detects a change then thats your signal to go to the subroutine for the pedestians

#### shaft10

##### Newbie level 3
Hi,

You really need to show the program code you have already done, or at least let us know what language you are using.

You have various options for reading the switch, you can do it direct from within your main program loop or via PortB which has two types of Interrupt.

As you are just beginning I would think avoiding the interrupts will be the easier route for now.

Connect your switch with one end connected to 0v and the other to an input pin with a 10k pull up resistor.
Read its status regularly from within your main program loop, when it detects a change then thats your signal to go to the subroutine for the pedestians
Hi

Here is the program so far I have.

LIST p=16F88

#include "P16F88.INC"

CBLOCK 0x10 ; Temporary storage
state
l1,l2
ENDC

org 0 ; Start up vector.
goto setports ; Go to start up code.

org 4 ; Interrupt vector.
halt goto halt ; Sit in endless loop and do nothing.

setports clrw ; Zero in to W.
movwf PORTA ; Ensure PORTA is zero before we enable it.
movwf PORTB ; Ensure PORTB is zero before we enable it.
bsf STATUS,RP0 ; Select Bank 1
clrw ; Mask for all bits as outputs.
movwf TRISB ; Set TRISB register.
bcf STATUS,RP0 ; Reselect Bank 0.

initialise clrw ; Initial state.
movwf state ; Set it.

movwf PORTB ; Write it to port.
incf state,W ; Increment state in to W.
andlw 0x03 ; Wrap it around.
movwf state ; Put it back in to memory.
call wait ; Wait
goto loop ; And loop

; Function to return bitmask for output port for current state.
; The top nibble contains the bits for one set of lights and the
; lower nibble the bits for the other set. Bit 1 is red, 2 is amber
; and bit three is green. Bit four is not used.
getmask movf state,W ; Get state in to W.
addwf PCL,F ; Add offset in W to PCL to calc. goto.
retlw 0x41 ; state==0 is Green and Red.
retlw 0x23 ; state==1 is Amber and Red/Amber
retlw 0x14 ; state==3 is Red and Green
retlw 0x32 ; state==4 is Red/Amber and Amber.

; Function using two loops to achieve a delay.
wait movlw 5
movwf l1

w1 call wait2
decfsz l1
goto w1

return

wait2 clrf l2
w2 decfsz l2
goto w2
return
END

#### shaft10

##### Newbie level 3
Hi,

You really need to show the program code you have already done, or at least let us know what language you are using.

You have various options for reading the switch, you can do it direct from within your main program loop or via PortB which has two types of Interrupt.

As you are just beginning I would think avoiding the interrupts will be the easier route for now.

Connect your switch with one end connected to 0v and the other to an input pin with a 10k pull up resistor.
Read its status regularly from within your main program loop, when it detects a change then thats your signal to go to the subroutine for the pedestians
Attachment probably more clear !

- - - Updated - - -

https://www.edaboard.com/attachments/64515d1321876445-crossing.zip
(from 2007)

I think can easily be added a push-button control...
Hi

Is this two traffic lights or one ?

#### Attachments

• 2.3 KB Views: 12

#### wp100

Hi,

Thats a good attempt, very nearly there ...

Below is your code with a couple of important points detailed in caps.

One of the important things is the oscillator /frequency you are uing as that affects your delays, though there is no mention what you are using.

The code builds fine but I have not checked your detailed progam / logic flow until you correct the points mentioned.

Code:
	LIST p=16F88

#include "P16F88.INC"

; SEE TEXT AT END

;     __CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
;     __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF

CBLOCK 0x20 ; Temporary storage   ; USER REGISTERS START AT 0x20 , your 0x10 will be overwritten by the SYSTEM REGISTERS
state
l1,l2
ENDC

org 0 ; Start up vector.
goto setports ; Go to start up code.

org 4 ; Interrupt vector.

setports clrw ; Zero in to W.
movwf PORTA ; Ensure PORTA is zero before we enable it.    ;PORTA DEFAULTS AT START UP TO ANALOGUE INPUTS SO YOU MUST CHANGE THEM DIGITAL IF YOU NEED DIGITAL PINS
movwf PORTB ; Ensure PORTB is zero before we enable it.	  	 ; THE DATA SHEET PORT SECTION DETAILS HOW
bsf STATUS,RP0 ; Select Bank 1
clrw ; Mask for all bits as outputs.
movwf TRISB ; Set TRISB register.
bcf STATUS,RP0 ; Reselect Bank 0.

initialise clrw ; Initial state.
movwf state ; Set it.

movwf PORTB ; Write it to port.
incf state,W ; Increment state in to W.
andlw 0x03 ; Wrap it around.
movwf state ; Put it back in to memory.
call wait ; Wait
goto loop ; And loop

; Function to return bitmask for output port for current state.
; The top nibble contains the bits for one set of lights and the
; lower nibble the bits for the other set. Bit 1 is red, 2 is amber
; and bit three is green. Bit four is not used
getmask movf state,W ; Get state in to W.
addwf PCL,F ; Add offset in W to PCL to calc. goto.
retlw 0x41 ; state==0 is Green and Red.
retlw 0x23 ; state==1 is Amber and Red/Amber
retlw 0x14 ; state==3 is Red and Green
retlw 0x32 ; state==4 is Red/Amber and Amber.

; Function using two loops to achieve a delay.
wait movlw 5
movwf l1

w1 call wait2
decfsz l1,F    ; AND STORE RESULT BACK IN FILE
goto w1

return

wait2 clrf l2
w2 decfsz l2,F	; AND STORE RESULT BACK IN FILE
goto w2
return
END

;  YOU DO NOT SPECIFY THE DEALY TIME OR WHAT OSCILLATOR  / FREQUENCY YOU CARD USING
;  TYPICALLY USE CAN USE THE DEFAULT INBUILT OSCILLATOR	, THOUGH YOU SHOULD USE THE CONFIG STATMENT TO CLARIFY - TYPICAL LINES AT BEGINNING OF CODE
;  DELAY CODES CAN BE GOT AT THIS SITE http://www.golovchenko.org/cgi-bin/delay

#### shaft10

##### Newbie level 3
hi

what are the delays for ?

#### wp100

hi

what are the delays for ?
Hi,

As you have already used, you need delays to slow things down for real time use.

You have not mentioned what oscillator /frequency you are using, but I would guess you are using the default internal oscillator which at the power on defaults for OSCCON is 000 0000 which means you are running at 31k, very slow.
This would make you delay routine .5second.

However its more typical, though not essential, to run at higher speeds, so your delay routines need a lot more loops, so a delay calculator is very handy if you are not a maths expert or a little lazy ..

Status
Not open for further replies.