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

How to control two channes PWM independently?

Maybe I should post my all the code here.

I want to generate two kinds of PWM1 and PWM2,used by the code as follows: static volatile bool ready_flag;

APP_PWM_INSTANCE(&PWM1,1); APP_PWM_INSTANCE(&PWM2,2);

void pwm1_ready_callback(void) { ready_flag = true;//This code may be unuseful,but it comes from the Nordic Demo. }; void pwm2_ready_callback(void) { ready_flag = true;//This code may be unuseful,but it comes from the Nordic Demo. };

One of them is initialized by the code as follows:

void pwm_ble_led(void) { static ret_code_t error_code; app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1000000,LED_0); pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_LOW; error_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm1_ready_callback); APP_CHECK_ERROR(error_code); }

The another as the same,the code as follows:

void pwm_power_ble(void) { static ret_code_t error_code; app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1000000,LED_1); pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH; error_code = app_pwm_init(&PWM2,&pwm1_cfg,pwm2_ready_callback); APP_CHECK_ERROR(error_code); }

The PWM closed code as follows:

void pwm_ble_close(void) {
uint32_t err_code; app_pwm_disable(&PWM1); nrf_drv_gpiote_out_task_disable(LED_0); err_code = app_pwm_uninit(&PWM1); APP_ERROR_CHECK(err_code); }

void pwm_power_close(void) { uint32_t err_code; app_pwm_disable(&PWM2); nrf_drv_gpiote_out_task_disable(LED_1); err_code = app_pwm_uninit(&PWM2); APP_ERROR_CHECK(err_code); }

The PWM start working code as follows:

void pwm_ble_led_start(void) { pwm_ble_close();//this code is used for guaranting that the PWM is closed,otherwise the system would reset. pwm_ble_led(); app_pwm_enable(&PWM1); nrf_drv_gpiote_out_task_enable(LED_0); app_pwm_channel_duty_set(&PWM1,0,50); }

void pwm_power_led_start(void) { pwm_power_close(); pwm_power_led(); app_pwm_enable(&PWM2); nrf_drv_gpiote_out_task_enable(LED_1); app_pwm_channel_duty_set(&PWM2,0,50); }

The question is how to control the two PWM independently?I have tried the function of app_pwm_disable() and app_pwm_uninit(),but it does not work result of influencing the another PWM.

Parents
  • I might find a solution. If you want to use two PWM and they are not same frequency(I had problem on this situation), simply make two instances and initialize them by using two timers.

    But if you don't use APP_PWM_INSTANCE macro, make sure that they don't share same app_pwm_cb_t instance. I didn't use the macro and lazily shared the same app_pwm_cb_t instance by two app_pwm_t instances and that was causing strange behavior.

    And if you want to use two PWM with same frequency, use APP_PWM_DEFAULT_CONFIG_2CH as Ole mentioned and only use app_pwm_disable if you want to disable both channels. If one of the channel is still active, setting duty 0 for unused channel might be enough by using app_pwm_channel_duty_set function.

Reply
  • I might find a solution. If you want to use two PWM and they are not same frequency(I had problem on this situation), simply make two instances and initialize them by using two timers.

    But if you don't use APP_PWM_INSTANCE macro, make sure that they don't share same app_pwm_cb_t instance. I didn't use the macro and lazily shared the same app_pwm_cb_t instance by two app_pwm_t instances and that was causing strange behavior.

    And if you want to use two PWM with same frequency, use APP_PWM_DEFAULT_CONFIG_2CH as Ole mentioned and only use app_pwm_disable if you want to disable both channels. If one of the channel is still active, setting duty 0 for unused channel might be enough by using app_pwm_channel_duty_set function.

Children
Related