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.

Table 2-11. Timer Configuration
Timer SettingValue
STARTRising ERS Edge
RESETAt Start + PR Match
STOPAt 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
When the periodic input rises (changes from 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.
Figure 2-15. PWM Generator

The period of the PWM is dependent on the period of the ERS input signal:

PWMPeriod=ERSInputPeriod

When OPOL = 0 (left-aligned PWM), the PR value determines the ON time. Hence, pulse width of the signal is:

PulseWidth=(PRvalue+1)×TUCLKperiod

When OPOL = 1 (right-aligned PWM), the PR value determines the OFF time. Hence, pulse width of the signal is:

PulseWidth=PWMPeriod((PRvalue+1)×TUCLKperiod)

The duty cycle ratio is a function of the pulse width and period of the PWM signal:

DutyCycleRatio=PulseWidthPWMPeriod

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.

PWMResolution=log2(PWMPeriodUTMRRolloverPeriod×2UTMRSize)bits

Important: The UTMR rollover period must be greater than or equal to the ERS input period for proper PWM operation.

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.

PWMPeriod=ERSInputPeriod=1/(1kHz)=1ms

PulseWidth= Duty Cycle Ratio×PWM Period =30%×1ms=300μs

PRvalue=PulseWidthTUCLKPeriod1=300μs1/16MHz1=300μs62.5ns1=4799

PWMResolution=log2(1ms4.096ms×216)=13bits