PDA

View Full Version : Long Delays - Is there a valid way?



rad
- 28th December 2003, 04:34
If you want to have something occur every day at the same time, is there a reliable way to code in a very long delay?

Say you want to turn on the pump once every 24 hours, or what ever. Besides using a variable and incrementing it after a small delay, is there anything more to be done here?

Desterline
- 28th December 2003, 08:19
A large delay can be created by nesting several loops, like such:

start:
for hours = 0 to 23
for minutes = 0 to 59
for seconds = 0 to 59
pause 1000
next seconds
next minutes
next hours

-your code here-

goto start

This will work, and be reliable, but it run a little slow. (I paused exactly a second, but didn't take into account the loop overhead)

Other delay loops exist, many with better accuracy than you can get with this one. Any delay loop is subject to the accuracy of the oscilator for the PIC, as such you'll never get exactly 24.00000 hours delay. It'll run fast or slow and your event will migrate around the clock as time goes by.

But I'm guessing that you actualy want the event to hapen at a set time each day. For that you need an RTC (real time clock). There are many options available. (check out the ones from Maxim, www.maxim-ic.com) Or if you have AC mains available you can set the PIC up to count the sine waves (they have excelent long-term timing accuracy, most plug-in clocks use this method).

-Denny

Darrel Taylor
- 28th December 2003, 10:10
Hi rad,

  I just recently posted an Elapsed Timer Demo in the sample code forum http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=190

This should make it really easy for you.

Your whole program could look like this.

Include "Elapsed.pbp"
Gosub ResetTime ' Reset Time to 0d-00:00:00.00
Gosub StartTimer ' Start the Elapsed Timer

Loop:
If DaysChanged = 0 then Loop
DaysChanged = 0
... Do the daily task here ...
Goto Loop
Can't get much easier than that.

Like Desterline says, the accuracy depends on the crystal. But I've found that with an HC49U type crystal, it holds to around 3-4 seconds per day. Even the DS1302/1307 has a tolerance of about 4 seconds per day so this is pretty good.

This doesn't have a battery backup, so if power is lost, the time will get reset. But then, there's a hardware solution for that too. ;)

HTH,
  Darrel

Melanie
- 28th December 2003, 11:04
Doing something every day depends on the criticality of your application. If it MUST happen exactly at the same time each day, then accurate crystals or RTC's are the way to go. If it's arbitrary (eg just running a waste pump that's clearing your sewage pit each day into your neighbours swimming pool) and it doesn't matter if it's plus or minus a few minutes, then a simple loop counter would be just fine.

I use a 16F628 with internal oscillator in an application that times about fourteen months... my accuracy is about plus/minus six hours across that period.

rad
- 28th December 2003, 20:07
Thanks all.

This is turning on a filter flush valve once a day or once every few days.

What I wanted was to do it at midnight, or say between midnight and 1am. That's as accurate as I need.

I guess what I was really wondering was could this be done or jiggered to make work. And I wanted to make the worlds cheapest device too. So I intended to use the internal resonator.

If there is an inaccuracy, will it be either gaining time or losing time? If this is the case, I can watch it, experiment, and put in fudge factor every three, or so many, loops to throw the thing back to the correct 60 minute window.

Or do the inaccuracies lead to a device that gains and loses time?

I can't imagine how the thing could be inaccurately slow for a while, then become inaccurately fast for a while. But I don't really know.

It seems like a fudge factor would be a big help in adding to the accuracy or functionality in my case.

I don't want to go all John Harrison-esque though !

Thanks.

Darrel Taylor
- 28th December 2003, 20:20
If you need to cut something, and you have a Sharp knife in your hand. Why look for a Dull one to do the job?

The Elapsed Timer Demo compiles to 149 words (without the daily task). So it easily fits into a 12F629 if you wanted. If you need the accuracy, use a crystal. If you don't, use the internal oscillator.

After a year, it should only be off by about 24 minutes or less.


I can't imagine how the thing could be inaccurately slow for a while, then become inaccurately fast for a while. But I don't really know.

The oscillator will run faster or slower depending on the ambient temperature. So indoor applications can be more stable that outdoor ones.

Desterline
- 28th December 2003, 20:26
The PIC doesn't have an "internal resonator", it has an internal R-C oscilator (some require extrnal parts) an RC oscillator is much less acurate than a resonator or crystal. It also has a much greater temperature coeficient. Meaning that the speed of the osc will be related to the ambient temperature. And yes, it could run fast and slow depending on the temp.

If the place where you intend to install this has a mostly steady temp, then you could probably get by with a "fudge factor" and get within a couple minutes a day. But remember the error will be cumulative. One minute a day is half an hour a month, six hours a year. To keep it near midnight you'll have to reset it about every 6 months.

-Denny

rad
- 28th December 2003, 22:39
OK, thanks again.

I meant interal oscillator and the ambiant temperature thing I didn't know.

I'll play around with this and let you all know what I decide and how it works.

electronicsuk
- 29th December 2003, 11:52
It's probably a bit late to mention this now, but can't you get real time clock modules that you could interface to your micro?

Matthew