How to use PWM Sequence for complex LED control ( n times dimming and then stop )

Hi Nordic, 

I want to implement a LED blinking like below pseudo code. 
I'd like to use PWM sequence Timer x n , GPIOTE, PPI since it doesn't consume any CPU time. 

for (10 times) {
    for (100 times) }
        LED_ON  : 0.4ms
        LED_OFF : 0.6ms
    }
   LED_OFF : 200ms
}   

Is there any example for Timer, GPIOTE, and PPI coding ?

nRF52840DK board
nRF Connect SDK 3.0.1

Regards, 
Kenny

  • You could just use a PWM peripherial for this task. Should be simple.

    If you used the RTC timer, you would achieve a lot less current consumtion in the off time - because the HF(int) oscillator can sleep. It can't when TIMER or PWM was active.

  • Hi Turbo, 


    As your suggestion, I've tried to make the LED control with PWM. But, I couldn't make it. 
    I had used PWM and sequence, but it just show about 1s ON without any blinking. 

    Can you see the code below and what I got wrong ?


    This is what I want to do

     * for ( n_blink  ) {                       // 'n_blink' for number of visual toggle
     *  for ( n_fast_blink ) {                  // 'fast' for LED bright control
     *    led on :    t_fast_on_us   us         //     t_fast_on_us + t_fast_off_us makes 't_on' since LED toggles very fast
     *    led off  :   t_fast_off_us   us
     *   }
     *    led off : t_off_us   us               // 't_off'
     * }
        led_blinkx2_conf(
            /* n_f_blinks  */ 10,           
            /* t_f_on_us   */ 100*1000,
            /* t_f_off_us  */ 100*1000, 
            /* n_blink     */ 5,            
            /* t_off_us    */ 200*1000 
        );

    Settings


    Read-back
    NRF_PWM0->PSEL.OUT[0] = 0x0000000D 13
    NRF_PWM0->PSEL.OUT[1] = 0xFFFFFFC0 -64
    NRF_PWM0->PSEL.OUT[2] = 0xFFFFFFC0 -64
    NRF_PWM0->PSEL.OUT[3] = 0xFFFFFFC0 -64
    NRF_PWM0->MODE = 0x00000000 0
    NRF_PWM0->PRESCALER = 0x00000007 7
    NRF_PWM0->DECODER = 0x00000000 0

    NRF_PWM0->COUNTERTOP = 0x000061A8 25000
    NRF_PWM0->SEQ[0].PTR = 0x2000105A 536875098
    NRF_PWM0->SEQ[0].CNT = 0x0000000A 10
    NRF_PWM0->SEQ[0].REFRESH = 0x00000002 2
    NRF_PWM0->SEQ[0].ENDDELAY = 0x000061A8 25000
    NRF_PWM0->LOOP = 0x00000004 4
    NRF_PWM0->ENABLE = 0x00000001 1
    PWM_SEQ
    PWM_SEQ [ 000 ] = 0x000030D4 12500
    PWM_SEQ [ 001 ] = 0x000030D4 12500
    PWM_SEQ [ 002 ] = 0x000030D4 12500
    PWM_SEQ [ 003 ] = 0x000030D4 12500
    PWM_SEQ [ 004 ] = 0x000030D4 12500
    PWM_SEQ [ 005 ] = 0x000030D4 12500
    PWM_SEQ [ 006 ] = 0x000030D4 12500
    PWM_SEQ [ 007 ] = 0x000030D4 12500
    PWM_SEQ [ 008 ] = 0x000030D4 12500
    PWM_SEQ [ 009 ] = 0x000030D4 12500
    PWM_SEQ [ 010 ] = 0x000030D4 12500
    PWM_SEQ [ 011 ] = 0x000030D4 12500
    PWM_SEQ [ 012 ] = 0x000030D4 12500
    PWM_SEQ [ 013 ] = 0x000030D4 12500
    PWM_SEQ [ 014 ] = 0x000030D4 12500
    PWM_SEQ [ 015 ] = 0x000030D4 12500
    PWM_SEQ [ 016 ] = 0x000030D4 12500
    PWM_SEQ [ 017 ] = 0x000030D4 12500
    PWM_SEQ [ 018 ] = 0x000030D4 12500
    PWM_SEQ [ 019 ] = 0x000030D4 12500


    What I got
    LED keeps blinking about 5Hz. 
    But it never stops. 
  • Thanks,. 

    I replaced NRFX_PWM_PIN_NOT_USED  with   NRF_PWM_PIN_NOT_CONNECTED. to fix a compile error. After that, I compiled and flashed the code onto nRF52840. The LED dimming works great, but it keeps dimming continuously without stop. 

    (void)nrfx_pwm_complex_playback(&pwm_instance, &led_on_seq, &led_off_seq, 1,
    NRFX_PWM_FLAG_LOOP);
    The fourth parameter '1' is the playback count, which should go into the LOOP register. 
    From what I understand, this should control the number of dimming cycles. So, the LED should stop after one dimming cycle. 
    And if I change the value to 3, the LED should dim three times and then stop. 

    However, no matter what number I use, the LED never stops dimming. 

    Could you please check the code and teach me how to correctly specify the number of dimming cycles?

    --> 

    After I change  NRFX_PWM_FLAG_LOOP  to  NRFX_PWM_FLAG_STOP, LED dimming stops. 
    I'll study the code and check the register settings in detail. 

    I'll open new ticket when I have another question. 
    Please close this ticket. 

    Note. 
    The title of this ticket does not match with the contents. So, I changed it. 


    Best Regards, 

Related