PDA

View Full Version : Real-Time Issue



Shadowcasterx4ffc
09-17-2008, 10:52 PM
We are having trouble keeping real time on our BS2 board. The clock is about 3 seconds off per minute. We also tried using the DS1302 chip, but that is also inaccurate. We're wondering if it's an issue with the code that we're using:


second VAR Word
tree VAR Word
hour = 11
minute = 01
second = 00
SEROUT 13, 84, [22, 12]
DO
PAUSE 995
second = second + 1
PAUSE 5
SEROUT 13, 84, [22, 12]
SEROUT 13, 84, ["Automated Greenhouse", 13,
DEC2 hour, ":", DEC2 minute, ":", DEC2 second]
DEBUG HOME, DEC2 hour, ":", DEC2 minute, ":", DEC2 second, CR

IF second > 59 THEN
minute = minute + 1
second = 0
ENDIF
tree = 400
IF minute > 59 THEN
minute = 0
hour = hour + 1
ENDIF
tree = 800
IF hour > 24 THEN
hour = 1
ENDIF
tree = 1000
IF hour > 18 THEN
LOW 12
ELSEIF hour > 6 THEN
HIGH 12
ELSEIF hour < 7 THEN
LOW 12
ENDIF
tree = 2000
LOOP
The tree variable is just a placeholder, and does nothing in the program.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greenhouse Project

Zoot
09-17-2008, 11:09 PM
Keeping real-time with this kind of accuracy·by counting/pausing·on a Stamp is very tricky. Remember that any time you change a single piece of code you'll need to check your main loop and adjust it to keep anything close to accurate time. Also keep in mind that any if/thens change execution time depending on which "branch" of your if/then is executed. The latter can be obviated by only using constructs that always execute, e.g. instead of

IF seconds >= 60 THEN
minutes = minutes + 1
seconds = 0
ENDIF

' do this instead

LOOKUP 59 - seconds >> 15, [ minutes, minutes + 1 ], minutes

LOOKUP 59 - seconds >> 15, [ seconds, 0 ], seconds

' the idea is that the entire expression evaluates *every* time regardless of outcome, and so
' the code always takes the same time to execute

But really, an RTC such as the DS1302 or the DS1307 *is the way to go*. The Stamps are just not set up for this kind of time-base keeping.

I have found that these clocks are accurate to within a minute or so over many months, so I am curious why you say your DS3102· was inaccurate? The biggest factor in an inaccurate timebase from these clocks seems to be either mis-matched or improperly setup crystal. Noise or lack of ground plane or distance from the crystal to the chip can be factors (breadboards are not good for this --it is often better to solder the crystal right to the osc pins of the clock chip, or insert the pins with the clock itself into the socket). If you are soldering on a protoboard or the like, install the crystal right next to the osc pins and make sure to route other power/data with some distance from the crystal (if you can't do a real ground plane beneath it as detailed in the clock datasheets).

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST

1uffakind.com/robots/povBitMapBuilder.php (http://1uffakind.com/robots/povBitMapBuilder.php)
1uffakind.com/robots/resistorLadder.php (http://1uffakind.com/robots/resistorLadder.php)

Mike Green
09-17-2008, 11:15 PM
3 seconds per minute is about 5%. You have quite a lot of code that has to be executed every second and that takes time. The largest piece is the two output statements (SEROUT and DEBUG) which together output 40 characters that take about 1ms each. A BS2 executes about 4000 bytecode instructions per second and you've got (guessing) 50 to 100 that execute every second. That's 60ms or so per second which is about 5%.

Best thing to do is to adjust the PAUSE time and change that when you remove the DEBUG statement. Run this for 24 hours and see how far off it gets, then fine tune the PAUSE time to compensate. Remember that the Stamp is not designed for long term timekeeping accuracy. For that, you'll have to use an external clock. The DS1302 is much better than just a Stamp, but you have to be careful with the board layout and follow the manufacturer's recommendations. All RTCs with external crystals are sensitive to board layout and all RTCs without temperature compensation are sensitive to ambient temperature.

Shadowcasterx4ffc
09-19-2008, 10:25 PM
Do you have any suggested code to use for the DS1302? And if possible, a wiring diagram for a breadboard (soldering isn't an option right now).

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greenhouse Project

Mike Green
09-19-2008, 10:41 PM
Nuts and Volts Columns #33 and #34 discuss the use of a DS1302 with a Stamp.

Go to the main Parallax web page and select the Resources tab, then choose Nuts and Volts Columns.

Chris Savage
09-19-2008, 11:04 PM
There is also a lot of information and a template at the following link. This DS1302 Demo Code covers writing to the RAM as well.

http://forums.parallax.com/showthread.php?p=531080

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering