PIC16F88 Traffic Light Program

Thread Starter

Shaft

Joined Nov 20, 2012
9
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
 

Thread Starter

Shaft

Joined Nov 20, 2012
9
This will depend. How does your traffic lights system look like. Is it more than one pedestrian crossing
Hi

Theres just one pedestrian crossing, so when pedestrian button is pressed, both lights have to go red for 10 seconds.
 

t06afre

Joined May 11, 2009
5,934
If you post your code. We can help you much faster. With some pointers. Then you post your code. Use code tags. Paste your code in the selecet the code. Then use this button
 

Thread Starter

Shaft

Joined Nov 20, 2012
9
If you post your code. We can help you much faster. With some pointers. Then you post your code. Use code tags. Paste your code in the selecet the code. Then use this button
Rich (BB code):
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.
loop       call    getmask         ; Convert state to bitmask.
           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
 

t06afre

Joined May 11, 2009
5,934
If I am not wrong. Your program spend most of its time in the wait loop. So why not place a check for the pedestrian button here
 

tshuck

Joined Oct 18, 2012
3,534
Yeah agree could use an interupt, I just thought subroutines were easier to use. Not sure where these would sit in the program.
As far as the subroutines go, you'd have to set the state and make the delay, before returning to the previous state.....this could be as simple as on detection of a switched level, go to a subroutine, which will set the state(LEDs) and wait for 10 seconds, then, it will return to what it was previously doing...

As far as the where, I'd put it in the wait routines like t06afre already said...
 

Thread Starter

Shaft

Joined Nov 20, 2012
9
As far as the subroutines go, you'd have to set the state and make the delay, before returning to the previous state.....this could be as simple as on detection of a switched level, go to a subroutine, which will set the state(LEDs) and wait for 10 seconds, then, it will return to what it was previously doing...

As far as the where, I'd put it in the wait routines like t06afre already said...
Ok, ill have a go at adding a subroutine then then I'll repost the code on here for you guys to check it out.
 

t06afre

Joined May 11, 2009
5,934
and call on a subroutine ?
Yes something like this. Then the pedestrian button is pressed does all lights have to go red, or just in one dirrection? Perhaps you could draw a sketch of the traffic light system. Just make a pencil drawing and take a snapshot of it. If you feel for it
 

Thread Starter

Shaft

Joined Nov 20, 2012
9
Yes something like this. Then the pedestrian button is pressed does all lights have to go red, or just in one dirrection? Perhaps you could draw a sketch of the traffic light system. Just make a pencil drawing and take a snapshot of it. If you feel for it
they both need to go red (10 seconds) once the button is pressed
 

Thread Starter

Shaft

Joined Nov 20, 2012
9
Rich (BB 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.
	retfie ; DO NOTHING, RETURN TO POINT OF INTERRUPT


	
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.
	
loop call getmask ; Convert state to bitmask.
	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
they both need to go red (10 seconds) once the button is pressed
 
Top