This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I want to change the frequency in PWM_driver.

Hello

I only used PWM_library, but this is my first time using PWM_driver.
Simply controlling the duty cycle of multiple channels has been successful, but I am not sure about frequency control yet.

I found out that the Prescaler value can be changed through 'base_clock' in the code below. But I don't know where to modify the period.

static void pwm_init(void)
{
    uint32_t err_code;
    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            test_pwm,   // channel 0
            NRF_DRV_PWM_PIN_NOT_USED, // channel 1
            NRF_DRV_PWM_PIN_NOT_USED,  // channel 2
            NRF_DRV_PWM_PIN_NOT_USED  // channel 3
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_4MHz, //Prescaler change
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = m_top,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };

    err_code = nrf_drv_pwm_init(&m_pwm0, &config0, NULL);
    APP_ERROR_CHECK(err_code);
    // m_used |= USED_PWM(0);

        // This array cannot be allocated on stack (hence "static") and it must
    // be in RAM (hence no "const", though its content is not changed).
}

May I know about this?

Thank you.

  • Hello,

    I found out that the Prescaler value can be changed through 'base_clock' in the code below. But I don't know where to modify the period.
    May I know about this?

    The period of the PWM waveform is given by the base_clock and COUNTERTOP value combined.
    The prescaler(base_clock) decides the frequency of each tick, and the COUNTERTOP decides how many such ticks each period will contain. As a simple example, say that you would like the period of the PWM to be 1 MHz. Then you could use 16 MHz base_clock, and 1/16 prescaler. Alternatively, when using the driver, you can just change the NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY default value to 1 MHz, and configure the PWM driver with the default value.

    An exempt from the PWM peripheral documentation reads:

    The timer top value is controlled by the COUNTERTOP register. This register value in conjunction with the selected PRESCALER of the PWM_CLK will result in a given PWM period.

    Please do not hesitate to ask if any part of my answer is unclear, or if you have any other issues or questions! 

    Best regards,
    Karl

Related