ST AN974 Application note

AN974
APPLICATION NOTE

Real time clock with ST7 Timer Output Compare

By MCD Application Team

1 INTRODUCTION

The purpose of this note is to present how to use the ST7 Timer output compare function. As an applica tion exam ple, this do cumen t present s how to g enerate a re al time clo ck with counters for seconds, minutes and hours, based on a fixed basic time base.

2 GENERATING A TIME BASE WITH ST7 TIMER

The ST7 timer output compare function can be used to generate the fixed time base.

2.1 TIMER CONFIGURATION AND INITIALIZATION

The application example is managed using the output compare interrupts. The ST7 timer has two output compare func tions whos e interrupts ar e both enabled by the O CIE bi t in the CR 1 register. In this case, the Output Compare 1 (OC1) is used for time base generation while the second one (OC2) is ignored when it generates an interrupt each time the free running counter value matches the value in the OC2 register.
To minimize the CPU load, we need to c hoose the ma ximum tim e b ase value tha t has to be multiplied to obtain one sec. For this reason the maximum timer clock divider ratio (1/8) is se­lected by the CC1 and CC0 bits in the CR2 register.

2.2 OUTPUT COMPARE UPDATE

To maintai n a fi xed ela psed tim e b etwee n e ach O utp ut Compa re 1 interrupt ge nerati on, th e Output compare registers (OC1HR, OC1LR) have to be updated with a fixed offset time base at each interrupt.
(OCxHR, OCxLR ) = (OCxHR, OCxLR) + (OCxHR OCxLR )
Caution: As the ST7 can only handles 8-bit data and as the output compare function is inhib­ited between the wri te in the high (OCxHR) and the low (OCx LR) output co mpare register s, special care has to be taken when the updating of the 16-bit compare register. A typical algo­rithm flowchart is shown in Figure 1.
offset
AN974/0303 1/6
1
Real time clock with ST7 Timer Output Comp are

Figure 1. Output Compare Register Updat e Flowch art

STORE OCxHR AND OCxLR REGISTER
VALUES IN TEMPORARY REGISTERS
UPDATE OCxHR AND OCxLR VALUES
THROUGH TEMPORARY REGISTERS
STORE NEW OCxHR REGISTER VALUE
STORE NEW OCxLR REGISTER VALUE
Based on the assumption that f
is 4-MHz, Table 1 gives some basic possible time bases
CPU
with the Output compare register offset value (OCxH R, OCxLR).
f
f
timercnt
=
CPU
(cc1,cc0)
(OCxHR OCxLR )
offset
4 MHz
=
8
TimeBase
=
T
timerc n t
=
500 KHz
TimeBase
=
2 us
Table 1. Output Compare Register and Timebase Values with f
Time Base [ms] OCxHR OCxLR offset [hex]
2 10 20 50 100 131.07
03E8 1388 2710 61A8 C350 FFFF

3 REAL TIME CLOCK APPLICATION

timercnt
T
timercnt
at 500KHz
= 2 us
The real time clock application is based on four software counters (100ms, seconds, minutes and hours) updated by a 100ms ST7 timer output compare 1 interrupt.
2/6
2
Loading...
+ 4 hidden pages