AN1602
APPLICATION NOTE
16-BIT TIMING OPERATIONS USING ST7262 OR ST7263B
MCUs
Microcontroller Division Applications
INTRODUCTION
This Application Note describes how to use the ST7262 or ST7263B for 16-bit timing opera-
tions. The intention of this document is to show how to perform pulse measurement and PWM
generation using the different timers available on each microcontroller type.
1 16-BIT TIMING MEASUREMENTS
1.1 USING ST7262
The aim of this section is to describe how to use the ST7262 8-bit timer to perform pulse and
frequency measurements with 16-bit accuracy.
1.1.1 16-Bit Capture
1.1.1.1 Principle
The ST7262 architecture allows you to cascade the 8-bit Auto Reload Timer (ART) with the 8-
bit Time Base Unit (TBU) to obtain a 16-bit counter (The carry bit of the ART acts as the clock
for the TBU). Using this configuration, the ARTCAR register represents the Least Significant
Byte and the TBUCV register represents the Most Significant Byte (see Figure 1).
Figure 1. TBU+ART 16-Bit Counter Value
TBU (MSB)
ART (LSB)
16 Bit Counter Value
On each valid input capture, the 16-bit counter value is obtained by:
– The ARTCAR register which is automatically latched.
– The TBUCV register which is saved by software in the Input Capture Interrupt routine.
This method ensures a “real-time” capture of the low bits, while the software delay needed to
save the TBUCV can be compensated.
AN1602/0503
1/21
1
16-BIT TIMING OPERATIONS USING ST7262 OR ST7263B MCUs
1.1.1.2 Delay Compensation method
The software delay introduced by the interrupt routine should be taken into account otherwise
the measurement value can be corrupted. This happens if the ART counter rolls over from FFh
to 00h while software is storing the TBUCV value after a valid input capture interrupt. In this
case, the TBUCV is incremented and a wrong value occurs in the 8th bit of the 16-bit input
capture value.
The delay compensation method is described in the following flowchart (Figure 2).
The first step consists of transferring both TBU and ART values into temporary variables
(TmpTBU1, TmpART, TmpTBU2) to be used later on. Then in the second step we compare
TmpART and ICxR. There are two cases to be considered:
– TmpART >= ICRx: This means that the ART counter value was incremented by a few cycles
but has not yet reached the maximum value (FFh). In this case we use TmpTBU1.
– TmpART < ICRx:This means that the ART counter was incremented beyond the maximum
value and has been reset. This means we should decrement TmpTBU2 once to get the right
value of TBUCV.
Figure 2. TBU Delay Compensation flowchart
Interrupt From ICAP1 or ICAP2
Read & Store the TBUCV in TmpTBU1
Read & Store the ARTCAR inTmpART
Read & Store the TBUCV in TmpTBU2
Yes
TBU
ok
= TmpTBU1
No
TmpART >= ICxR
TBU
ok
= TmpTBU2 - 1
1.1.1.3 Example of an interrupt capture routine
The input capture interrupt routine given below can be used to get the precise value of the
input capture interrupt instant on ICAP1 pin. It can be used to detect a rising or falling edge de-
pending on the configured edge in ARTICSR.
.Inp_Cap_Routine
;----------------------------------------;
Ld A , TBUCV
;Load the TBU counter value
2/21
2
16-BIT TIMING OPERATIONS USING ST7262 OR ST7263B MCUs
Ld TmpTBU1 , A
Ld A , ARTCAR
Ld TmpART1 , A
Ld A , TBUCV
Ld TmpTBU2 , A
;----------------------------------------;
Ld A , TmpART1
Cp A , ARTICR2
jrpl Case2
Ld A , TmpTBU2
Dec A
jra Next2
.Case2
Ld A , TmpTBU1
.Next2
Ld PulseEndHR , A
Ld A , ARTICR2
Ld PulseEndLR , A
Ld A , ARTICCSR
Iret
;into TmpTBU1
; Load the ART counter Value
;into TmpART1
; Load the TBU counter value into
;TmpTBU2
; Compare ART & ICR
; If ART > ICR then Case 2
;
; Case 1 : TBU = TBU2 - 1
;
;
; Case TBU = TBU1
;load TBUCV into PulseEndHR
;Load The ART Inp Capt 2 register
;Value into PulseEndLR
;Clear The input capture interrupt
;Exit From Interrupt
1.1.2 Pulse Length measurement
1.1.2.1 Measurement Principles
As first example in this application note, we will try to measure the pulse length of an external
signal applied to the input capture pins.
Figure 3. Pulse Length Measurement
Pulse Length
To perform this operation, two input captures are used together and they are configured as fol-
lows:
– Input capture1 pin (ARTIC1) configured to detect rising edges
– The input capture2 pin (ARTIC2) configured to detect falling edges
The ARTIC1 & ARTIC2 are connected together (Figure 4).
Two 16-bits variables are used to hold the 16-bits timer values on each valid input capture
edge (PulseStart for the rising edge and PulseEnd for the edge).The pulse length value can be
deduced later using a 16-bits substruction between PulseStart and PulseEnd
3/21
16-BIT TIMING OPERATIONS USING ST7262 OR ST7263B MCUs
Figure 4. Input Capture Configuration
ST7262
ARTIC1
External Signal
ARTIC2
Figure 5 shows the steps that have to be followed to successfully measure the length of a
pulse in 16-bit accuracy.
Figure 5. Input Capture Sequence
- Configure Time clock frequency
- Select edge sensitivity on ICAP pins
- Cascade PWMART & TBU
No
Input capture interrupt
YES
ICAP1 Or ICAP2
ICAP1
- Store the TBU counter Value
(Compensated value) in PulseStartHR
- Store the ICRx Value in PulseStartLR
ICAP2
- Store the TBU counter Value
(Compensated value) in PulseEndHR
- Store the ICRx Value in PulseEndLR
1.1.2.2 Example of input capture interrupt routine
.Inp_Cap_Routine
Btjt ARTICCSR,#0,Flag1
.Flag2 Ld A , ARTICR2
Ld IcapReg2 , A
Ld A , ARTICCSR
;
;
;
;
;
Test If is ICAP(1 Or 2)
Latch the Input Cap Reg into
IcapReg2
Clear Interrupt
4/21
1
16-BIT TIMING OPERATIONS USING ST7262 OR ST7263B MCUs
; Load the TBU counter value
; into TmpTBU1
; Load the ART counter Value
; into TmpART1
; Load the TBU counter value into
; TmpTBU2
Ld A , TmpART1
; Compare ART & ICR
Cp A , IcapReg2
; If ART > ICR then Case 2
jrpl Case2
;
Ld A ,TmpTBU2; Case 1 : TBU = TBU2 - 1
Dec A
;
jra Next2
;
.Case2
Ld A , TmpTBU1
; Case TBU = TBU1
.Next2
Ld PulseEndHR , A ; load TBUCV into PulseEndHR
Ld A , IcapReg2
; Load The ART Inp Capt 2 register
Ld PulseEndLR , A ; Value into PulseEndLR
Iret
; Exit from Interrupt
.Flag1 Ld A , ARTICR1
; Latch the Input Cap Reg into
Ld IcapReg1 , A
; IcapReg1
Ld A , ARTICCSR
; Clear Interrupt
Ld A , TBUCV
; Load the TBU counter value
Ld TmpTBU1 , A
; into TmpTBU1
Ld A , ARTCAR
; Load the ART counter Value
Ld TmpART1 , A
; into TmpART1
Ld A , TBUCV
; Load the TBU counter value into
Ld TmpTBU2 , A
; TmpTBU2
Ld A , TmpART1
; Compare ART & ICR
Cp A , IcapReg1
;
jrpl Case1
;
Ld A , TmpTBU2; Case : TBU = TBU2 - 1
Dec A
;
jra Next1
;
.Case1
Ld A , TmpTBU1
; Case TBU = TBU1
.Next1
Ld PulseStartHR , A; load TBUCV into PulseEndHR
Ld A , IcapReg1
; Load The ART Inp Capt 2 register
Ld PulseStartLR , A; Value into PulseEndLR
Iret
; Exit from Interrupt
Ld A , TBUCV
Ld TmpTBU1 , A
Ld A , ARTCAR
Ld TmpART1 , A
Ld A , TBUCV
Ld TmpTBU2 , A
1.1.3 Frequency Measurement
1.1.3.1 Measurement Principles
Frequency measurement is a little bit different from pulse length measurement. To get the fre-
quency, we measure the full signal period which can be defined by the time interval between
two consecutive rising edges (Figure 6)
5/21