Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PWM disable doesn't work. Can't turn off PWM.

Hi! I am using an nRF51 DK board and the app_pwm library. I am using SDK 12.3.0.

Why doesn't

app_pwm_disable(&PWM1);

stop the PWM? I call it right after initialization:

// initialization in main.c
static int pwm_value = 50;

void pwm_complete_callback(uint32_t pwm_instance_index) {
}
APP_PWM_INSTANCE(PWM1,1); 
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH	(3822L, 25);

// in main() function:
    NRF_LOG_INFO("Initializing PWM For Buzzer...\r\n");
	
		err_code = app_pwm_init(&PWM1, &pwm1_cfg, pwm_complete_callback); 
		APP_ERROR_CHECK(err_code);
		while (app_pwm_channel_duty_set(&PWM1, 0, pwm_value) == NRF_ERROR_BUSY);
		
		// !!! Why doesn't this stop PWM?
		app_pwm_disable(&PWM1);
		
    // Start execution.
    NRF_LOG_INFO("Blinky Start!\r\n");
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
        }
    }

... as well as in a button handler, and neither stop PWM from happening.

I am using it to control a buzzer, so it doesn't matter whether it stops high or low. It just needs to stop.

None of the solutions on other threads worked, and they were all for older versions of the SDK <10.

Parents Reply Children
  • Thanks Martin for the detailed response! I still have a few questions.

    The PWM Library example and nRF52 peripheral tutorials you gave never disable the PWM instance, they just set the Duty Cycle to 0. This is the temporary solution I have been using as well. Would a PWM of 0 prevent interrupts and power consumption in the same way that disabling PWM should?

    "A quick look at your code shows that you do no enable the PWM instance after you initialize it. Which could be the reason why you cannot disable an instance that is not yet enabled."

    First, I tried enabling the PWM immediately before disabling PWM, and it still did not stop the PWM. Similarly, enabling the PWM and later disabling it on a button press also fails to stop the PWM.

    Second, doesn't your suggestion seem a little strange to you? If I never enabled PWM, then why would unwanted PWM be occurring? Why would I need to enable something in order to disable it?

Related