ST AN1216 Application note

AN1216
APPLICATION NOTE
Implementing a Periodic Alarm with TIMEKEEPER
The TIMEKEEPER M48T59 and M48T59Y, from STMicroele ctronics, 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 mapp ing (such as the pointer to register address) would need to be made.
Table 1. Typical TIMEKEEPER (M48T59) Register M ap
Address
Data
Function
D7 D6 D5 D4 D3 D2 D1 D0
Range (in BCD 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 1FF6hAFE0ABE00000Interrupt 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 1FF0hWDFAF0BLZZZZFlags
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 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 c lock modification. Modi fications to the alarm regist ers, 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 oscil­lator
*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 (a t offset 1F F0h) mus t be read at the beginning of the alarm updat ing rou tine. If not, the AF flag will never be released, and the TIMEKEEPER will continuously output an inte rrupt to the MCU, and the system will become jammed.
M48T59
SOFTWARE CONFIGURATION
The program is listed on page 4. To unders tand its operation, it is important to distinguish between t he 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 poin ters to the phy sical a ddress 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). T his 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 i n the TIM EKEEPER d evice. These should a ll be se t, exc ept fo r those corre sponding to fields that contain significant data. For instance, to set an alarm that repe ats every 3 minutes and 45 s econds, the
alarm_minutes
and
alarm_seconds
variables would be loa ded with these two values. Then a ppropriate values would be calculated for load ing in the “Alarm Minutes” and “Alarm Seconds” fields of the alarm registers (at addresses 1 FF3h a nd 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 D ate” and “Alarm H our” fields of the alarm reg isters (at address es 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 Repea t bits (RPT4 and RPT3, respectively) being set. This is s ummarized i n Table 2, with the three local integer variables, period.
Table 2. Bit Setting to Control the Period of the Repeated Alarm
RPT4 RPT3 RPT2 RPT1 Periodic alarm activated every
11111 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
alarm_second, alarm_minute
and
alarm_hour
, used to represent the
SOFTWARE IMPLEMENTATION TIMEKEEPER Data Format
TIMEKEEPER data is held as BCD (binary coded decimal). This is handled in the C programming
language using th e ‘ uns igne d char’ data type. Th is can be converted within the C prog ra m to other data types, such as ‘integer’, for numeric processing. Two functions are prov ide d 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.
Tabl e 3. TIMEKEEPER Data Format
Data C language type
Alarm second 0-59 00-3B 0-59 Alarm minute 0-59 00-3B 0-59 Alarm hour 0-12 00-0B 0-12
Int (integer)
Decimal
Char (character)
Hexadecimal
Char (character)
BCD
Alarm Update Management
When the alarm signal is g enerated by the TIME KEEPER device, it is communicated to the MCU. The MCU can monit or for th is event either by po lling, or b y usi ng interr upts. T here ar e two v arian ts of eac h method:
Polling
3/8
Loading...
+ 5 hidden pages