traffic lights

Discussion in 'Programmer's Corner' started by djstar, Nov 28, 2010.

  1. djstar

    Thread Starter Active Member

    Jan 26, 2008
    37
    0
    hi , im very new to assembly langauge and im doing a unit at college on it. my assignment is to write a program that would simulate a pelican crosing. ive had a good go at writting it but when i load it into the assembler it comes up with erros.


    th sequence should go

    TURN ON GREEN LED
    DELAY
    TURN ON AMBER LED ON
    SMALL DELAY
    TURN RED LED ON AND SOUND A BUZER
    DELAY
    FLASH AMBER LED ON AND OF



    ive had a go at the program up to the buzer but im a bit confused to whi it isent workong.

    p3 is port 3 of the robot is a bank of leds and port 2.7 is the buzzer.

    if you need any more information then just say.

    many thanks laura

    Code ( (Unknown Language)):
    1.  
    2.  main1()
    3.  
    4.  {
    5.  
    6.  #pragma asm
    7.  
    8.  
    9.  
    10. start:
    11.  
    12.                 mov P3, #0Dfh                 ;// turn on green led//
    13.                
    14.  
    15.    
    16.                 mov r7, #0ffh                 ;// move ffh to reg 7 ( DELAY)//
    17. outerloop1:            
    18.                
    19.  
    20.                 mov r6, #0ffh                ;// move ffh to reg 6 (DELAY)//
    21. innerloop1:    
    22.  
    23.  
    24.                 mov r5, #0ffh
    25. inner:        
    26.  
    27.                 djnz r5 inner
    28.                
    29.                 djnz r6 innerloop1
    30.                
    31.                 djnz r7 outerloop1
    32.                
    33.                 mov P3, #0bfh                 ;// turn amber led on //
    34.            
    35.            
    36.            
    37.            
    38.            
    39.                
    40.                 mov r7, #3fh                 ;// move ffh to reg 7//
    41. outerloop2:                    
    42.  
    43.                 mov r6, #0ffh                ;// move ffh to reg 6//
    44. innerloop2:                
    45.                
    46.                 mov r5, #0ffh
    47. inner2:                
    48.                 djnz r5 inner2
    49.                
    50.                 djnz r6 innerloop2
    51.                
    52.                 djnz r7 outerloop2
    53.                
    54.            
    55.                 mov P3, #7fh                  ;// turn red led on //
    56.            
    57.            
    58.            
    59. buzzer:        
    60.            
    61.                 mov P2, #80h                  ;// siren on //
    62.  
    63.                 ACALL, delay:                 ;//  (delay)//
    64.                                
    65.                 mov P2, #00h                 ;// siren off//
    66.                
    67.                 ACALL, delay:                 ;//  (delay)//
    68.                
    69.                
    70.  
    71.  
    72.  
    73. delay:    
    74.                            
    75.  
    76.                 mov r2, #0fh                ;// move ffh to reg 6//
    77. innerloop8:    
    78.  
    79.  
    80.                 mov r1, #0ffh                ;// move ffh to reg 5//
    81. inner8:        
    82.  
    83.                 djnz r1 inner8
    84.                
    85.                 djnz r2 innerloop8
    86.                
    87.                 ret
    88.                
    89.                
    90.                
    91. #pragma endasm
    92.  
    93. }
    94.  
    95.  
     
    Last edited: Nov 29, 2010
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    Aren't pelicans color blind? :)


    It might help if you listed the error you are getting.

    Do you have to do this in assembler? C and BASIC are so much easier.
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It's OK, they can tell by the position of the light.
     
  4. djstar

    Thread Starter Active Member

    Jan 26, 2008
    37
    0
    Thansk for the reply guys. we are doing a microcontroller module and it is all about assembly langague so that is what we have to program in. ive edited the script a bit because i realised one thing that was going wrong. The only thing i am stuck on is how to loop a sub routine.

    on the label "buzzerr:" the buzer is turned on and then it jumps to the delay and then turns the buzzer off then jumps down to the delay again, i need to loop this "buzzerr:" for this delay

    Code ( (Unknown Language)):
    1.  
    2.  
    3.  
    4.  
    5.                 mov r7, #0ffh               ;// move ffh to reg 7 ( DELAY)//
    6. outerloop1:        
    7.                
    8.  
    9.                 mov r6, #0ffh               ;// move ffh to reg 6 (DELAY)//
    10. innerloop1:
    11.  
    12.  
    13.                 mov r5, #0ffh
    14. inner:     
    15.  
    16.                 djnz r5 inner
    17.                
    18.                 djnz r6 innerloop1
    19.                
    20.                 djnz r7 outerloop1
    21.  
    22.  

    so what i am after is the buzer to turn on and of for a set time. Im not sure if you need this information but the controller i am using is a 8051 and the spare registeries i have left are R3 R4

    many thanks
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I'd just put a loop around the buzzer part so it flashes 10 times (change as required).
    Make sure it finishes with buzzer off.
    In your delay sub you can reuse R7 and R6 if you want instead of r1 and r2.
     
  6. PRS

    Well-Known Member

    Aug 24, 2008
    989
    35
    Use a software delay. This is just a loop that counts to a number then exits. The number determines the length of the delay.
     
  7. djstar

    Thread Starter Active Member

    Jan 26, 2008
    37
    0
    Thanks for the reply, but could you explain what a software loop is? im very new to assembly langague.
     
  8. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    This is a loop: it just turns the siren on and off about 10 times.
    (not sure about my syntax, this is my first 8051 code).
    Code ( (Unknown Language)):
    1. mov r6, #0Ah
    2. buzzer:        
    3.            
    4.                 mov P2, #80h                  ;// siren on //
    5.  
    6.                 ACALL, delay:                 ;//  (delay)//
    7.                                
    8.                 mov P2, #00h                 ;// siren off//
    9.                
    10.                 ACALL, delay:                 ;//  (delay)//
    11.  
    12. djnz r6 buzzer
     
  9. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    What would be cool is to take it a step further and add sensors to the red lights to make them change when a car pulls up to the red. This would require a little more hardware and coding but would be a real representation of a stop light. Also find out the formula for yellow light lengths. There is a formula involving the type of street and mph. You could also add the white led at the bottom so police can tell if the light is red from the other direction. That should give you some extra credit. Have it where you can select 25mph light, 35mph, .... 55mph light. Sensors to detect cars for rural lights, Flashing yellow/red, flashing red for all stop, Then may be think about a turn light system and how it ties into all the other lights.
     
    Last edited: Dec 1, 2010
  10. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718

    I don't think pelicans can walk over 25mph.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    And the addition of a white light to inform police the other side is red? Is he inferring that pelicans run red lights?? Preposterous!
     
  12. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    Sorry I must be dislesic. My mind kept reading pedestrian not pelican. So I kept thinking it would be a normal light. LOL Whoops my bad for not reading left to right.
     
  13. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    We are all only human and make errors. Being corrected keeps us on our toes, since some people think we can never be wrong.
     
  14. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Are you thinking something like this?
    (Actually pelican crossings are just what we call pedestrian crossings on this side of the pond).
    [​IMG]
     
Loading...