ST AN1504 APPLICATION NOTE

AN1504

APPLICATION NOTE

STARTING A PWM SIGNAL DIRECTLY AT HIGH LEVEL USING THE ST7 16-BIT TIMER

by Microcontroller Division Applications

INTRODUCTION

The 16-bit timer is a standard peripheral of the ST7 microcontroller family. This peripheral 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 application note is about using the PWM mode of the standard 16 bit timer. It explains how to synchronize the PWM signal output. In other words, how to make sure it outputs a high state when the counter restarts after it has been stopped (for any reason) or simply when it starts at the beginning of the application. In some applications, like 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

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 OLVL2=1 and OLVL1=0

FREE RUNNING

COUNTER VALUE

 

Tmax = Ttimer × 65535

FFFFh

 

FFFCh

 

OC2R

 

OC1R

 

0000h

time

 

OCMP1

OLVL2= 1

Ouput Compare pin

Timer output

OLVL1=0

time

When the counter reaches the OC2R register value, the value of OLVL2 is applied on the OCMP1 pin (=1 in this case). When the counter reaches the value of the OC1R 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

ST AN1504 APPLICATION NOTE

STARTING A PWM SIGNAL DIRECTLY AT HIGH LEVEL USING THE ST7 16-BIT TIMER

Fcpu= CPU frequency

presc= Timer 16 presc

Figure 2 shows the waveforms we see on an oscilloscope if the PWM signal is initialized at 10KHz with a 50% duty cycle (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 register 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 FFFC (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

Loading...
+ 6 hidden pages