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

PWM frequency accuracy

hello, I want to generate a square wave; examples of your company use PWM official offer;Square wave frequency lower accuracy, configuration 10KHz frequency, there will be error ± 16Hz.How do I need to configure the frequency accuracy can reach ± 1Hz? Here is the code:

void demo1(void)
{
    printf("Demo 1\r\n");

    /*
     * This demo plays back a sequence with different values for individual
     * channels (LED 1 - LED 4). Only four values are used (one per channel).
     * Every time the values are loaded into the compare registers, they are
     * updated in the provided event handler. The values are updated in such
     * a way that increase and decrease of the light intensity can be observed
     * continuously on succeeding channels (one second per channel).
     */

    uint32_t err_code;
    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // channel 0
            BSP_LED_1 | NRF_DRV_PWM_PIN_NOT_USED, // channel 1
            BSP_LED_3 | NRF_DRV_PWM_PIN_NOT_USED, // channel 2
            BSP_LED_2 | NRF_DRV_PWM_PIN_NOT_USED  // channel 3
        },
        .irq_priority = _PRIO_APP_HIGH,
        .base_clock   = PWM_PRESCALER_PRESCALER_DIV_1,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 100,
        .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);

    m_demo1_seq_values.channel_0 = 33;
    m_demo1_seq_values.channel_1 = 40;
    m_demo1_seq_values.channel_2 = 50;
    m_demo1_seq_values.channel_3 = 60;
    m_demo1_phase = 0;

    nrf_drv_pwm_simple_playback(&m_pwm0, &m_demo1_seq, 1,
        NRF_DRV_PWM_FLAG_LOOP);
}
Related