AN1504
APPLICATION NOTE
STARTING A PWM SIGNAL DIRECTLY
AT HIGH LEVEL USING THE ST7 1 6-BIT TIMER
by Microcontroller Division Applications
INTRODUCTION
The 16-bit timer is a standard peripheral of the ST7 microcontroller family. This peri pheral can
be used for a variety of purposes, including pulse length measurement of up to two input signals (input capture feature) or generation of up to two output waveforms (output compare and
PWM mode).
This applicat ion note is abo ut using th e PWM mod e of the st andard 16 bit ti mer. It ex plains
how to synchronize the PWM signal output. In other words, how to make s ure it outputs a high
state when the counter res tarts after it ha s been stopp ed (for any reas on) or simply when it
starts at the beginning of the application. In some applications, lik e motor control, it may be essential to output the high level part of the signal duty cycle when the counter is started.
AN1504/0302 1/9
1
STARTING A PWM SIGNAL DIRECTLY AT HIGH LEVEL USING THE ST7 16-BIT TIMER
1 16-BIT TIMER PWM MODE
1.1 DESCRIPTION
In pulse width modulation mode, the frequency of the signal is determined by the value in
Output Compare 2 register (OC2R) and the pulse length by the value in the Output Compare
1 register (OCR1) or duty cycle value.
The OLVL2 bit selects the level to be applied to the output pin after a successful comparison
between the counter and the OC2R register and the OLVL1 bit selects the level to be applied
on the output after a successful comparison between the counter and the OC1R register
1.2 NORMAL BEHAVIOUR
Figure 1 shows the normal behaviour of output compare 1 (OCMP1) pin when a PWM signal
is output with OLVL2=1 and OLVL1=0.
Figure 1. PWM output when O LVL2=1 and OLVL1=0
FREE RUNNING
FREE RUNNING
COUNTER VALUE
COUNTER VALUE
Ttimer × 65535
Ttimer × 65535
OLVL1=0
OLVL1=0
OLVL2= 1
OLVL2= 1
time
time
time
time
FFFFh
FFFFh
FFFCh
FFFCh
OC2R
OC2R
OC1R
OC1R
0000h
0000h
OCMP1
OCMP1
Ouput Compare pin
Ouput Compare pin
Timer output
Timer output
Tmax =
Tmax =
When the counter reaches the OC2R register value, the value of OLVL2 is applied on the
OCMP1 pin (=1 in this case). W hen the c ounter reaches the v alue of the O C1R register, the
value of OLVL1 is applied on the OCMP1 pin (= 0). The formulas needed to compute the
values to be put in OC2R and OC1R registers are in the 16- bit Timer chapter of the ST7 datasheets. As the 16-bit Timer is reset at FFFC, the formulas are:
OCiR=((t*Fcpu)/presc)-5 where:
t= period of the signal
2/9
2
STARTING A PWM SIGNAL DIRECTLY AT HIGH LEVEL USING THE ST7 16-BIT TIMER
Fcpu= CPU frequency
presc= Timer 16 presc
Figure 2 show s the wav ef orms we see on an osc illos cope if th e PWM s ignal is ini tialized a t
10KHz with a 50% duty c ycle (OLVL2=1 and OLVL1=0) and if a flag is set as soon as the
counter is started.
Figure 2. OCMP1 waveform with 50% Duty Cycle
PWM
OUTPUT
Timer 16 start
output enabled
flag
We can see from this figure that we have to wait until the counter reaches the OC2R r egister
value to get the first high state on the OCMP1 pin. So we have lost one PWM cycle before getting the first high level and in some applications, like energizing motor windings, this is not acceptable. After a motor demagnetization phase, if the windings are not energized immediately
when the PWM is started, the motor can stall. The purpose of the following sections is to explain how to get a high level on OCMP1 pin immediately.
To solve the problem, we have to handle two cases.
1. The PWM signal is high when the timer is between F FFC (its reset value) and the OC1R
register value. This means OLVL1=0 and OLVL2=1.
2. The PWM signal is high when the timer is between the OC1R register value and the OC2R
register value. This means OLVL1=1 and OLVL2=0.
3/9