

- #Real time clock with seconds manual
- #Real time clock with seconds code
- #Real time clock with seconds plus
The seconds count matches for at least 10000 seconds when using the two external oscillators. #pragma config FNOSC = PRIPLL, POSCMOD = EC, OSCIOFNC = OFFĪnd connecting a 8MHz oscillator to OSC1 (bottom of timers page) makes the ISR-driven PT_GET_TIME() millisecond count almost as accurate as the RTC. Switching SYSCLK to use the primary external oscillator input in the config_1_3_2.h file If it reads something like 1.004 Hz, then the oscillator setup is not correct! The oscilloscope measure function should give exactly 1.000 Hz. I used the one second output time pulse from the RTC to make sure the oscillator was set up correctly. Even if the oscillator is accurate and stable, there are small systematic errors which can creep in. BUT you must test the elapsed time on the RTC.

The RTCC is the only way to get a reliable, long term clock. So the thread time is completely useless for a realtime clock. The ISR-driven timer would be about a day off after a year of real-time (and in fact overflows after about 2 months). This number is within the published error range of the internal RC oscillator.
#Real time clock with seconds code
This time typically runs slow because of thread overhead, which for this code is dozens of mSec. The number of seconds as measured by a thread with a 1000 mSec sleep between executions.
#Real time clock with seconds plus
Then reads three different time estimates, plus clock time: You can see that driving SOSCO makes a direct connection to the clock output schmitt trigger.Īn example code turns on the clock, sets the time/day at boot-time, sets up a 1 second RTCC pulse on RB3, The schematic of the internal circuitry (below) shows a tristate-driver which is controlled by FSOSCEN, and a resistor which has a value of around 2 Mohm. With the oscillator turned off, the internal circuitry floats this output. Instead, you wire the external oscillator to the output pin of the internal oscillator, SOSCO (RA4) through a 1Kohm resistor. The internal oscillator is not actually used.

Remember that you must make sure the config_1_3_2.h file is set so that the secondary oscillator is turned off: This part draws 2 microamps and has a frequency accuracy of ☒0 ppm max (equivalent to ☑0 min/year).Īccording to the data sheet, most parts fall in the range ±5 ppm when maintained at about 20-25 C (equivalent to ☒ min/year). This part draws about 150 microamps and has a frequency accuracy of ☒ ppm (equivalent to ☑ min/year).Ī smaller, lower power part is the SiTIME SIT1630AI-S4-DCC-32.768E (digikey 1473-31301-1-ND) For example digikey part number DS32KHZS#T&RCT-ND. Accuracy is then determined by the external crystal, in the range of ±5 to 50 ppm. (For comparison, the internal oscillator draws about 25 microamps ( datasheet TABLE 29-7). Instead of using a crystal, I strongly suggest that you use an external oscillator module which can be connected to SOSCO (RA4) through a 1K resistor. Note that the Day-of-the-week is zero based, but month is 1-based.

The details of the BCD storage are shown below.
#Real time clock with seconds manual
See the reference manual chapter and PLIB chapter 19 for details on time/date format and setting the time. Setting up the RTCC is easy, but remember that the time and date registers store data in BCD format! The lowest 4 bits store the first decimal digit, the next 4 bits the second decimal digit, so that decimal 10 is stored as 0x10 and decimal 18 is stored as 0x18. It is easier to set up and more stable, and may draw less power. Rather than use this circuit, use the oscillator system described below. You will probably have to tune the frequency using different capacitors, and the one second pulse as discussed below. The crystal circuit is shown below but is very sensitive to stray capacitance. Typically, each side of the crystal would have a 12 pf capacitor to ground and would be connected to SOSCI and SOSCO (pins RB4 and RA4). Note that most solderless prtotype boards have too much capacitance for an external crystal, which requires around 12 pf of load capacitance. The datasheet suggests using a crystal, but don't! The choice of capacitors is too critical for easy prototyping. The realtime clock and calender (RTCC) maintains time-of-day and date based on an external 32768 Hz crystal or oscillator.
