PIC16F88 Traffic Light Program

Discussion in 'Embedded Systems and Microcontrollers' started by Shaft, Nov 20, 2012.

  1. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    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
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    This will depend. How does your traffic lights system look like. Is it more than one pedestrian crossing
     
  3. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    Hi

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

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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 [​IMG]
     
  5. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    Hi

    Hope this works. See attachment
     
  6. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    Code ( (Unknown Language)):
    1. LIST    p=16F88
    2.            #include "P16F88.INC"
    3.            CBLOCK 0x10   ; Temporary storage
    4.               state
    5.               l1,l2
    6.            ENDC
    7.            org     0               ; Start up vector.
    8.            goto    setports        ; Go to start up code.
    9.       org     4               ; Interrupt vector.
    10. halt       goto    halt            ; Sit in endless loop and do nothing.
    11. setports   clrw                    ; Zero in to W.
    12.            movwf   PORTA           ; Ensure PORTA is zero before we enable it.
    13.            movwf   PORTB           ; Ensure PORTB is zero before we enable it.
    14.            bsf     STATUS,RP0      ; Select Bank 1
    15.            clrw                    ; Mask for all bits as outputs.
    16.            movwf   TRISB           ; Set TRISB register.
    17.            bcf     STATUS,RP0      ; Reselect Bank 0.
    18. initialise clrw                    ; Initial state.
    19.            movwf   state           ; Set it.
    20. loop       call    getmask         ; Convert state to bitmask.
    21.            movwf   PORTB           ; Write it to port.
    22.            incf    state,W         ; Increment state in to W.
    23.            andlw   0x03            ; Wrap it around.
    24.            movwf   state           ; Put it back in to memory.
    25.            call    wait            ; Wait :-)
    26.            goto    loop            ; And loop :-)
    27.            ; Function to return bitmask for output port for current state.
    28.            ; The top nibble contains the bits for one set of lights and the
    29.            ; lower nibble the bits for the other set. Bit 1 is red, 2 is amber
    30.            ; and bit three is green. Bit four is not used.
    31. getmask    movf    state,W         ; Get state in to W.
    32.            addwf   PCL,F           ; Add offset in W to PCL to calc. goto.
    33.            retlw   0x41            ; state==0 is Green and Red.
    34.            retlw   0x23            ; state==1 is Amber and Red/Amber
    35.            retlw   0x14            ; state==3 is Red   and Green
    36.            retlw   0x32            ; state==4 is Red/Amber and Amber.
    37.            ; Function using two loops to achieve a delay.
    38. wait       movlw   5
    39.            movwf   l1
    40. w1         call    wait2
    41.            decfsz  l1
    42.            goto    w1
    43.            return
    44.  
    45. wait2      clrf    l2
    46. w2         decfsz  l2
    47.            goto    w2
    48.            return
    49.            END
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  8. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    and call on a subroutine ?
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Why not use an interrupt? That is what the button does in the program flow....
     
  10. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    Yeah agree could use an interupt, I just thought subroutines were easier to use. Not sure where these would sit in the program.
     
  11. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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...
     
  12. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    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.
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  14. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    they both need to go red (10 seconds) once the button is pressed
     
  15. Shaft

    Thread Starter New Member

    Nov 20, 2012
    9
    0
    Code ( (Unknown Language)):
    1. LIST p=16F88
    2.    
    3.     #include "P16F88.INC"
    4.  
    5. ; SEE TEXT AT END
    6.  
    7. ;     __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
    8. ;     __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF
    9.  
    10.  
    11.    
    12.     CBLOCK 0x20 ; Temporary storage   ; USER REGISTERS START AT 0x20 , your 0x10 will be overwritten by the SYSTEM REGISTERS
    13.     state
    14.     l1,l2
    15.     ENDC
    16.    
    17.     org 0 ; Start up vector.
    18.     goto setports ; Go to start up code.
    19.    
    20.     org 4 ; Interrupt vector.
    21.     retfie ; DO NOTHING, RETURN TO POINT OF INTERRUPT
    22.  
    23.  
    24.    
    25. setports clrw ; Zero in to W.
    26.     movwf PORTA ; Ensure PORTA is zero before we enable it.    
    27.  movwf PORTB ; Ensure PORTB is zero before we enable it.         ;
    28.     bsf STATUS,RP0 ; Select Bank 1
    29.     clrw ; Mask for all bits as outputs.
    30.     movwf TRISB ; Set TRISB register.
    31.     bcf STATUS,RP0 ; Reselect Bank 0.
    32.    
    33. initialise clrw ; Initial state.
    34.     movwf state ; Set it.
    35.    
    36. loop call getmask ; Convert state to bitmask.
    37.     movwf PORTB ; Write it to port.
    38.     incf state,W ; Increment state in to W.
    39.     andlw 0x03 ; Wrap it around.
    40.     movwf state ; Put it back in to memory.
    41.     call wait ; Wait
    42.     goto loop ; And loop
    43.    
    44.     ; Function to return bitmask for output port for current state.
    45.     ; The top nibble contains the bits for one set of lights and the
    46.     ; lower nibble the bits for the other set. Bit 1 is red, 2 is amber
    47.     ; and bit three is green. Bit four is not used
    48. getmask movf state,W ; Get state in to W.
    49.     addwf PCL,F ; Add offset in W to PCL to calc. goto.
    50.     retlw 0x41 ; state==0 is Green and Red.
    51.     retlw 0x23 ; state==1 is Amber and Red/Amber
    52.     retlw 0x14 ; state==3 is Red and Green
    53.     retlw 0x32 ; state==4 is Red/Amber and Amber.
    54.    
    55.     ; Function using two loops to achieve a delay.
    56. wait movlw 5
    57.     movwf l1
    58.    
    59. w1 call wait2
    60.     decfsz l1,F    ; AND STORE RESULT BACK IN FILE
    61.     goto w1
    62.    
    63.     return
    64.    
    65.    
    66. wait2 clrf l2
    67. w2 decfsz l2,F  ; AND STORE RESULT BACK IN FILE
    68.     goto w2
    69.     return
    70.     END
    71.  
     
Loading...