Very Accurate 18X Timing

hippy

Technical Support
Staff member
Well, as accurate as a watch crystal is.

Finally got round to trying to make an 18X work with Timer1 using a watch crystal, and it seems to work fine. Demo code to follow when I've done some more testing.

The magic is a watch crystal connected to O6 and O7 ( no caps used ), turn on Timer1 with an external LP Osc source and see it roll. By checking bit 15 changes of Timer1 a LED can be toggled every 1 second, bit 14 gives a 1Hz output.

Using CCP1, any arbitrary period up to 16 seconds can be polled or can generate a hardware interrupt, just like Timer1 was used for 18X multi-tasking.

Should be as accurate as a DS1307, and gives the capability of sub-second timing. Endless possibilities here, from 1Hz reference sources to single chip LCD clocks. Timer1 should also work through SLEEP mode so reasonably accurate wake-and-do-something every N seconds or sleep again code becomes a possibility.

[Added]

I was hoping this would work on the 08M, 14M, 18 and 18A ( the datasheets suggest it should ), but it only has on the 18A and 18X so far :-(

Edited by - hippy on 12/06/2007 04:31:09

[Added]

It doesn't seem to work through NAP on an 18X, but it does work when under-clocked by poking OSCCON, but not at maximum under-clocking.

Edited by - hippy on 12/06/2007 05:01:26
 

hippy

Technical Support
Staff member
That should work. Where the sampling interval is specified by a SLEEP or PAUSE it's a one line command to initialise Timer1 at reset, then a matter of polling a bit to see when some time has elapsed. Put that in FOR loops to get the required delay.

To replace a DS1307 solution it's a case of polling for elapsed seconds and updating variables to keep track of hh:mm:ss. The time setting Wizard would be altered to update those variables.

Unfortunately O6 and O7 are used in the AXE110, but for a homebrew design it can be done.
 

manuka

Senior Member
Hippy- "poor man's" time keeping is something I've been meaning to do for ages,as my increasingly dusty stack of coin shop watches attributes. Fancy having your ideas pinched for a Silicon Chip article? This is a near perfect schools level project of course. Stan
 

hippy

Technical Support
Staff member
You are welcome to pinch any ideas I have Manuka. The wider dissemination the better. In a few hours I should have some code published for people to play with and you're welcome to 'help yourself'.

I was surprised how easy it was. The pins are auto-configured as inputs when the Timer1 LP Osc is enabled and apparently read as zero.

I was hoping this would work for the 08M so, once programmed, the crystal across Serial In and Pin 4 ( Legs 2 & 3 ) would oscillate and not invoke Download Mode. Hasn't worked so far, so some datasheet reading and bread-boarding to try. Likewise the PICAXE-18 should have worked but didn't.
 

hippy

Technical Support
Staff member
Bingo ! Working on an 08M now as well

Needed the 2x33pF caps to 0V. The 14M should also work, and maybe the 18 with the caps fitted.

With the watch crystal wired to an 18X on 10cm wires, other wires off O6 and 07, and no caps ( not the best electrical environment ), it was accurate to the second according to my stopwatch over 60 minutes. Certainly far better than the usual +/-40 seconds per hour one sees from using the Internal RC Oscillator.

Example code for 08M, 18A and 18X at ...

http://homepage.ntlworld.com/the.happy.hippy/picaxe/lposc.zip
 
Last edited:

Michael 2727

Senior Member
Hippy, have you got any other Xtals, 1 Meg
4Meg, 8Meg and some time to experiment.
Just wondering what is the Max you could
squeeze out of an 08M ?

Makes my 08M stopwatch look like a bucket
with a nail hole, clock now ~ ;o(
Still < 2 Sec/15 Min accuracy is ok for fun.
 

hippy

Technical Support
Staff member
I'd considered hammering a high-speed oscillator module signal through Timer1 and driving the system clock from that when playing with the multi-tasking but ( for the 18X/16F88, Table 18-6, 'Ft1' ) the oscillator input for Timer1 is 32kHz max. I had hoped to run an 18X as 16MHz this way. I guess someone could always try but I don't really want to risk losing an 18X.

The 08M looks to be the same and doesn't have the option of using Timer1 as a system clock.

The accuracy is quite astounding. I have nearly finished coding an 08M-based DS1307 replacement and will add that code soon.

Looks like my one-chip 08M alarm clock is finally going to be reliable enough to use. That's been a really long project considering how simple it should be !
 

kranenborg

Senior Member
Amazing! Still one question remains: what needs to be done to trap an interrupt without external hardware?

Regards,

Jurjen
 

manuka

Senior Member
Hippy- I'm full of admiration for your zeal! This is now much too comprehensive to pinch ideas from (yeah- us colonials have ethics!), so I may just rework the watch crystal basics & give you full credit for the senior version. Stan
 

hippy

Technical Support
Staff member
Manuka : I'm afraid I got a bit carried away there.

Here's an idea - watch crystal basics, check for 1 second tick, updates HH:MM:SS, sends that via 433MHz, and another unit pulls in the time and SERTXD's to the Terminal display

Basically a DIY "Rugby"/MSF time reference.
 

hippy

Technical Support
Staff member
Jurjen : Added an LPOSC6.BAS to the download ( http://homepage.ntlworld.com/the.happy.hippy/picaxe/lposc.zip ) which uses a heart-beat LED looped-back to an input to generate a call to "Interrupt"; once every second.

Once up and running, all clock and timer handling is done in the background.

Unfortunately there's no other way to cause an interrupt without tying up two pins.
 
Last edited:

demonicpicaxeguy

Senior Member
it would be interesting to have two of these setup expose one to cooler temperatures like on a concrete floor in a garage and then have another one in a black box outside
then after a week see how much they have drifted time wise
 

hippy

Technical Support
Staff member
Plus a reference at room temperature.

I did do some searching to try and find out what accuracy should be expected from watch crystals, and it seems to be around 10ppm for 10'C-40'C, 20ppm for 0'C-50'C.

10ppm is just under a second per day, 20ppm just less than two. So around 3-10 minutes per year would be expected.

Relatively drift of two units would be easy to measure over a long enough period, but absolute drift is harder, especially given that most of our usual references use exactly the same crystals. As I've heard said; "A man with two watches never knows what time it is".

Radio-time clocks would probably be the best us mere mortals have access to unless we're lucky to have an accurate timepiece. I have no idea how accurate Teletext/Ceefax time codes are but would expect them to be NPL based.
 

demonicpicaxeguy

Senior Member
if you we're to suspend the whole thing inside somthing like a thermos with some kind of tempreture compensation i imagine you'd get very little drift with the timing
 
Top