AN974
APPLICATION NOTE
By MCD Application Team
The purpose of this note is to present how to use the ST7 Timer output compare function. As an application example, this document presents how to generate a real time clock with counters for seconds, minutes and hours, based on a fixed basic time base.
The ST7 timer output compare function can be used to generate the fixed time base.
The application example is managed using the output compare interrupts. The ST7 timer has two output compare functions whose interrupts are both enabled by the OCIE bit in the CR1 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 choose the maximum time base value that has to be multiplied to obtain one sec. For this reason the maximum timer clock divider ratio (1/8) is selected by the CC1 and CC0 bits in the CR2 register.
To maintain a fixed elapsed time between each Output Compare 1 interrupt generation, the Output compare registers (OC1HR, OC1LR) have to be updated with a fixed offset time base at each interrupt.
(OCxHR, OCxLR ) = (OCxHR, OCxLR) + (OCxHR OCxLR )offset
Caution: As the ST7 can only handles 8-bit data and as the output compare function is inhibited between the write in the high (OCxHR) and the low (OCxLR) output compare registers, special care has to be taken when the updating of the 16-bit compare register. A typical algorithm flowchart is shown in Figure 1.
AN974/0303 |
1/6 |
1
Real time clock with ST7 Timer Output Compare
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 fCPU is 4-MHz, Table 1 gives some basic possible time bases with the Output compare register offset value (OCxHR, OCxLR).
ftimercnt = |
fCPU |
= |
|
4 MHz |
|
= 500 KHz |
|
T |
|
= 2 us |
|||
|
|
|
|
|
|
timercnt |
|||||||
|
|
|
|
||||||||||
|
|
||||||||||||
|
(cc1,cc0) |
8 |
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
||||
|
|
TimeBase |
|
TimeBase |
|
|
|
|
|||||
(OCxHR OCxLR )offset = |
|
|
= |
|
|
|
|
|
|
|
|||
|
|
|
2 us |
|
|
|
|
||||||
|
|
|
|
Ttimercnt |
|
|
|
|
|
|
Time Base [ms] |
2 |
10 |
20 |
50 |
100 |
131.07 |
|
|
|
|
|
|
|
OCxHR OCxLR offset [hex] |
03E8 |
1388 |
2710 |
61A8 |
C350 |
FFFF |
|
|
|
|
|
|
|
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