This makes things clear though it forces the user to read the spec of the MCU anyway to define the correct period.
For instance for PIC32 it makes following equations:
Code: Select all
#define PWM_FREQ_HZ 14000ULL
#define PB_FREQ_HZ MIPS_CPU_FREQ
// PWM Frequency = 1/[({PWM Period} + 1) * Tpb * Tp]
// Tp := timer prescaler
// Tpb := 1/Fpb
// Fpb := peripheral bus clock frequency
// PWM Period = [Fpb/({PWM Frequency} * Tp)] - 1
// PWM resolution = log2(PWM Period)
// ---
// max(map(lambda Tp: (log2((PB_FREQ_HZ/(PWM_FREQ_HZ*Tp))-1), Tp), (1, 2, 4, 8, 16, 32, 64, 256))) -> (12.48, 1)
// to achieve max resolution for given PWM frequency 1:1 timer prescaler fits best