1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice


Discussion in '8051/8951' started by adrianvon, Apr 12, 2015.

  1. adrianvon

    adrianvon Member

    Jan 22, 2011
    Hi all,

    I need to count pulses coming from a sensor when a switch is pressed (please refer to the attached diagram). The 8051 internal counter will be used.
    When the switch is released, the counter must stop immediately and the counts has to be displayed on an LCD. An LED is also turned on for 3 seconds on switch release (using 8051 internal timer).
    At the same time the counter must be refreshed to be ready for the next switch press.

    Now my problem is that I am not sure which timers are best to use for this application (timer 0, timer 1 or timer 2). I am also confused on which mode to use.

    Below is how I am planning to do it:

    1*Use timer 0 as timer in mode 1
    2*Use timer 1 as counter in mode 1
    3*Wait for the switch to be pressed
    4*When switch is pressed, start the counter
    5*When switch is released, immediately stop the counter, display counts on the LCD, refresh counter and start the LED 3 seconds timer
    6*Wait for the 3 seconds until LED is off (most probably a loop must be used for creating a 3 seconds timer delay)
    7*repeat process

    Now my concern is mostly on steps 5 and 6. Since these steps take time to execute, how can I make it in a way that a switch press is not ignored while executing these two steps?

    Any kind of help wound be appreciated and sorry for my poor English.

    Attached Files:

    Last edited: Apr 12, 2015
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Mar 28, 2011
    Rochdale UK
    • Like Like x 1
  3. Jon Wilder

    Jon Wilder Active Member

    Oct 22, 2010
    Fresno, CA
    I would use timer 0 in mode 1 (16-bit timer/counter) with bit C/T0# in the TMOD register set to configure it as a counter. Use P3.4/T0 as your pulse input. The pulse count can be read from the TH0 and TL0 register pair.

    Write the value 0x05 to register TMOD. This configures timer 0 as a 16-bit counter.

    In your code, write the value 0x10 to register TCON when the switch is pressed. This turns on timer 0 and enables it to count pulses. On switch release, write the value 0x00 to register TCON to turn off timer 0. From there you would read your pulse count from the TH0 and TL0 register pair.

    As for the LED, you can either derive a 3 second nested delay loop for that or you can configure timer 1 as a 16-bit timer and use the timer 1 interrupt with a software post scaler in the interrupt routine. With a 12MHz crystal, you would make timer 1 interrupt 46 times before it turns off the LED, which will amount to a 3 second delay.
  4. dave

    Dave New Member

    Jan 12, 1997

  5. Tony Stewart

    Tony Stewart Well-Known Member Most Helpful Member

    Aug 31, 2012
    Richmond Hill , ON Canada near Toronto

    You did not specify pulse rate, I suspect it is quite slow relative to switch bounce frequency , ( which needs to be debounced in hardware or software)

    The 3 second disarm , display and reset seems like an excessive amount of time for some unknown process.

    Perhaps you might elaborate on the higher level result. I think you mean, "count while pressed" compute and update display then clear input counter with a brief (<3s?) pause waiting for next switch event.
    Last edited: Apr 17, 2015

Share This Page