PDA

View Full Version : Timeout function



johnmiller
- 7th May 2007, 09:47
hi

i am a beginner in pic programming with picbasic.
I am wondering if there is any way I can make a function timeout after 1 second. Here is the code that I tried (on a PIC18F458):



DEFINE OSC 20

TIME_COUNTER VAR WORD
INTCON = 0
TIME_COUNTER = 0

While In_STB=1
IF INTCON.2 = 1 THEN
INTCON.2 = 0
TIME_COUNTER = TIME_COUNTER + 1
ENDIF
IF TIME_COUNTER > PRESET THEN GOTO DataDone
Wend
....


I am not really interested to be 1 second (it can be >0.9 & < 2) somewhere in that interval. Any input is apreciated

sayzer
- 7th May 2007, 11:14
Why not "PAUSE 1000" ?

---------------------

johnmiller
- 7th May 2007, 11:31
I need it to wait in the WHILE In_STB=1 state (that's the clock of my data) and it's in a 1 state always (as soon as it drops I need to read the data) that's why I need a timeout function out of the while

T.Jackson
- 7th May 2007, 11:32
Properly doing the job would involve the use of one of the PIC's internal timers. TMRO could be used, load it with a value and let it overflow a few times. Possibly the best person to help you with this would be Darrel. I think he might have some good code that's directly applicable in this area. In fact, the code that you already have
looks remarkably like a working timeout proc ;)
<br/>

sayzer
- 7th May 2007, 12:02
Once In_STB = 0 then you might have had half a second passed not a full second.

Or, if a full second is passed and you are in DataDone, then you might still have had In_STB = 1; then you come back immediately (possibly).

Or, having a timer module running inside a WHILE statement will keep you and your Timer module locked in that WHILE statement. Thus, having a "PAUSE 1000" will do the same job for you cause the statement is locked anyway.

--------------------------

T.Jackson
- 7th May 2007, 12:17
PAUSE is synchronous - meaning nothing else can be processed during its execution. At least not PBP code. Asynchronous on the other hand - (which is what you want in this case) - typically something that runs in the background, usually hardware-based interrupts, timers and so forth.
<br/>

johnmiller
- 7th May 2007, 12:25
can you please supply me with a example for my code using the timer.

T.Jackson
- 7th May 2007, 12:37
Take a look at this thread: http://www.picbasic.co.uk/forum/showthread.php?t=2129

RTC has some primary TMR0 fundamentals that might help point you in the right direction.

sayzer
- 7th May 2007, 12:38
PAUSE is synchronous - meaning nothing else can be processed during its execution. At least not PBP code. Asynchronous on the other hand - (which is what you want in this case) - typically something that runs in the background, usually hardware-based interrupts, timers and so forth.
<br/>

This is what I said in a different way.

skimask
- 7th May 2007, 13:31
Once In_STB = 0 then you might have had half a second passed not a full second.

Or, if a full second is passed and you are in DataDone, then you might still have had In_STB = 1; then you come back immediately (possibly).

Or, having a timer module running inside a WHILE statement will keep you and your Timer module locked in that WHILE statement. Thus, having a "PAUSE 1000" will do the same job for you cause the statement is locked anyway.

--------------------------

You could check 'Pause 1' for 1000 counts...that'll only lock up the program for 1 ms at a time, and end up giving a timeout of a bit over 1 second.

sayzer
- 7th May 2007, 14:06
You could check 'Pause 1' for 1000 counts...that'll only lock up the program for 1 ms at a time, and end up giving a timeout of a bit over 1 second.


So the question is, does johnmiller want to have an independent counter running int the background? the sample code above seems to say "No".

If we follow the sample code above, then the same code logic would also work with PAUSE 1000 or Pause 1 with 1000 counts as Skimask said.

Tomato or Potaito.
or was it tomato or tomaito :)
-----------

skimask
- 7th May 2007, 14:57
So the question is, does johnmiller want to have an independent counter running int the background? the sample code above seems to say "No".

If we follow the sample code above, then the same code logic would also work with PAUSE 1000 or Pause 1 with 1000 counts as Skimask said.

Tomato or Potaito.
or was it tomato or tomaito :)
-----------

That's what I'm thinking...and since the O/P said it doesn't have to be too accurate, that's why I suggested the Pause 1 w/ 1000 counts. He'll get 1000 1ms pauses, and the extra cycles used will bring it up to over a second, but well short of 2 seconds. He can trim the 1000 counts value later easily if needed.

johnmiller
- 7th May 2007, 16:41
well I don't need it to be accurate in that time frame (0.9-2sec) but I can't pause it in the while. So my best guess it a timer, but I can't seem to be able to do it that way

skimask
- 7th May 2007, 17:09
well I don't need it to be accurate in that time frame (0.9-2sec) but I can't pause it in the while. So my best guess it a timer, but I can't seem to be able to do it that way

How long can you actually pause?
Instead of a 1,000 count Pause, you could use a 1,000,000 PauseUs. Surely that would be fast enough to do what you want to do within 5-10us, which we still don't know what that is...