AN1216
APPLICATION NOTE
Implementing a Periodic Alarm with TIMEKEEPER
The TIMEKEEPER M48T59 and M48T59Y, from STMicroelectronics, each provide an 8K x 8 bit non volatile static RAM, and an integrated real time clock. Each also provides an alarm which can be set either for a given time and day, or to repeat at a certain day in every month, or at a certain hour in every day, or at a certain minute of every hour, or at a certain second of every minute. With this functionality already provided in the hardware, the software to implement an alarm of any given period is greatly simplified, as described in this document.
Although specifically tailored for the M48T59 (or M48T59Y) device, the ideas can be adapted easily to use any of ST’s other TIMEKEEPER devices that have the alarm feature. Some modifications in the MCU memory mapping (the TIMEKEEPER address space) and in the MCU register mapping (such as the pointer to register address) would need to be made.
Table 1. Typical TIMEKEEPER (M48T59) Register Map
|
|
|
|
|
|
Data |
|
|
|
|
|
|
Range |
|
Address |
|
|
|
|
|
|
|
|
|
|
|
|
Function |
(in BCD |
|
D7 |
D6 |
|
D5 |
|
D4 |
D3 |
|
D2 |
|
D1 |
D0 |
|
Format) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FFFh |
|
10 Years |
|
|
|
Year |
|
|
Year |
00-99 |
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FFEh |
0 |
0 |
|
0 |
|
10M |
|
|
Month |
|
Month |
01-12 |
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FFDh |
0 |
0 |
|
10 Date |
|
|
Date |
|
|
Date |
01-31 |
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FFCh |
0 |
FT |
|
0 |
|
0 |
0 |
|
|
|
Day |
|
Day |
01-7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
||
1FFBh |
0 |
0 |
|
10 Hours |
|
|
Hours |
|
Hour |
00-23 |
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FFAh |
0 |
|
10 Minutes |
|
|
|
10 Minutes |
|
Minute |
00-59 |
||||
|
|
|
|
|
|
|
|
|
|
|
||||
1FF9h |
ST |
|
10 Seconds |
|
|
|
Seconds |
|
Second |
00-59 |
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FF8h |
W |
R |
|
S |
|
|
|
Calibration |
|
|
Control |
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FF7h |
WDS |
BMB4 |
|
BMB3 |
|
BMB2 |
BMB1 |
|
BMB0 |
|
RB1 |
RB0 |
Watch |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FF6h |
AFE |
0 |
|
ABE |
|
0 |
0 |
|
0 |
|
0 |
0 |
Interrupt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
1FF5h |
RPT4 |
0 |
|
AI 10 Date |
|
|
Alarm Date |
|
A Date |
01-31 |
||||
|
|
|
|
|
|
|
|
|
|
|
||||
1FF4h |
RPT3 |
0 |
|
AI 10 Hour |
|
|
Alarm Hour |
|
A Hour |
00-23 |
||||
|
|
|
|
|
|
|
|
|
|
|
|
|||
1FF3h |
RPT2 |
Alarm 10 Minutes |
|
|
Alarm Minutes |
|
A Minute |
00-59 |
||||||
|
|
|
|
|
|
|
|
|
||||||
1FF2h |
RPT1 |
Alarm 10 Seconds |
|
|
Alarm Seconds |
|
A Second |
00-59 |
||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
||
1FF1h |
|
unused |
|
|
|
unused |
|
unused |
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1FF0h |
WDF |
AF |
|
0 |
|
BL |
Z |
|
Z |
|
Z |
Z |
Flags |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
February 2000 |
|
|
|
|
|
|
|
|
|
|
|
|
1/8 |
AN1216 - APPLICATION NOTE
TIMEKEEPER CONFIGURATION
The TIMEKEEPER register mapping is shown in Table 1. This is divided in two parts: the clock registers and the alarm registers.
Clock Registers
The clock registers can be configured in the C language computer program as follows:
*TIMEKEEPER_CAL |= 0x80
*TIMEKEEPER_SEC= 00 //user clock setting : seconds parameter *TIMEKEEPER_MIN= 00 //user clock setting : minutes parameter *TIMEKEEPER_HOUR= 00 //user clock setting : hours parameter *TIMEKEEPER_CAL&= 0x7F
The process for starting the clock and making the calibration adjustments are described in the M48T59 data sheet, and in application notes AN925 and AN934.
Alarm Registers
It is necessary to set the Write bit, W, at the top of the control register (at address offset 1FF8h) before proceeding to any clock modification. Modifications to the alarm registers, though, can be made at any time, with no prior changes to the control register being necessary.
The program listing, at the end of this document, contains statements to perform the following functions:
1. The Stop bit (ST, bit 7 of the register at offset 1FF9h) has to be reset to start the TIMEKEEPER oscillator
*TIMEKEEPER_SEC &= 0x7F; // reset bit D7 using a mask 0x7F
2. The Alarm Flag Enable (AFE) bit (bit 7 of the register at offset 1FF6h) is set, thereby allowing the IRQ pin (pin 26) to output the interrupt signal (active low).
*TKPER_AL_IT |= 0x80; // set bit D7 using a mask 0x80
3.The flag register (at offset 1FF0h) must be read at the beginning of the alarm updating routine. If not, the AF flag will never be released, and the TIMEKEEPER will continuously output an interrupt to the MCU, and the system will become jammed.
SOFTWARE CONFIGURATION
The program is listed on page 4. To understand its operation, it is important to distinguish between the three pointer variables, pointing to physical addresses in the hardware:
*TKPER_AL_HOUR, *TKPER_AL_MIN, *TKPER_AL_SEC and the three integer variables, used as work-space by the software:
alarm_hour, alarm_minute, alarm_second
The first three variables are pointers to the physical address of the values that are stored in the M48T59 memory device.
The three software variables are used to hold the user’s data (they specify the period of the alarm in hours, minutes and seconds). This is not the same information as is stored in the TIMEKEEPER registers, as pointed to by the pointer variables, but is used in their calculation.
The program does make use of the four Repeat bits (RPT4, RPT3, RPT2 and RPT1) that are physically located in the TIMEKEEPER device. These should all be set, except for those corresponding to fields that contain significant data. For instance, to set an alarm that repeats every 3 minutes and 45 seconds, the alarm_minutes and alarm_seconds variables would be loaded with these two values. Then appropriate values would be calculated for loading in the “Alarm Minutes” and “Alarm Seconds” fields of the alarm registers (at addresses 1FF3h and 1FF2h, *TKPER_AL_MIN and *TKPER_AL_SEC, respectively), and their Repeat bits (RPT2 and RPT1, respectively) would be reset to ‘0’. Meanwhile, the alarm_hour variable, and the “Alarm Date” and “Alarm Hour” fields of the alarm registers (at addresses 1FF5h and 1FF4h, *TKPER_AL_DATE and *TKPER_AL_HOUR, respectively) would be treated as “Don’t Care”, as
2/8
AN1216 - APPLICATION NOTE
indicated by their Repeat bits (RPT4 and RPT3, respectively) being set. This is summarized in Table 2, with the three local integer variables, alarm_second, alarm_minute and alarm_hour, used to represent the period.
Table 2. Bit Setting to Control the Period of the Repeated Alarm
RPT4 |
RPT3 |
RPT2 |
RPT1 |
Periodic alarm activated every |
|
|
|
|
|
1 |
1 |
1 |
1 |
1 second |
|
|
|
|
|
1 |
1 |
1 |
0 |
alarm_second seconds (less than 1 minute) |
|
|
|
|
|
1 |
1 |
0 |
0 |
alarm_minute minutes alarm_second seconds (less than 1 hour) |
|
|
|
|
|
1 |
0 |
0 |
0 |
alarm_hour hours alarm_minute minutes alarm_second seconds (less than 1 day) |
|
|
|
|
|
For example, to set a period of 1 hour 49 minute 35 seconds, the procedure is as follows:
RPT4 = 1
RPT3 = RPT2 = RPT1 = 0 alarm_second = 0x35 alarm_minute = 0x49 alarm_hour = 0x01
Or, to set a period of 49 minute 35 seconds, the procedure is as follows:
RPT4 = RPT3 = 1
RPT2 = RPT1 = 0 alarm_second = 0x35 alarm_minute = 0x49 alarm_hour = DontCare
SOFTWARE IMPLEMENTATION
TIMEKEEPER Data Format
TIMEKEEPER data is held as BCD (binary coded decimal). This is handled in the C programming language using the ‘unsigned char’ data type. This can be converted within the C program to other data types, such as ‘integer’, for numeric processing. Two functions are provided in the program at the end of this document for making this conversion.
–Char_To_Int: to take a BCD parameter, and to return the equivalent integer value
–Int_To_Char: to take an integer parameter, and to return the equivalent BCD value. The valid ranges for the alarm fields are summarized in Table 3.
Table 3. TIMEKEEPER Data Format
Data C language type |
Int (integer) |
Char (character) |
Char (character) |
|
Decimal |
Hexadecimal |
BCD |
||
|
||||
|
|
|
|
|
Alarm second |
0-59 |
00-3B |
0-59 |
|
|
|
|
|
|
Alarm minute |
0-59 |
00-3B |
0-59 |
|
|
|
|
|
|
Alarm hour |
0-12 |
00-0B |
0-12 |
|
|
|
|
|
Alarm Update Management
When the alarm signal is generated by the TIMEKEEPER device, it is communicated to the MCU. The MCU can monitor for this event either by polling, or by using interrupts. There are two variants of each method:
■ Polling
3/8