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

Correct way to change frequency with APP_PWM.

I'm trying to create a siren tone using the pwm library, that ramps up and down between 2kHz and 3kHz.

Initially I started with the example provided in SDK v15.3.0
nRF5_SDK\examples\peripheral\pwm_library
using this with the nrf52840 DK.

Example was working with 2kHz  @ 30% duty.

I attempted to modify the example to allow for frequency shifts, but the output waveform is not as expected.

Here is the example output  as expected with fixed frequency:

This is the attempted frequency shift with edges in the spaces that I cannot make sense of at the moment:

Ignoring the dodgy uS increment Slight smile
Here is the code snippet of the attempt.

APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.

static volatile bool ready_flag;            // A flag indicating PWM status.

void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    ready_flag = true;
    app_pwm_disable(&PWM1);
    app_pwm_uninit(&PWM1);
}

static const uint32_t base_frequency = 500; //in us or 2kHz
int main(void)
{
    ret_code_t err_code;

    /* 2-channel PWM, 200Hz, output on DK LED pins. */
    //static app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(500, 30);

    static app_pwm_config_t pwm1_cfg = {
        .pins            = {30, APP_PWM_NOPIN},
        .pin_polarity    = {APP_PWM_POLARITY_ACTIVE_HIGH, APP_PWM_POLARITY_ACTIVE_LOW},
        .num_of_channels = 1,
        .period_us       = base_frequency
    };

    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);
    app_pwm_channel_duty_set(&PWM1, 0, 30);

    while (true)
    {
        ready_flag = false;

        /* ... or wait for callback. */
        while (!ready_flag);
        pwm1_cfg.period_us += 10;        
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
        app_pwm_channel_duty_set(&PWM1, 0, 30);

    }
}


/** @} */

I'm not entirely sure what is going wrong here, and I'm not sure what the intended method for updating frequency with the APP_PWM library.

Secondly, what does the PWM state change busy and ready mean? Does ready occur on full period, or duty set?

Ideally, I would like to get the timer->PPI->GPIOTE method working as one of targets does not have PWM peripherals.

I'm hoping I'm missing something very simple. Thanks in advance.

Parents Reply Children
No Data
Related