Clock with timer interrupt0

Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2007

    Default Clock with timer interrupt0

    I have problem with the stability of my clock using 12F625. I have 4 mhz crystal with 2 x 22 pF.
    My assumption was, that instruction takes 1 microsecond. Timer0 has 8 byte = 256 microsecond. I have prescaler 1:256 = 65536 microseconds.

    So my code looks like:

    day VAR BYTE ' Define day in the week
    hour var byte ' Define hour variable
    minute var byte ' Define minute variable
    second var byte ' Define second variable
    ticks var byte ' Define pieces of seconds variable
    command var byte
    SERIALBYTE var byte

    OPTION_REG = $07

    on interrupt goto intManagement
    WHILE 1
    if manageIntMinute == 1 then
    if (manageIntMinute // 10) second = second - 1
    manageIntMinute = 0
    minute = minute + 1 ' manage time update
    if (LIGHTCOUNTER > 0) then
    if (LIGHTCOUNTER == 0) then low PORTA.2
    If minute >= 60 Then
    minute = 0
    hour = hour + 1
    If hour >= 24 Then
    hour = 0
    day = day + 1
    if day >= 7 THEN day = 0
    serin porta.3,2,10, cont, command 'T9600
    serin porta.3,2,5, cont, SERIALBYTE 'T9600
    serout porta.0,2,["R ", #command, " ", #SERIALBYTE, 10]
    gosub manageSerialByte

    if (INTCON.2 == 1) then
    ticks = ticks + 1 ' Count pieces of seconds
    tmr0 = 61
    If ticks < 21 Then tiexit
    toggle PORTA.1
    ticks = 0 'One second elasped - update time
    second = second + 1
    If second >= 60 Then
    second = 0
    manageIntMinute = 1
    INTCON.2 = 0
    INTCON.7 = 1

    As is seen, I will each time initialize tmr0 to 61: 256 x 61 = 15616
    So I asumed 20 times * (65536 - 15616) = 998400 microseconds.
    So I have counted that hour has 3600 seconds, the error is 3600 x 0,001600 = 5,76 second per hour so each 10 min one sec down...

    But the problem is, that my clock is not running like that, it looks like, that each minute i will loose about 7 second.
    Any ideas?

  2. #2
    Join Date
    Aug 2006
    Look, behind you.

    Did you find this post helpful? Yes | No


    Hello boban,
    I am getting a little rusty being away from here, while at home. Preload your timer with a value to compensate for the error. As I recall you lose a few cycles each time the timer is called and the preload will offset that. instead of tmr0 = 61 try a larger value until it keeps time.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Jul 2003

    Did you find this post helpful? Yes | No


    Looks like you have a good xtal... wait until you find a bad one...

    Most xtals are 4.0 MHz... their accuracy isn't 4.000000000 MHz, some could even be 4.09 or 3.90 MHz.

    You want to spend $20 on a xtal, you'll get better accuracy, but what are you asking from a 20 cent device? Put in a 30pF trmmer capacitor and trim for exactly 4.0000000MHz with a 10-digit Frequency Counter phase-locked to a 10MHz Atomic Standard. Oh... don't forget to enclose the xtal and PIC in a temperature controlled oven +/- 0.1C as well... and then, and only then, you can start getting accuracy to better than 10 seconds per DAY.

    Lets face it, I've used a Dallas/maxim DS1307 (which costs a fortune) and a quality EPSON 32kHz xtal, and I whoop for joy if I get an accuracy better than 15 minutes in a YEAR.

    All this timekeeping using microprocessors is complete rubbish. You need to lock it to a high quality, high stability clock source for any kind of accuracy even approaching your cheap 99 cent Laser-Trimmed, digital wristwatch.

  4. #4
    Join Date
    Jul 2005

    Did you find this post helpful? Yes | No


    7 seconds a minute is 11.6% error, probably not a crystal error but the code.

    Changing timer zero's count is very funky to do as there is a built-in two instruction cycle delay before it starts counting again. The last timer I built (2% accuracy using a resonator) uses a Timer 2 as the time beat. Timer 2 is preferable as it has a compare value; I have the same clock (1MHz) and load PR2 with 250 so I get a steady evenly spaced interupt every .25 miliseconds without having to reload any registers.

    That's kind of fast for once a sec updating but you also have pre and post scaler to work with.

    BTW, what processor are you using? Microchip says "your search 12F625 did not match any documents. please refine your search."

  5. #5
    Join Date
    Sep 2005
    Campbell, CA

    Did you find this post helpful? Yes | No


    If you are running from AC, You can get good accuracy by timing the 50 or 60Hz from the power transformer. If the AC goes away, you can turn on your
    timer interrupt.
    Just like the little alarm clocks with battery backup.
    Charles Linquist

  6. #6

    Did you find this post helpful? Yes | No


    The comment about using the AC line frequency is pretty sharp - The Hammond Clock company used to sell AC powered clocks with synchronous motors, and they would make gifts of these clocks to operators of the power utilities. The power plant operators would then have an incentive to regulate the AC line frequency.

    I think in your case, you should look into a real-time clock IC. There are many of these to choose from, and you can interface to them using a variety of techniques. As has been stated in this thread, trying to use the counter in a PIC to keep accurate time in the long term is a waste of, um, time.

  7. #7
    Join Date
    Apr 2006
    New Hampshire USA

    Did you find this post helpful? Yes | No

    Smile Search and Research

    Darrel Taylor's
    A better Search tool for the Forum


    Paul Borgmeier's
    Easy and Accurate Clocks without RTC IC
    Ohm it's not just a good idea... it's the LAW !

Similar Threads

  1. Single digit 7 Seg LED clock - PIC16F88
    By thirsty in forum Code Examples
    Replies: 4
    Last Post: - 17th July 2009, 09:42
  2. Clock using Instant Interrupts
    By PICpocket in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th February 2009, 22:43
  3. clock and date without using real timer
    By eepro in forum Off Topic
    Replies: 1
    Last Post: - 21st September 2008, 03:38
  4. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 21:36
  5. Help with realtime clock and timer
    By geselaw in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 8th October 2005, 10:56

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts