Basically, I want to turn something on - like an LED (later it will actually be a 7 HP siren with a longer on and off time) for 500 ms. I go through all the decision process to decide it should be turned on. Then....
I save the current ticks value - tmr - # of ticks at start, which is 16 for this grab.
I turn on the port - for the test program, I'm just toggling an LED at 500 ms intervals.
I come out of the main program loop every pass to see if I have expired my timer. Get current ticks - ms100 - we'll say I get 65 for this grab.
Sooo... IF ms100 - tmr >= 50 THEN 65 - 16 = 49.... almost there..
Where you run into the problem is if you grab, say 80 for tmr.
For 500 ms to elapse, you'd have to roll over the 100 mark. So, if you try to do the test IF ms100 - tmr >= 50 THEN with tmr = 80, and ms100 = 10 it evaluates true because, under pic math 10 - 80 = 250, or 65391 if you look at all 16 bits. So I figured out that you have to test to see if ms100 (the current tick value) is less than tmr (the tick value when you started timing) and if so, add 100 to the ms100 before subtracting tmr to see if it's 50 or better elapsed.
Sure you do timing with pause, but you can't look at change of state while you wait for pause to expire. Your timing routine makes it possible watch inputs, and manipulate and time multiple outputs at the same time.
I used to run into this when doing timing in vbdos. Seconds was easy - if you wound up with a negative, just add 86400.




Bookmarks