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

how to stop PWM

Hi, i am working on PWM to operate vibration motor with different frequency levels. so that i integrated PWM Library example to my ble_app_uart example. in the PWM example i initialised PWM. i just want to run this for only 1 or 2 sec after that it should stop. how to stop it. and how the PWM is generating in the for loop below. here when iam changing time_period in below code i am able to see difference in LED blinking frequency. i am unable to understand wt it is doing.

thenk you,

PWM_init {
uint32_t time_period = 200L;
 ret_code_t err_code;

    /* 2-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(time_period, ARDUINO_3_PIN, BSP_LED_3);

    /* Switch the polarity of the second channel. */
    //pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;

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

    uint32_t value;
    while(true)
    {
        for (uint8_t i = 0; i < 40; ++i)
        {
            value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);

            ready_flag = false;
            /* Set the duty cycle - keep trying until PWM is ready... */
            while (app_pwm_channel_duty_set(&PWM, 0, value) == NRF_ERROR_BUSY);

            /* ... or wait for callback. */
            while(!ready_flag);
            APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM, 1, value));
            nrf_delay_ms(50);
        }
    }
}
  • Hi,

    You can disable the PWM instance using the function app_pwm_disable(), and uninitialize the PWM instance using app_pwm_uninit(). Note that your function "PWM_init" is will be stuck in the while(true) loop, constantly changing the duty cycle.

  • i want to run the PWM for 2 sec and then it should stop. how to do this with out using while(true)

  • Then you should looking into using a app_timer. Take a look at the Application Timer Tutorial. You should stop the PWM in the app_timer timeout handler. The ble_app_uart example already includes the app-timer. Here is some code snippets on how you can implement this.

    APP_TIMER_DEF(m_pwm_timer_id);
    
    // Timeout handler for the timer
    static void timer_handler(void * p_context)
    {
        // ADD CODE TO TURN OFF PWM
    }
    
    static void create_timer()
    {   
        uint32_t err_code;
    
        // Create timers
        err_code = app_timer_create(&m_pwm_timer_id,
                                    APP_TIMER_MODE_SINGLE_SHOT,
                                    timer_handler);
        APP_ERROR_CHECK(err_code);
    }
    
    
    
    static void start_pwm_timer()
    {
        uint32_t err_code;
    
        //ADD CODE TO START THE PWM. (app_pwm_enable() + set duty cycle)
    
        // Start timer, call the timout handler afte 2 sec
        err_code = app_timer_start(m_pwm_timer_id,
                                    APP_TIMER_TICKS(2000, APP_TIMER_PRESCALER),
                                    NULL);
        APP_ERROR_CHECK(err_code);
    }
    
Related