Link here

Battery-Backed RTC
Reading and setting the battery-backed real time clock, calendar/time of day clock


The battery backed real time clock is set and read using the Control-C functions ReadWatch() and SetWatch(). In C, these two routines access a fixed pre-allocated structure in the 68HC11's on-chip RAM using structure macros defined in the WATCH.H header file. For example, the following code reads the watch and stores the current minute (after the hour) in the variable named current_minute:

static int  current_minute;
ReadWatch();
current_minute = WATCH_MINUTE;

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 WATCH_MINUTE 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.

Interrupts are disabled during the entire operation of the functions ReadWatch() and SetWatch() for approximately 0.5 and 0.45 msec respectively.

The words that access the watch use the top 16 bytes of the 68HC11F1’s on-chip RAM (at addresses 0xB3F0-0xB3FF) as a buffer to hold intermediate results during the data transfers. You may use these locations for other purposes when the watch is not being accessed.

 
This page is about: Battery-Backed RTC, Reading and Setting Battery-backed Real Time Clock, Calendar/time of Day Clock – The battery backed real time clock is set and read using Control C functions ReadWatch() and SetWatch(). In C, these two routines access fixed pre allocated structure in 68HC11s on chip RAM using structure macros defined in WATCH.H header file. For …
 
 
Navigation