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

How to start pwm sequence after particular time delay of gpio event?

I am working on an application where I need to start pwm sequence after particular delay (accurate delay of multiple of 100us).

I am quite new to nordic environment.

Here is my code . Can please guide me what is wrong here??

#define    Period    20000 //20ms

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);

APP_PWM_INSTANCE(PWM1,1); 

static int gpio_flag =0;

void pwm_update(void)
{
      uint32_t value;
      value = duty_1;
      ready_flag = false;
      /* Set the duty cycle - keep trying until PWM is ready... */
      
      

      while ((app_pwm_channel_duty_set(&PWM1, 0, value))&(app_pwm_channel_duty_set(&PWM1, 1, value)) == NRF_ERROR_BUSY);
 
      /* ... or wait for callback. */
      while (!ready_flag);
      APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
      APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 0, value));

}


void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
   gpio_flag =1;
   nrf_drv_timer_enable(&TIMER_LED);

   
}

void timer0_handler(nrf_timer_event_t event_type, void* p_context)
{
  switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
             timer_flg =1;
             nrf_drv_timer_clear(&TIMER_LED);
             gpio_flag=0;
             pwm_update();
            break;

        default:
            //Do nothing.
            break;
    }
}


void timer_init(void)
{
    uint32_t time_us = 500; 
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer0_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_LED, time_us);
    nrf_drv_timer_compare(
         &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, true);

    
}

Parents
  • Hello,

    I am quite new to nordic environment.

    Welcome! Please do not hesitate to ask if you should have any questions.

    I am working on an application where I need to start pwm sequence after particular delay (accurate delay of multiple of 100us).

    Could you show me the entirety of your main.c code?
    Right now I do not see where you setup your gpio for triggering the enabling of the timer, so I am not sure that the timer is ever started, for instance.

    Could you also elaborate on what behavior you are seeing, and how this differs from what you would expect? Does the device reset for example, or is it just that 'nothing' is happening, for example?

    Best regards,
    Karl

  • Hello Karl,

    Right now I do not see where you setup your gpio for triggering the enabling of the timer, so I am not sure that the timer is ever started, for instance.

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
       gpio_flag =1;
       nrf_drv_timer_enable(&TIMER_LED);
    
       
    }

    Could you also elaborate on what behavior you are seeing, and how this differs from what you would expect? Does the device reset for example, or is it just that 'nothing' is happening, for example?

    I am not getting the delay I am expecting.

    I think I have done some wrong configuration in linking to timer from gpio interrupt routine. 

    Best regards,
    Ram



  • Hello Karl,

                   Waiting for your valuable inputs.

     

    Thanks & regards,
    Ram

  • Hello Karl,

                   Waiting for your response.

     

    Thanks & regards,
    Ram

  • Hello again, Ram

    Thank you for your continued patience with this. I am sorry that it has taken so long to get back to you on this.

    RAM_MS said:
     Okay .As I am quite new to Nordic environment ,so take multiple Nordic sdk example  into account to create my application.

    Welcome!
    Absolutely, merging different examples could absolutely be a challenge if ones not already familiar with the SDK and how it works.

    RAM_MS said:
    You can also suggest me ,how to implement this as complete scenario is known to you( I am expecting ).

    Yes, sure, I'll take a look at this today. I'll update you by the end of day.

    Best regards,
    Karl

  • Hello Karl,

    Yes, sure, I'll take a look at this today. I'll update you by the end of day

    Sure, I am eagerly waiting for your response.

     

    Thanks & regards,
    Ram

  • Hello again, Ram

    Attached you'll find an example that updates the PWM duty cycle using a button to start a timer that calls the pwm_update function when it expires. The delay is configurable and defined at the top of main.c
    To test the example just flash it to a nRF52 DK and press button 1 on the DK -  after the configurable delay has passed it will call the (currently empty) pwm_update function.

    I did not know exactly what you would like to update the PWM to, or what configuration you need to PWM to have wrt. frequency and polarity, so I used the default pwm configuration from the pwm_library example, and left the pwm_update function empty.
    You could also reconfigure the input to be a non-button pin, if that's what you are going to use in your application.

    Take a look and let me know if this was what you were looking for, or if you should have any questions! :) 

    Best regards,
    Karl

Reply
  • Hello again, Ram

    Attached you'll find an example that updates the PWM duty cycle using a button to start a timer that calls the pwm_update function when it expires. The delay is configurable and defined at the top of main.c
    To test the example just flash it to a nRF52 DK and press button 1 on the DK -  after the configurable delay has passed it will call the (currently empty) pwm_update function.

    I did not know exactly what you would like to update the PWM to, or what configuration you need to PWM to have wrt. frequency and polarity, so I used the default pwm configuration from the pwm_library example, and left the pwm_update function empty.
    You could also reconfigure the input to be a non-button pin, if that's what you are going to use in your application.

    Take a look and let me know if this was what you were looking for, or if you should have any questions! :) 

    Best regards,
    Karl

Children
  • Strange, it seems something went wrong with the previous upload.
    I try again here:
    5852.pwm_button_timer_demo.zip

    I also forgot to mention: extract the .zip to your SDK17.0.2/examples/peripheral path.

  • Hello Karl,

    ake a look and let me know if this was what you were looking for, or if you should have any questions!

    I definitely tested this today and update you the status .

    Advance thank you for valuable time and inputs. 

    Thanks & regards,
    Ram

  • Great, I look forward to hearing if this was what you were looking for.

    RAM_MS said:
    Advance thank you for valuable time and inputs. 

    It is no problem at all, Ram - I am happy to help!

    Best regards,
    Karl

  • Hello Karl,

                   Thank you for your valuable inputs .

                   I tried with your code and timer is triggering but instead of 1ms delay its coming increments                         automatically.

                   For your better understand I am attaching both project .zip and my configuration aim in .png file

                  Also I am adding video of my outputs.

    pwm_library_timer_delay.zip

    The yellow signal is the interrupt signal and the blue signal is my pwm signal .

    Thanks & regards,
    Ram

  • Hello again, Ram

    RAM_MS said:
    Thank you for your valuable inputs .

    No problem at all, I am happy to help you.

    RAM_MS said:
    I tried with your code and timer is triggering but instead of 1ms delay its coming increments                         automatically.

    I am not sure that I understand what you mean by this - are you not seeing the button trigger a configurable timer, which calls pwm_update on expiration?
    Keep in mind that the pwm_update function is not populated in my example, so you will have to have it do something worthwhile in the pwm_update example before the example outputs anything useful when the delay expires.

    RAM_MS said:

     For your better understand I am attaching both project .zip and my configuration aim in .png file

                  Also I am adding video of my outputs.

    Oh, I see - I was under the impression that you wanted to update a PWM's duty cycle after a given delay. I was not aware that you wanted to output the same waveform out that you have received in, with an added delay.

    The simplest implementation of this would perhaps be to just have the GPIOTE IN event trigger a timer with the provided delay, that in turn sets the output pin high or low?
    You could use the PPI peripheral to have the GPIOTE in event start the TIMER, and the TIMER CC event toggle the output GPIO directly, so that none of it requires CPU intervention.

    Best regards,
    Karl

Related