PDA

View Full Version : How best to time a pin state for extended periods of time



Hylan
- 9th March 2011, 23:34
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

cncmachineguy
- 9th March 2011, 23:57
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

Charles Linquis
- 10th March 2011, 04:22
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.

cncmachineguy
- 10th March 2011, 04:50
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. :)

aratti
- 10th March 2011, 06:29
If Time accuracy is not an issue then a simple routine like this should solve the problem:



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.

cncmachineguy
- 10th March 2011, 11:20
another option with more time to do other things:



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.