Crude timing loop for my 28X project

vk6bgn

New Member
Hello All,

I am a non coder who wrote some horrible code the other day for my PICAXE 28X & serial LCD project which needed to roughly know when 2 minutes and 10 minutes expired according to a “high” on some of the inputs. I noticed that the old PICAXE 28X I have, has no timer function.(?) Timing is really not that critical so I just incremented a counter every 250 milliseconds with PAUSE 250. Example, it went something like this.... (you all know this!)

Start:
Counter = Counter + 1
Pause 250
(other code)
Goto Start

So, 4 counts per second. Then at 480 counts (2min) and 2400 counts (10min) the program goes and does what it needs to do. In the end I timed the program verses a stop watch and to my surprise, it appears the program executed about 30 seconds short of 2 minutes and 2.5 minutes short of 10 minutes! Well well! I’m not a math wiz but there possibly seems to be some sort of ratio here. Anyway, I started to adjust the “count” figures but decided not to as 480 & 2400 represented the “seconds”. So I changed the “timebase” or “clock” as you might say??? Anyway, I had to adjust the PAUSE to 264 (milliseconds) for the program to pretty much execute spot on 2 minutes and 10 minutes. I would of thought I would have had to adjust the PAUSE below 250. Possibly to 240 or 245 to compensate for execution times, overhead processing or somethig there of etc. etc. I guess not?

All this dribbling on to ask a couple question.

Question: Why is this? Is the problem (not really a problem for me) with the onboard internal 4Mhz. oscillator? If so, then the frequency might be higher then 4Mhz.??? Is the frequency way out or just a few cycles?

All in all, it's not raeally a problem. Just curiosity.

"The Addict"
 

BCJKiwi

Senior Member
If 480 pauses of a nominal 250ms come up with too short an elapsed time (1.5 Mins),
then isn't it reasonable that you would have to make the individual pause longer
to make (480 x pause) a longer time (2 mins)?

How about code like this?
'set b0 = 480 or b0 = 4800 in calling code
gosub Delay
'put this subroutine at the end of the program
Delay:
for counter = 0 to b0
pause 250
counter = counter +1
next
Return

This way you go off to the delay or counter routine and come back to your regular program, instead of doing bits of your regular program inside the loop (if that is what you are actually doing).
 

vk6bgn

New Member
BCJ Kiwi,

Yes, you’re right. I am PAUSEing and how would you say…. program execution from with-in a loop. Hum…. Not good? But then this project is pretty much about timing and I did what I thought was right and what would work for this rookie:) I’d post what I wrote but it’s probably laughable.

To the original question…..

“”… I’m still a bit mixed up by what is so slow or possibly what is so fast. I could of definitely lived with being out by a few seconds in a 2 minutes time or many seconds in 10 minutes time. I just didn’t expect to be out by 25 percent in 2 minutes””. Not that it can’t be fixed. (and is has)

I like your idea of getting PAUSE out of the loop and just have a GOSUB to go and increment a counter. I think I originally tried this because one of the problems I encountered was when leaving the main loop of the program. I had to have the Counter = Counter + 1 follow the execution of the program to 3 other loops in the program. Otherwise the timer (counter) didn’t increment. Lesson learned. (I should of known!)

While not really a problem but possibly a lesson learned was to many GOSUBS. While having fun toying around with this project for the last 2 weeks, a window popped up once and said something like “you have to may GOSUBS would you like to use 16 or 256 GOSUBS. Or something there of. I can’t remember exactly as it was more then a week ago:)

One thing I recall was a GOSUB seemed to be more memory hungry. I found by putting a Counter = Counter + 1 in four different palaces rather then using four GOSUB’s seemed to use less memory. I originally had the first running program using well over 300 bytes and I also wanted to be able to run a scaled down version of this project on a 08M chip. I’ve now toyed around with a few things and reduced it to about 202 bytes. Now that should fit on a 08M! (Wow, so many inefficiencies of a non-computer programmer!)

Hey, I even etched out circuit boards with the Press & Peel technique. And did the artwork with plain ol’ MS Paint! A great time over Xmas holidays. One thing I pinched from the net and would like to pass on (and thank you to who ever you are) is to import any .BMP “mirrored” schematic artwork into MS Word and print to a laser printer from Word, not Paint! It prints our much much better!!!

Thanks for the help.

“The Addict”
 

hippy

Ex-Staff (retired)
To the original question…..

“”… I’m still a bit mixed up by what is so slow or possibly what is so fast. I could of definitely lived with being out by a few seconds in a 2 minutes time or many seconds in 10 minutes time. I just didn’t expect to be out by 25 percent in 2 minutes””. Not that it can’t be fixed. (and is has)
You were simply not taking into account the work which has to be done other than in just the counting and pausing.

You could act as a timer for a friend; "one second ... two second ... three second", but it works because that's only what you're doing. Make that "one second ... run to the local shop and back ... two second ... run to the local shop and back ... three second", the timing is going to be far out.

What the PICAXE has to do between Pauses and and its counting is its equivalent of its shopping trip which takes time.
 

kevrus

New Member
To get a 1 second delay with my 18x driving an lcd displaying the timing countdown as well as monitoing an interrupt, I needed a pause of 765ms, less than the assumed 1000ms which corresponds with what hippy said.
If your delay timed out quicker than the theoretical delay, it does appear that your 28x is running fast. I reckon you could get a different result with every picaxe you try.

You could try an external resonator as I believe they are slightly more accurate.
 

demonicpicaxeguy

Senior Member
if the timer1 module in the PIC16F873A isn't being used by the interpreter , with some peeking and poking of the registers you could have somthing that is very accurate and works in the background
 

vk6bgn

New Member
Hello All,

Thanks for the simple explanation of the timing issue. All understood now. The 18X chip is a PIC16F88-I/P so I guess no clever PEEKing and POKEing for an accurate timer? Probably no need to as my timing issue has been corrected and everything is working great.

Thanks again,

"The Addict"
 

hippy

Ex-Staff (retired)
Your timing is as accurate as you need it so it's good enough. It is possible to use Timer1 in some cases, and even use a watch crystal, to get better accurate timing with the right software running and peeking and poking registers.
 
Top