Another operation during delay

Discussion in 'Embedded Systems and Microcontrollers' started by elyson, Aug 18, 2008.

  1. elyson

    Thread Starter New Member

    Aug 7, 2008
    7
    0
    I need to design a circuit using 8051, with 2 button and 2 LED. The circuit suppose to act this way, every time a button A is pressed, LED A will lit for 10 sec and so is when button B is pressed.

    So I connected the circuit and wrote the program. But there is something I notice, if I press button A and when the program is delayed 10 sec before clearing the port for LED A, button B has no effect.

    Basically I need to have those 2 button to work seperately, any idea how?
     
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    In one word TIMERS!
    Your 8051 has several timers built-in. Use one of them to do your 10 second delay timing. This will leave the processor free to do other more useful things like checking for pressed buttons.
     
  3. elyson

    Thread Starter New Member

    Aug 7, 2008
    7
    0
    Can someone give more details on the timer programing and application? Or better, can someone post a working timer program here for my exercise that works for me to study.
     
  4. amolhshah

    Member

    Aug 12, 2008
    13
    1
  5. Arm_n_Legs

    Active Member

    Mar 7, 2007
    183
    10
    If you pressed switch A, and your program goes into a delay loop to give you the 10 sec delay, then switch B will not be polled, and therefore has no effect while the system is waiting for this 10 sec delay to complete.

    The program below should give you some idea. It is not debugged, but the idea is there.

    The two switches have to be polled regularly. Let the timer runs freely and overflow every 50 ms. If switch A is pressed, initialise the value of R1 to 200. Decrement R1 everytime the timer overflow. After 200 overflow (200 x 50ms = 10 s), R1 becomes zero, and the program turn off LED A. LED B is separately controlled by the value in R2.

    Gee... i prefer to code in C...


    MOV TMOD,#10H ; CONFIGURE TIMER 1
    MOV TH1,#3CH ; ASSUME 12 MHZ CRYSTAL
    MOV TL1,#B0H ; TIMER TAKES 50MS TO OVERFLOW
    SETB TR1 ; START TIMER 1

    POLL: ; POLL FOR THE SWITCHES
    JNB P1.0,AA1 ; JUMP IF SWITCH A IS PRESSED
    JNB P1.1,BB1 ; JUMP IF SWITCH B IS PRESSED

    POLL2:
    JNB TF1,POLL ; TIMER NOT OVERFLOW YET, CONTINUE TO POLL
    CLR TF1
    MOV TH1,#3CH
    MOV TL1,#B0H
    CJNE R1,#0,CNTDOWN_A
    SETB P3.0 ; OFF LED A
    CJNE R2,#0,CNTDOWN_B
    SETB P3.1 ; OFF LED B
    SJMP POLL

    AA1:
    MOV R1,#200D ; START TIMING
    CLR P3.0 ; ON LED A
    SJMP POLL2

    BB1:
    MOV R2,#200D
    CLR P3.1 ; ON LED B
    SJMP POLL2

    CNTDOWN_A:
    DEC R1
    SJMP POLL

    CNTDOWN_B:
    DEC R2
    SJMP POLL
     
  6. elyson

    Thread Starter New Member

    Aug 7, 2008
    7
    0
    thanks alot for the info
     
  7. elyson

    Thread Starter New Member

    Aug 7, 2008
    7
    0
    Is it possible to extend this idea to 5 buttons or maybe 10 buttons?
     
  8. Arm_n_Legs

    Active Member

    Mar 7, 2007
    183
    10
    As many...
     
Loading...