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

Control two pwms in sequence

Hi,

Keywords: nrf52832, pwm_driver, two pwms, 6 channels, pwm in sequence

I have four LEDs: three mono white LEDs, one RGB LEDs(three color leds) that produce white color, so occupy six GPIOs.

I am using 2 pwms to control 6 channels for these three mono LEDs and three color LEDs.

I want the four LEDs burning from bottom to top one by one, which means RGB LEDs light up for 1s and then the rest of three mono LEDs controlled by another pwm light up one by one.

I am using while(wait) for RGB to finish its animation and then playback the second pwm otherwise they will play the animation at the same time, but I dont think it is a wise way to do so since it will block the system. Since they are 6 channels, I cannot just use one pwm and nrf_drv_pwm_complex_playback.

//Play RGB white fade in and out for 1s
(void)nrf_drv_pwm_simple_playback(&m_pwm0, &seq0, 1, NRF_DRV_PWM_FLAG_STOP);

//Wait for RGB animation finishing
while(!nrfx_pwm_is_stopped(&m_pwm0));

//Play mono white fade in and out for 1s one by one
(void)nrf_drv_pwm_simple_playback(&m_pwm1, &seq1, 1, NRF_DRV_PWM_FLAG_STOP);

//Wait for mono white animation finishing    
while(!nrfx_pwm_is_stopped(&m_pwm1));

Any idea?

Thanks

/CJ

Parents
  • Hello,

    You have probably considered this, but it is not possible to change out the color LED with a normal LED, so that you can use only one channel for that one as well? In that case, you would only need 4 channels -> 1 PWM instance. Or alternatively, use one pin to power all 3 colors of the color LED. But I assume you want to be able to control the colors as well, so therefore you need 6 channels:

    I agree that it is not efficient to use thw while (!nrfx_pwm_is_stopped());

    Before the call to nrf_drv_pwm_simple_playback(), you probably have a call looking something like this:

    nrf_drv_pwm_init(&m_pwm0, &config0, my_callback_handler);

    (my_callback_handler probably has a different name)

    But this event handler is an event handler that will be called when the sequence is finished. Look at how this is used in the SDK\examples\peripheral\pwm_driver example, in the demo1() function.

    Now, if you use NRF_DRV_PWM_FLAG_STOP in nrf_drv_pwm_simple_playback, you will get an event in the event handler. NB: You will get more, so you need to check the event type:

    static void my_callback_handler(nrf_drv_pwm_evt_type_t event_type)
    {
        if (event_type == NRFX_PWM_EVT_STOPPED)
        {
            NRF_LOG_INFO("PWM sequence done");
            start_next_sequence(); //custom function starting the next pwm sequence.
        }
    }

    Try this, and let me know if it doesn't work.

    Best regards,

    Edvin

Reply
  • Hello,

    You have probably considered this, but it is not possible to change out the color LED with a normal LED, so that you can use only one channel for that one as well? In that case, you would only need 4 channels -> 1 PWM instance. Or alternatively, use one pin to power all 3 colors of the color LED. But I assume you want to be able to control the colors as well, so therefore you need 6 channels:

    I agree that it is not efficient to use thw while (!nrfx_pwm_is_stopped());

    Before the call to nrf_drv_pwm_simple_playback(), you probably have a call looking something like this:

    nrf_drv_pwm_init(&m_pwm0, &config0, my_callback_handler);

    (my_callback_handler probably has a different name)

    But this event handler is an event handler that will be called when the sequence is finished. Look at how this is used in the SDK\examples\peripheral\pwm_driver example, in the demo1() function.

    Now, if you use NRF_DRV_PWM_FLAG_STOP in nrf_drv_pwm_simple_playback, you will get an event in the event handler. NB: You will get more, so you need to check the event type:

    static void my_callback_handler(nrf_drv_pwm_evt_type_t event_type)
    {
        if (event_type == NRFX_PWM_EVT_STOPPED)
        {
            NRF_LOG_INFO("PWM sequence done");
            start_next_sequence(); //custom function starting the next pwm sequence.
        }
    }

    Try this, and let me know if it doesn't work.

    Best regards,

    Edvin

Children
No Data
Related