Difference bewteen top value and duty cycle in PWM Driver

Hi,

I'm trying to use the pwm driver to turn off or on a dc motor (which the speed will variate via the pwm signal).
I need to achieve a frequency between 20kHz and 100kHz and to variate the dutycyle.

I do understand that the frequency is my main frequency divided by the top value, so for example if I put 500kHz with a top value of 6, I'll have a frequency of 83,33kHz. (is it correct ?)

But I don't understand how the duty cycle is working. I saw that there is a comparaison logic behind it, but I didn't understand how it works...

I saw an example that they made, which the dutycyle is updated like this : 

static uint16_t const m_top  = 6;
static nrf_pwm_values_individual_t  seq_values;
static nrf_pwm_sequence_t const seq =
{
    .values.p_individual = &seq_values,
    .length              = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats             = 0,
    .end_delay           = 0
};
    
void update_pwm(uint16_t duty_forward, uint16_t duty_reverse)
{
    
    seq_values.channel_0 = m_top-duty_forward;
    seq_values.channel_1 = m_top-duty_reverse;

    nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 1,  NRF_DRV_PWM_FLAG_LOOP);
}

Is it correct to make the top value minus the duty cycle (which duty_forward is a uint16_t, so his value is from 0 to 65535) to get the correct comparaison ?

Thanks for your answers,

Chris

Related