How best to time a pin state for extended periods of time


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: How best to time a pin state for extended periods of time

    The best solution to at least half of all timing problems is using a timer interrupt. I don't know why so many people avoid that technique!

    If you use an interrupt, you can do all your timing in the background.
    Charles Linquist

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: How best to time a pin state for extended periods of time

    While I don't disagree, when the level of accuracy is as low as this app seems to be, polling the flag seems the easier to understand solution. The OP is looking for time between 0 and 30 minuites. Seems like an INT may be just a bit overkill. Granted, we have no idea what else needs to be done in the program, so an Interrupt may well be the best overall solution.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: How best to time a pin state for extended periods of time

    If Time accuracy is not an issue then a simple routine like this should solve the problem:

    Code:
    Counter  Var WORD
    
    Main:
    
    IF Pin_1 = 0 then
    PAUSE 1000  ' one second delay
    Counter = Counter + 1
    IF Counter >1800 then Output_3 = 1
    ENDIF
    
    IF Pin_1 = 1  and Counter > 1 then
    IF Counter <= 120 then Output_1 = 1
    IF Counter >120 and Conter <= 1200 then Outpt_2 = 1
    IF Counter >1200 then Output_3 = 1
    Counter = 0
    ENDIF
    
    
    GOTO Main
    All the time your Pin_1 will go to zero the time check will be activated.

    Cheers

    Al.
    Last edited by aratti; - 10th March 2011 at 06:48.
    All progress began with an idea

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: How best to time a pin state for extended periods of time

    another option with more time to do other things:
    Code:
    counter var word
    counting var bit
    
    turn on 16 bit timer ' @4mHz osc, this will take .065535 seconds to roll over
    
    counter = 0
    
    MAIN:
     IF TxIF = 1 THEN GOSUB TIMESTUFF ' TxIF is the timer interrupt flag
     
     'USER CODE GOES HERE
    
    GOTO MAIN
    
    TIMESTUFF:
    TxIF = 0 'clear interrupt flag
     IF PIN_1 = 0 THEN 
       COUNTER = COUNTER + 1
       COUNTING = 1
     ENDIF
     IF (COUNTING) AND (PIN_1) THEN
       IF COUNTER <= 1830 THEN OUTPUT_1 = 1 ' 1 second = aproxx 15.25 counts
       IF COUNTER >1830 AND COUNTER <18300 THEN OUTPUT_2 = 1
       ' if you want to do anything for time between 20 and 30 mins, here's a good spot for that
       IF COUNTER >27450 THEN OUTPUT_3 = 1
       COUNTER = 0
       COUNTING = 0
     ENDIF
    RETURN
    Nothing wrong with Aratti's example, just showing another way.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

Members who have read this thread : 0

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

Posting Permissions

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