Real time clock on 28X2 or 40X2 using the secondary oscillator

matherp

Senior Member
This trivial little code snippet shows how to use a standard 32.768KHz clock crystal wired on to the secondary oscillator of a 28X2 or 40X2 (pins C.0 and C.1) to create an accurate time counter and real time clock without needing a DS1307 and with minimal processor overhead. Obviously after setting up the timer pins c.0 and c.1 must not be impacted by commands such as "dirsc"

Code:
'
' example to show how to get an accurate real time clock unrelated to processor speed on a 28X2 or 40X2
' uses a 32.768khz clock crystal across C.0 and C.1, with the usual 22pf capacitors to ground, driving timer3
' note the timer tick rate is unrelated to the main processor clock allowing perfect timekeeping
' the basic signal 32.768khz input creates a two second update in the picaxe timer3 variable which
' is incremented when the internal TMR3 register rolls over (65536/32768/khz = 2 second)
' reading the internal timer register allows for greater discrimination of time. In this example
' bit 7 of the high byte gives resolution to 1 second
'
' Use of this to create a real time clock
'
#picaxe 28X2
setfreq em64
'
symbol hours = b1
symbol minutes = b2
symbol seconds = b3
'
hours=12
minutes=57
seconds=00
tmr3setup %10001001 ; timer3 on, 1:1 prescalar, enable secondary crystal as timer source
b0=seconds/2
timer3=hours*60+minutes*30+b0 'time since midnight in 2 second increments (0-43199)
do 
  pause 500
  if timer3>43199 then: timer3=timer3-43200 :endif ' must be called within 12 hours of midnight ((65536-43200)/1800=12.4)
  hours = timer3/1800
  minutes= timer3//1800/30
  seconds=timer3//1800//30*2
  peeksfr $b3,b0 ' get the high byte of the internal timer 3 register
  b0=b0>>7 ' move bit 7 to bit 0 to give 0 or 1 seconds
  seconds=seconds|b0 ' OR this in to the seconds to complete the 1 second accuracy
  debug ; display time
loop
 
Last edited:

chipwich

Member
Very nice, and a great way to add a real-time clock to a project with minimal overhead in HW and SW. I only wish that tmr3 was available beyond the X2 chips.

Does using a DS1307 have any benefits over your code aside from the Hr/Min/Sec housekeeping functions?

I tried implementing a RTC solely in software on an 08M2: http://corticalcafe.com/picaxe_simplest_clock.html . Fun project, but for accuracy and efficiency, your method is far better!
 
Last edited:

westaust55

Moderator
Does using a DS1307 have any benefits over your code aside from the Hr/Min/Sec housekeeping functions?
Yes, for those who need the functionality, there is:

1. An output pulse which can be set for 1 second intervals or other intervals, and
2. There is 56 bytes of battery backed RAM (if you have a 3V back upo battery connected).

The RAM is discussed rather infrequently but can provide additional storage space and does not have the write limitations of EEPROM in terms of delay after writing or life (as in the number of writes).
 

binary1248

Senior Member
I am building a wooden gear clock but am willing to cheat and use a picaxe to generate accurate 1 sec pulses to advance the escapement once per second. No winding the clock daily, and provides considerable accuracy over traditional wooden gear clocks. As part of the cheat, the electronics will be hidden but a nice one second tick will be heard)
The code above is great and I have it working on my picaxe development board using a 32.768 Khz crystal.
I will be using 40X2 since I several on hand for now.
Two questions pop out as I am confused by two entries in the code above.
peeksfr $b3,b0 ' get the high byte of the internal timer 3 register
I can't find a good description on this peeksfr $b3,b0 , not the peeksfr, but $b3, and it says high byte, whats the low byte peeksfr look like.
.
Another question is he used setfreq em64, isn't this for an external resonator ? seems to work without this setfreq using internal resonator)??
.
Otherwise I have a good understanding of his code and think it is outstanding.
.
Many thanks, I'll post a picture of the clock when I get it going, right now I am learning to make wooden gears.
Paul
Gears001.JPG
 
Last edited:

hippy

Technical Support
Staff member
I can't find a good description on this peeksfr $b3,b0 , not the peeksfr, but $b3, and it says high byte, whats the low byte peeksfr look like.
You have to look in the native PICmicro datasheets at the Special Function Register Map.

The high byte of the Timer 3 counter is "TMR3H" and that is show as residing at location FB3h, or simply $B3 when referenced in a PEEKSFR or POKESFR.

The low byte is similarly "TMR3L" and that is at FB2h or $B2.

That TMR3H and TMR3L are the high and low bytes of the Timer 3 counter is detailed in the PICmicro datasheet section on timers.
 

techElder

Well-known member
Possibly the OP is confusing the locations "$B3" and "$B2" with the PICAXE variables "b3" and "b2"?
 

binary1248

Senior Member
That TMR3H and TMR3L are the high and low bytes of the Timer 3 counter is detailed in the PICmicro datasheet section on timers.
Thanks hippy, kinda wondered I might have to dig out some pic microchip data sheets. Used to program these chips in assembly where I worked, but now that I am retired I find these PicAxe basic chips are so much fun and easy.
.
Possibly the OP is confusing the locations "$B3" and "$B2" with the PICAXE variables "b3" and "b2"?
Not at all, I understood it was a special function register for the timer3, but didn't understand where the hex address came from.
Thanks for posting.
Paul
 
Top