ST AN590 Application note

AN590
APPLICATION NOTE
PWM GENERATION WITH ST62 AUTO-RELOAD TIMER
by Microcontroller Division Applications
INTRODUCTION
This note des cribe s how to u se th e ST 62 A uto -re load Tim er ( AR Tim er) to g ene rate a PW M signal with tunable frequency an d duty cycle. As an examp le, the genera tion of a 31.25 kHz PWM signal with duty cycle proportional to an analog input voltage is presented.
Auto-reload Timer description
This timer is an 8 bit timer/counter with prescaler. It includes auto-reload PWM, capture and compare capability with one input and one output pin. It is controlled by the following registers (8 bit):
– Mode Control Register (MC) – Status registers (SC0, SC1) – Load register (LR) – Incremental counter register (TC) – Compare register (CP) – Reload/Capture register (RC)
It can also wake up the MCU from wait mode and exit from stop m ode if an external event is present on the input pin. The prescaler ratio can be programmed to choose the timer input fre­quency F
(see Table 1).
IN
Figure 1. Aut o- reload Timer Block Diagr am
AN590/0703 1/7
1
PWM GENER ATION WITH ST62 AUT O- RELOAD TIMER
Pulse Width Modulation (PWM) Generation
Using the PWM gen eration ca pability of the AR -Timer, th e micro contro ller CPU has only to start/stop the time r and update the duty cycle. High s peed PWM signals in th e range of 100kHz can be generated. The timer clock input frequency clock f
and the prescaler ratio. The PWM signal period is controlled by the Reload register.
osc
can be selected by the oscillator
IN
F
The duty cycle is defined by the Compare register CP (see Figure 2). The register TC is incremented from RC to 255d, then reloaded at RC to count again. The
PWM output is set on the overflow of TC and reset when TC=CP. CP can have any valu e betwe en RC and 2 55d. So RC sho uld be m inimal and the presc aler
ratio as small as possible to achieve a maximum resolution. Major formulae for PWM generation are: F
IN
F
PWM
= f
/ (prescaler ratio)
osc
= FIN / (256 - RC) Duty cycle: (CP -RC) / (256 - RC) Resolution: 1 / (256 - RC) With a 8MHz oscillator and a pre scaler ratio of 1, the m aximum resol ution (1/256) leads to a
PWM frequency F
of 31.25kHz. A resolution of 1/64 allows to increase F
PWM
to 125kHz.
PWM
Table 1. Prescaler Programming Ra tio
Bit 0
PS2 PS1 PS0
Reg.SC1
0000 1 0001 2 0010 4 0011 8 0100 16 0101 32 0110 64 0111 128 1000 3 1001 6 1010 12 1011 24 1100 48 1101 96 1110 192 1111 384
PRESCALER
Ratio
2/7
2
Figure 2. PWM Timer Operation
PWM GENERAT ION WITH ST62 AUTO-RELOAD TIMER
Example 1 :
Target: Generate a 12kHz PWM signal with a duty cycle of 37%, using an oscillator frequency of 4MHz:
We want to generate a periodic signal at a frequency of 12 kHz and a duty cycle of 37% . The CPU frequency (quartz frequency) is 4 MHz. Let’s try with a prescaler ratio of 3:
= 4 MHz / 3 = 1333.33 kHz
F
IN
12 kHz = 1333.33 kHz / (256d - RC) gives RC = 144.889, rounded to 145 Resolution = 1 /(256 - 145) = 1 / 111 Duty cycle desired: 37%
0.37 = (CP - 145) / (256 - 145) yields CP = 186.07, rounded to 186. Summary: p resc aler = 3 , R C = 14 5d, CP = 18 6d: the se nu mbers yi eld a PW M freque nc y of
12.012 kHz, a duty cycle of 36.94 % and a resolution of 0.9 %, which is very cl ose to the init ial goal.
3/7
Loading...
+ 4 hidden pages