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


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2007
    Posts
    59

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

    I haven't done much with timing events and what little I've done haven't been critical so I've just used counts.

    However, I've got a project where I need to time how long a pin is switched to ground and need to do something like this.

    if pin1 = 0 for 0 to 2 minutes then output1 = 1
    if pin1 = 0 for 2 to 20 minutes then output2 = 1
    if pin1 = 0 for > 30 minutes then output3 = 1

    I was going through my PBP Compiler manual and found the RCTIME command. Not sure if this would work. But I'm using a 4mhz crystal and the RCTIME only counts to 65535. So if my math is right, it will only go for about 16 minutes?

    What's the easiest way to do this?
    Thank you,

    Hylan

  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

    I would just keep using counters. maybe set a timer up to freerun with the biggest pre-scale your PIC supports. Caculate how long it takes to rollever based on your clock. then just poll the timer flag for the timer you are using. when the flag is set, gosub to you counter update routine.

    in there clear the flag and add 1 to your count.

    As for accuracy, this will not be as spot on as an interrupt, but the margin of error will only be how long your main takes.

    you might need more then 1 counter, depends on PIC and speed and things like that
    -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
    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

  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

    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!

  5. #5
    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

  6. #6
    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 : 1

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