8051 2 second timer

Discussion in 'Embedded Systems and Microcontrollers' started by Dritech, Apr 10, 2015.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

    I have a low RPM motor which is connected to a sensor which outputs a pulse with every rotation. Now I want to connect this sensor with the 8051 counter. I will also be using a momentary switch which when pressed will short to ground.
    Now I want the counter to only operate when the switch is pressed (ex: if the switch is pressed for 5seconds, the counter will only count the rotations for that period). When the switch is released I need to read the counts to determine the rotations at that period and reset the counter for the next button press.
    A timer of 2 seconds will also be enables with the switch release.

    I other words, I need a counter and a timer. I was planning to use Timer 0 in Mode 1 as the counter and Timer 1 in mode 0 for the 2 seconds delay.

    Which is the best mode to use for this combination?
    Is is possible to achieve a 2seconds delay in Mode 0 using the prescaler?

    Any help would be highly appreciated.
     
    Last edited: Apr 10, 2015
  2. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    I'm not sure you can get all the way to 2 seconds with just a hardware timer. If so you can go with whatever works.
    If not then the most convenient thing you can do is to set the timer up so it auto-reloads when it expires so the time between the interrupts is always constant. Then no matter how long it takes to process the interrupt, every 'tick' will be the same length. Count interrupts until 2 seconds has passed and set a flag in the interrupt routine that you can check and reset in the non interrupt code. DONT make the mistake of coding the process to be invoked every 2 seconds into the interrupt routine. That is the road to perdition.
     
  3. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi again,
    Can I use timer 1 if timer 0 is used in mode 3?
    I was looking at the datasheet and realized that in mode 3, both TF0 and TF1 are used. So I assume that that if one timer is in this mode, the other timer cannot be used.
     
  4. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Basically I want to use two timers and one counter. Now I need to use the overflow interrupt of timer 0 and one overflow interrupt of timer 1 (since I cannot use three overflow interrupts). Is this possible to do this when used the timers in the modes shown in the attached diagram?

    Any help would be highly appreciated.
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    I don't think so, but if you give me a link to specific datasheet I might be able to come up with something.
     
  6. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
  7. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Timer 1 in mode 3 just halts and doesn't do anything.
    Placing Timer 1 in mode 3 causes it to halt and hold its count. This can be used to halt
    Timer 1 when TR1 run control bit is not available i.e. when Timer 0 is in mode 3.
    I don't think it will work the way you want it to. Is timer 2 also in use?
     
  8. xainnasir

    New Member

    Nov 8, 2012
    26
    2
  9. dannyf

    Well-Known Member

    Sep 13, 2015
    1,767
    357
    It is fairly easy.

    1 set up your timer to trip periodically. Let's say once every 10ms.
    2 in the timer isr, count the number of times the isr has been called. Count up or count down, it doesn't matter.
    3 once the timer isr has been called the desired number of times, 200 in this case, reset the counter and do your task,, or set a flag to do your task.

    This is a fairly standard approach.
     
Loading...