Link here

The Battery-Backed Real-Time Clock and Beeper

How to use the battery-backed real-time clock (RTC) on on the QScreen Controller

A battery-backed Real-Time Clock (RTC) is optionally available for the QScreen Controller and is included with the QScreen Starter Kit. The RTC is distinct from the real-time programming functionality provided by the QScreen's QED-Forth real-time operating system. You may,

  • Use the RTC to keep track of events in calendar and clock time;
  • Use built-in software drivers to read or set the RTC anytime.

The QScreen Controller combines an embedded computer based on the 68HC11 microcontroller with a touch panel and LCD (liquid crystal display) graphic user interface (GUI) that is ideal for instrument control and automation. A Real-Time Clock is included with the QScreen Starter Kit. The accuracy of the clock is better than +/- 2 minutes per month. A 7 ma-hr Lithium rechargable battery is used for the clock. This backup battery charges automatically while +5V is applied to the QScreen.


128 KB battery-backed RAM

Presently-sold QScreens1) provide a 512 KB RAM that is not battery-backed. Older QScreens that use a 128 KB RAM chip battery-back the RAM. Battery backed RAM is useful for preserving modal and operating information between power sessions. You should not rely on battery backed RAM for essential system code, as any battery will always ultimately fail. Instead, it should be used for data logging or to preserve status information between power sessions, so long as checksums are used to verify the validity of data on power-up and a hard initialization routine is used if needed to revert to default status data.


RTC battery retention time

The length of time the battery lasts varies from board to board primarily because diodes, RAM chips, and the RTC itself vary device-to-device in their leakage currents. Their leakage currents also depend strongly on ambient temperature so that while a battery may last a long time at normal or low ambient temperatures, at greater temperature it may discharge more quickly.

We have measured battery discharge times for typical 128 KB devices and have found the following:

  • Assuming the "typical" current specifications of the RAM data sheets the battery should last 139 days between charging; and,
  • At the worst case current specification for the RAM, battery life would be 44 days at normal temperatures, and as little as 6 days if operated continuously at an ambient temperature of 70°C.
  • If the RAM is not battery backed, but the RTC installed, typical RTC retention time at 25°C should be 300 days, decreasing to 150 days at 35°C.

The following graph illustrates the dependence of battery lifetime between charges while backing up the 128K RAM and powering the RTC, and ambient temperature.

real time os battery backup

Battery discharge time as a function of ambient temperature.

After each cold restart the kernel re-initializes the RTC chip to charge the battery and to use battery backup on removal of power. The battery recharge time is about 22 hours. To preserve RTC operation, a good rule of thumb is that your instrument should be powered, so that the battery trickle charges, for at least one day of every 90 days.


Setting and reading the Real Time Clock

The built-in library functions SetWatch() and ReadWatch() make it easy to set and read the real time clock. The SetWatch() and ReadWatch() functions use the top 16 bytes of the 68HC11F1’s on-chip RAM as a buffer to hold intermediate results during the data transfers, and the top 8 bytes at addresses 0xB3F8-0xB3FF serve as the watch_results structure 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 Control-C Glossary. 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.

All of the input and output parameters of SET.WATCH and READ.WATCH are passed on the Forth data stack. Because they are re-entrant they can be simultaneously called from multiple tasks in a single QED-Forth application without producing any conflicts.

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 Control-C Glossary, 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, 2004, you could interactively set the watch by typing:

SetTheWatch( 0, 10, 24, 17, 3, 26, 5, 4)↓

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 function ReadWatch() writes the current time and date information into a structure named watch_results that occupies the top 8 bytes of on-chip RAM at addresses 0xB3F8-0xB3FF. 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)
{   ReadWatch();     // results are placed in watch_results structure
printf("\nIt is now %d:%d:%d on %d/%d/%d.\n",

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/04.

After compiling TIMEKEEP.C, 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 value is required for SET.WATCH SetWatch() but it is ignored and it is always 0 from READ.WATCH ReadWatch().


The on-board beeper

An onboard beeper produces a tone under software control. To turn on the beeper you just write a 1 to a bit in a control register (just a simple address). In the C language you can use the function SetBits() to turn ON and ClearBits() to turn OFF the beeper as shown in the following routine. This code turns the beeper ON for just 2 milliseconds:

// define the beeper register
#define  BEEPER_REGISTER   ( (xaddr) 0x00C002 )
_Q void short_beep( void )

You can install a remote beeper and direct the control signal for it using jumper J10. To enable the on-board beeper install a jumper on J10 at the "On-Board" location; to enable a remote beeper install the jumper at the location marked "Remote". You can then connect your remote beeper between pins 7 and 8 on the remote mount header, H11. The header is provided for remotely mounting a contrast control potentiometer and/or a beeper. You should use a magnetic beeper with a coil resistance around 40 ohms or greater and a resonance in the range of 1-3 kHz. Soberton part number WT-1205 from is ideal. Even though that part is marked as polarized, you can connect it in either polarity.

QScreens using 128 KB RAM include -MR- in their serial numbers, while newer QScreens with 512 KB RAM include -2MR- in their serial numbers.
This page is about: Using Battery-backed Real-time Clock (RTC), Setting Watch (RTC), Reading Watch (RTC), Rechargeable Lithium Battery – How to use the battery-backed real-time clock (RTC) to access time of day, date, month and year on the QScreen Controller. rechargeable lithium battery