Simple "Time-out" routine


Closed Thread
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Time-out routine

    Hi Hendrik

    Thanks for the response. I have used two variables, Micro VAR WORD and Milli VAR WORD. For each cycle in the main loop where no switch was activated, the Micro (~micro second) increased by 1 until 999 by which time the Milli is increased by 1 and the micro is reset to 0. I had to do this because of the the varaible type word max 65 535 and that is not long enough. It just seemed very clumsy. I though it will be more elegant to use an internal timer which is reset each time the switch is activated. If a switch is not activated within 20 seconds, the system must stop the motor and end the application.

    Does that make sense?

    The reason why I am after a good solution, is that I might use this circuit in another application where timing is a bit more critical.


    Quote Originally Posted by HenrikOlsson View Post
    Hi Jaco,
    I haven't looked into your code very deeply but here's an idea that might work.
    Have a "counter" variable that you increment in each of the states where you have movement but not yet detected "the other" switch.

    Figure out the magic number that matches ~20 seconds and if the "counter" ever reaches that number you abort the whole thing. When you go from a state of movement but no switch to a state where you detect a s switch you reset the "counter".

    The timing may not be super accurate depending on how long your various subroutines takes but for a "simple timeout" it may suffice?

    /Henrik.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Sure, it makes sense. If you need accurate timing you'll have to resort to a timer based interrupt. However, if timing isn't that critical you can set up the hardware timer and just check the interrupt flag in software (ie. not actaully enabling the interrupt).

    You can use TMR1 of the 12F675, with a 1:8 prescaler and 4Mhz clock it will overflow at a rate of (4000000/4/8/65536) = ~1.9Hz. Unfortunately the highest prescaler ratio is 1:8 so that's as "slow" as it gets. Having it "time" 20 seconds intervals would require an external clock x-tal for the timer oscillator which would cost you two I/O pins that you don't have.... :-(

    For accurate timing, as already been said, you'll have to resort to an interrupt based routine. In this case you set up the timer so it overflows at a rate high enough for your timing purposes, lets say 100Hz. Then you keep track of the time pretty much as you already did - with variables like Days, Hours, Minutes, Seconds, ms.

    Have a look at Darrel Taylors Elapsed Time routine I think it pretty much fits your need perfectly.

    /Henrik.

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default ~20 sec hardware timer

    In the attached file, you can use the Timer1 with Interrupts. Just Start Timer1 by setting t1con.0 bit and then either check flag or stop whatevere your are doing in the ISR routine, inside the IF-THEN.

    Thanks to Darrel once again for his DT-INTS.

    Don't forget to include the files in the directory of your code and use MPASM as an assembler.

    Ioannis
    Attached Files Attached Files

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts