PIC Real-Time Clock

Up until now I’ve resisted adding to the millions of “Blink an LED” projects that are out on the web.  That is not to say that I didn’t actually make a “Blink an LED” project when I first started messing around with the PIC because I did.  Well, the time has come for me to present a project that looks suspiciously like “Blink an LED” but it really isn’t.  The fact that it blinks LEDs is just peripheral to the real focus which is using an external crystal oscillator as a building block for real-time circuits.

Hardware

Real-Time Clock

Up until now, all of the PIC projects I have presented have used the internal oscillator.  The rationale is that none of what we have done so far has been time critical.  So, when it comes to making a decision to add the extra parts for an external oscillator, I determine if I need a faster clock than what is internally available or if I need something that will provide an accurate real-time clock.  In the diagram above, the chosen crystal seems to be an oddball frequency but if you divide it down you find that it is a binary multiple of 1-Hz.  I got a package of 10 for next to nothing from a China supplier on eBay.  The required 22pf capacitors are also very inexpensive.  In the simple circuit above, we use the 16-bit Timer1 of the PIC to generate a 2-Hz frequency.  How we do that and what we do with it are detailed in the software section.

Software

The software link is listed below.  While it is targeted for the 12F683, it is easily ported to bigger versions of the PIC.  Mostly it requires changing names like TRISIO to TRISA, and GPIO to PORTA.  You will also need to change the line that identifies the PIC version (LIST=) and the INCLUDE file but those are intuitive changes.  The __CONFIG line may also need tweaking just because one or two of the labels used are spelled differently in some of the INCLUDE files.   The only difference in this _CONFIG line from previous 12F683 projects is that the entry for using the internal oscillator has been replaced by the entry _FOSC_HS (high speed external oscillator).

The Timer1 overflow is actually handled as an interrupt but it would be easy enough to just keep track of the setting of the interrupt flag in the main routine without generating an interrupt.  The interrupt frequency is 2-Hz.  That frequency is derived by setting the Timer1 control to use the oscillator frequency / 4 input, to pre-scale that input by 8, and to interrupt when the timer overflows at 65536 counts [4194304 Hz / (4*8*65536)].

The output on GP0 is turned on during even numbered interrupts and turned off during odd numbered interrupts.  That generates a nice 1-Hz pulse rate.  The software counts the number of 1-Hz pulses (seconds) and uses that to turn on the GP1 output once every minute.  The minutes are counted in order to turn on the GP2 output once every hour.  The duration of each of the output pulses is 500ms (the time until the next interrupt).  The LEDs on GP0, GP1, and GP2 are just there as visual indicators in our example.  That’s it for this post.  Check out my other electronics projects.

Crystal Oscillator