2.3.5 Pulse-Width Modulation (PWM) Generator
The timer can be configured to generate a PWM signal when combined with a periodic ERS input signal. The timer rollover period must be greater than or equal to the ERS input period for correct operation. The PWM period is the period of the ERS input signal. The PWM duty cycle is determined by the PR value in the TUxyPR period register. The TUxyPR period register is also double-buffered to avoid glitches when changing the duty cycle. Use OM = Level mode to generate a PWM output. The OPOL bit controls whether the PWM signal is left-aligned or right-aligned.
The timer configuration settings are shown in Table 2-11.
Timer Setting | Value |
---|---|
START | Rising ERS Edge |
RESET | At Start + PR Match |
STOP | At PR Match |
CSYNC (Clock Sync) | Sync |
ERS (Ext. Reset Source) | Periodic input of desired PWM frequency |
OM (Output Mode) | Level mode |
OPOL (Output Polarity) |
Low when Idle (for left-aligned PWM) High when Idle (for right-aligned PWM) |
PR (Period Register) | Calculated as per desired duty cycle and OPOL setting |
0
to 1
),
the timer is reset and starts counting. This process asserts the level output. The timer
stops counting and resets at PR match, which deactivates the level output. This process
repeats when the next rising input edge is detected. The level output generated is a PWM
waveform. When OPOL = 0
(low when idle), the waveform is left-aligned
PWM while, when OPOL = 1
(high when idle), the waveform is
right-aligned PWM. A left-aligned PWM is shown in Figure 2-15
below.The period of the PWM is dependent on the period of the ERS input signal:
When OPOL = 0
(left-aligned PWM), the PR value determines the ON time.
Hence, pulse width of the signal is:
When OPOL = 1
(right-aligned PWM), the PR value determines the OFF time.
Hence, pulse width of the signal is:
The duty cycle ratio is a function of the pulse width and period of the PWM signal:
The resolution of the PWM is a function of the PWM period, UTMR rollover period, and UTMR size. The maximum PWM resolution is the size of the UTMR when the UTMR rollover period matches the PWM period.
As an example, the following calculations show how to determine PR value and PWM resolution to generate a left-aligned 1 kHz PWM signal with 30% duty cycle. For this calculation, it is assumed that the UTMR clock, TUCLK = FOSC = 16 MHz and UTMR size = 16 bits.