**DIRECT PROGRAMMING SAMD21 REGISTERS**

FOR

OUTPUT PWM FREQUENCY / RESOLUTION OF ARDUINO ZERO

FOR

OUTPUT PWM FREQUENCY / RESOLUTION OF ARDUINO ZERO

4. SYSTEM MATHEMATICS & THE PPWM TOOL

The Excel file PPWM TOOL allow easy and quick interactive selection of the system operating parameters based on PWM requirements needed. Math. expressions are as follows.

Resolution (Bits) = R

Resolution (Ratio) = PER = 2^R – 1

**Example 1**: R = 10, V = 2^10 – 1 = 1024 – 1 = 1023

Clock Frequency GCLK

Clock Divisor GENDIV, D

Prescaler Factor CTRLA, N

Timer frequency, Ft = GCLK/(D*N)

**Example 2**: Clock = 48 Mhz = 48.000.000 Hz; D = 3 ; N = 16;

Timer frequency Ft = 48.000.000/(3*16) = 1.000.000 Hz = 1 MHz.

PWM frequency, Fpwm = Ft/(2*PER)

**Example 3**: Fpwm = 1.000.000/(2*1023) = 488.76 Hz.

Fractional resolution (Bits) = R = (LOG10 (PER + 1) / LOG10 (2)

**Example 4**: PER = 1728;

R = (LOG (1728 + 1) / 0,301030 = 3,237795/0,301030 = 10, 756 = 10 Bits

The PPWM tool allows interactive selection and calculation of parameters to achieve the required result. The top section uses the Resolution as the Main Variable, while the bottom section uses the Frequency as the Main Variable. Data must be written in the DATA column only and results are given in the CALC column.

**Example 5**

We need to generate PWM with exact 12 Bit resolution (main requirement) and PWM must have a frequency in the order of low audio, below 500 Hz,

**because it must drive low frequency Optocouplers**. In the top section of the PPWM Tool we enter in the DATA COLUMN:

Resolution: 12Bit Tool calculates required PER = 4095

GENDIV D : 1 and

Prescaler N : 8 and

Clock Frequency: 48.000.000 Hz

Tool calculates: Timer frequency Ft: 6.000.000Hz (6 MHz) and

PWM frequency Fpwm: 732,60 Hz.

This value of Fpwm is too high, so we change N from 8 to 16 and obtain Fpwm: 366,30 which is in the ballpark.

In general the RESOLUTION section is used when we can compromise on PWM frequency, but we must have a non-fractional resolution value, because the PWM must be converted into a precision D.C. voltage by lowpass filtering.

When we do not have the above requirement, but we need a specific PWM frequency, then the same mathematical expressions are used in the PWM Frequency (lower) section of the PPWM Tool. Of course even in this case we shall manipulate parameters in order to maximise resolution as much as possible.

**EXAMPLE 6**

We need to have a PWM output of exactly 17 KHz. We enter in the DATA column:

PWM frequency: 17 KHz = 17.000 Hz

GENDIV, D: 1

Prescaler N: 4 with this data we end up with a PER = 347, a fractional resolution of 8.47 and a rounded resolution of 8 Bit.

To maximize the resolution we have to further reduce prescaling, so we enter N = 1. Now PER climbs to 1411,fractional resolution to 10,46 and rounded resolution to 10.

**EXAMPLE 7**

After testing, we realize that frequency must be changed to precisely 17.307 HZ. Entering this frequency into the PPWM Tool, the new PER value is: 1387 and the effect on resolution is negligible.