Using the Battery-Backed Calendar Time Clock
A continuously running calendar time clock with battery backup is optionally included on the PDQ Single Board Computer (SBC). The clock's accuracy is better than +/- 2 minutes/month. Using its pre-coded C language functions, you can incorporate real-time data (that is, calendar time as year, month, date, day, and time of day) in your instrumentation or automation applications.
The board hosts a rechargeable backup battery that charges automatically while +5V is applied to the board. When +5V is not applied the battery keeps the RTC functioning. The battery does not back up the RAM on the PDQ Board or its Freescale 9S12 (HCS12) microcontroller. The length of time the battery is able to keep the RTC ticking varies a bit from board to board, but we have measured battery discharge times for typical devices and have found typical RTC retention time at 25°C should be 300 days, decreasing to 150 days at 35°C. That is, your product can sit unpowered for as long as 300 days without losing track of calendar time. As soon as it is powered up, the backup battery starts charging back to its maximum capacity. The maximum battery recharge time is 22 hours.
After each cold restart the kernel re-initializes the RTC chip to charge the battery and to use battery backup on removal of power.
Setting and reading the real time clock with built-in software drivers
The built-in library functions SetWatch and ReadWatch declared in C:\MosaicPlus\c\libraries\include\mosaic\WATCH.h make it easy to set and read the real time clock. The SetWatch and ReadWatch functions use an 8-byte watch_results
structure in reserved system RAM that contains the results returned by the most recent call to ReadWatch as described below.
Because the structure that is written to by ReadWatch is at a fixed location, this code is not re-entrant. This is not a problem in single-task applications or applications where only one task uses the watch. But it can cause problems if multiple tasks are executing ReadWatch. For example, assume that task #1 calls ReadWatch(), but before it can access the contents of the structure using the assignment statement, the timeslice interrupt occurs and task#2 proceeds to call ReadWatch, then the contents of the structure could be changed before task#1 is able to execute its assignment statement. To avoid this situation, the multitasking application can be configured so that only one task calls ReadWatch and SetWatch and shares the data with other tasks.
Another option is to define a resource variable to mediate access to the watch. The routines needed to accomplish this are declared in the MTASKER.H
file and described in detail in the C Function Glossary (A-H). The following brief example illustrates how to design a re-entrant function that returns the current WATCH_MINUTE:
RESOURCE watch_resource; // declare resource variable: controls // watch access _Q int CurrentMinute( void ) // reads watch, returns current minute { int minute; GET( &watch_resource ); // get access to watch; Pause() if // another task has it ReadWatch(); // updates contents of watch structure minute = WATCH_MINUTE; // you can also transfer other contents // from the watch structure to // "task-private" variables here RELEASE( &watch_resource ); // release access to watch so other // tasks can use it return minute; }
The CurrentMinute function can be simultaneously called from multiple tasks without causing any conflicts. The GET and RELEASE macros automatically mediate access to the watch, ensuring that only one task has access at a time.
The battery-backed clock is pre-set at the factory to Pacific Time in the United States. To re-set the smart watch to your time zone, your program can call the function:
void SetWatch(hundredth_seconds,seconds,minute,hour,day,date,month,year)
The interactively callable routine named SetTheWatch is defined in the TIMEKEEP.C file so you can set the watch from your terminal. As explained in the C Function Glossary (A-H), the hour parameter ranges from 0 to 23, the day from 1 to 7 (Monday=1 in this example), and the year parameter ranges from 00 to 99. For example, if it is now 10 seconds past 5:24 PM on Wednesday, May 26, 2009, you could interactively set the watch by typing:
SetTheWatch( 0, 10, 24, 17, 3, 26, 5, 9 )↓
The watch is set and read using 24-hour time, where midnight is hour 0, noon is hour 12, and 11 PM is hour 23. Note that you may assign any day of the week as day number 1.
The software function ReadWatch writes the current time and date information into the watch_results sructure. Pre-coded macros name the structure elements so it is easy to access the time and date information. For example, the following simple function in the TIMEKEEP.C
file reads the smart watch and prints the time and date:
_Q void SayDate( void ) // prints time and date; does not print day of week; // you can add this if you wish. { ReadWatch(); // results are placed in watch_results structure iprintf( "\n\nIt is now %.2d:%.2d:%.2d on %.2d/%.2d/%.2d.\n\n", WATCH_HOUR, WATCH_MINUTE, WATCH_SECONDS, WATCH_MONTH, WATCH_DATE, WATCH_YEAR ); }
It simply calls ReadWatch, and then executes a printf statement using the pre-coded structure macros to reference the time parameters in the watch_results structure. If you called this function immediately after setting the watch as described in the prior section, the response at your terminal might be:
It is now 17:24:31 on 5/26/09.
After compiling a Timekeep Demo project, you can interactively type at your terminal:
SayDate( )
at any time to see a display of the current time and date.
For backwards compatibility with the QED product line, the hundredths of a second field is present but it is not used. The hundredths of a second input parameter is required for SetWatch but it is ignored, and it is always returned as a 0 value by ReadWatch.
RTC lifetime and temperature range
The real time clock runs continuously, even when the controller board is not powered. To stay ON during the board's OFF time, the RTC uses a 3V, 7 mA-h vanadium pentoxide/lithuim rechargeable battery available from Panasonic as part number VL1220. We have found that when the battery is not recharged a typical RTC retention time at 25°C is 300 days or greater, decreasing to 150 days at 35°C. The battery is trickle charged whenever the controller board is powered and requires 22 hours to be fully recharged. The battery is specified for operation over a -20°C to +60°C temperature range. Battery lifetime may be compromised if continuously operated at more extreme temperatures than those specified. For more information, feel free to consult the datasheets for the RTC and battery:
- Maxim/Dallas DS1305 real time clock chip
- VL1220 datasheet for lithium-vanadium rechargeable battery
- VL1220 product guide for lithium-vanadium rechargeable battery
See also → A Turnkeyed C Application Program