<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/79356/how-to-set-duty-cycle-of-two-pwm-channel-in-opposite-polarity-inside-gpio-interrupt-handler</link><description>I am quite new with nordic environment . 
 My application is to start 2 pwm instance in opposite polarity (i.e 1 high polarity and another is low polarity with same duty cycle) when got interrupt in gpio pin. 
 I am able to create the same by using PWM_Library</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 20 Sep 2021 11:56:05 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/79356/how-to-set-duty-cycle-of-two-pwm-channel-in-opposite-polarity-inside-gpio-interrupt-handler" /><item><title>RE: How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?</title><link>https://devzone.nordicsemi.com/thread/330270?ContentTypeID=1</link><pubDate>Mon, 20 Sep 2021 11:56:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b40de952-0e29-4ee1-8c2a-5f2a6e9f57a5</guid><dc:creator>RAM_MS</dc:creator><description>&lt;p&gt;I want One-pulse mode PWM like present in stm32 .&lt;/p&gt;
&lt;p&gt;50% duty cycle is fine and 1Mhz i.e 1 microsecond clock .&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?</title><link>https://devzone.nordicsemi.com/thread/330171?ContentTypeID=1</link><pubDate>Mon, 20 Sep 2021 07:38:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cbd5b8e5-caec-45ac-89a4-376f9b123776</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I thought you wanted 50% duty cycle, if you want different duty cycle then maybe you can consider using a second pwm instance for the second channel? If you start them from the CPU there will be a 16MHz period between the two, will that be a problem?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?</title><link>https://devzone.nordicsemi.com/thread/329923?ContentTypeID=1</link><pubDate>Fri, 17 Sep 2021 06:44:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dbc6300f-7398-4f0f-846a-5375f2e7fd45</guid><dc:creator>RAM_MS</dc:creator><description>&lt;p&gt;Hello&lt;/p&gt;
&lt;p&gt;Again by doing (duty | 0x8000) channel 1 output is opposite to channel 0 but with different duty cycle .&lt;/p&gt;
&lt;p&gt;That is if duty cycle of&amp;nbsp; channel 0 is 40 then duty cycle become 60 .which is not required .&lt;/p&gt;
&lt;p&gt;My question was about opposite polarity with same duty cycle.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?</title><link>https://devzone.nordicsemi.com/thread/329855?ContentTypeID=1</link><pubDate>Thu, 16 Sep 2021 13:09:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e23742fb-9ea0-4adc-8363-a06d8e7ed35d</guid><dc:creator>RAM_MS</dc:creator><description>&lt;p&gt;Hello ,Kenneth&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please guide me to solve this problem .&lt;/p&gt;
&lt;p&gt;My simple aim to control two pwm channel in opposite duty cycle from interrupt and interrupt signal is also an square wave signal with 50% duty cycle and 20ms period.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?</title><link>https://devzone.nordicsemi.com/thread/329833?ContentTypeID=1</link><pubDate>Thu, 16 Sep 2021 12:22:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e43f4c22-f00e-4b61-9dc4-a29b9607c1f7</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Please don&amp;#39;t mix direct hardware access to the PWM peripheral and at the same time also use the driver to access the PWM peripheral. Then you very likely will end up in a bad state or configuration.&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?</title><link>https://devzone.nordicsemi.com/thread/329800?ContentTypeID=1</link><pubDate>Thu, 16 Sep 2021 11:12:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b9c3b063-7958-48b6-9373-5af315854665</guid><dc:creator>RAM_MS</dc:creator><description>&lt;p&gt;Hello, Kenneth&lt;/p&gt;
&lt;p&gt;I tried using pwm hardware peripheral but It&amp;#39;s not working can You please guide me where is the fault in my code??&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define OUTPUT_PIN LED_1
#define OUTPUT_PIN2 LED_2

#define    PIN_IN    22
#define    PWM_PIN   11


unsigned int duty = 4000 ;
static volatile bool  gpio_flag;
  

static int16_t buf[] = {10000}; // Inverse polarity (bit 15), 10000us duty cycle
static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);



// Declare variables holding PWM sequence values. In this example only one channel is used 
nrf_pwm_values_individual_t seq_values[] = {0, 0, 0, 0};
nrf_pwm_sequence_t const seq =
{
    .values.p_individual = seq_values,
    .length          = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats         = 0,
    .end_delay       = 0
};

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
   gpio_flag = true;
}

static void gpio_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
    in_config.pull = NRF_GPIO_PIN_PULLDOWN;

    err_code = nrf_drv_gpiote_in_init(PIN_IN, &amp;amp;in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(PIN_IN, true);
}



// Set duty cycle between 0 and 100%
void pwm_update_duty_cycle(uint16_t duty_cycle)
{
    
   
    {
        seq_values-&amp;gt;channel_0 = duty_cycle ;
        seq_values-&amp;gt;channel_1 = duty_cycle | 0x8000;
    }
    
    
    nrf_drv_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq, 1, NRF_DRV_PWM_FLAG_LOOP);
}

static void pwm_init(void)
{
    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            OUTPUT_PIN, // channel 0
            OUTPUT_PIN2,             // channel 1
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 2
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 3
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_1MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 10000,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    // Init PWM without error handler
    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL));
    
}

static void zcd_init(void)
{

 // Configure PWM_PIN as output, and set it to 0
  NRF_GPIO-&amp;gt;DIRSET = (1 &amp;lt;&amp;lt; PWM_PIN);
  NRF_GPIO-&amp;gt;OUTCLR = (1 &amp;lt;&amp;lt; PWM_PIN);
  
  
  NRF_PWM0-&amp;gt;PRESCALER   = PWM_PRESCALER_PRESCALER_DIV_16; // 1 us
  NRF_PWM0-&amp;gt;PSEL.OUT[0] = PWM_PIN;
  NRF_PWM0-&amp;gt;MODE        = (PWM_MODE_UPDOWN_Up &amp;lt;&amp;lt; PWM_MODE_UPDOWN_Pos);
  NRF_PWM0-&amp;gt;DECODER     = (PWM_DECODER_LOAD_Individual       &amp;lt;&amp;lt; PWM_DECODER_LOAD_Pos) | 
                          (PWM_DECODER_MODE_RefreshCount &amp;lt;&amp;lt; PWM_DECODER_MODE_Pos);
  NRF_PWM0-&amp;gt;LOOP        = (PWM_LOOP_CNT_Disabled &amp;lt;&amp;lt; PWM_LOOP_CNT_Pos);
  
  NRF_PWM0-&amp;gt;COUNTERTOP = 20000; // 20ms period
  
  
  NRF_PWM0-&amp;gt;SEQ[0].CNT = ((sizeof(buf) / sizeof(uint16_t)) &amp;lt;&amp;lt; PWM_SEQ_CNT_CNT_Pos);
  NRF_PWM0-&amp;gt;SEQ[0].ENDDELAY = 0;
  NRF_PWM0-&amp;gt;SEQ[0].PTR = (uint32_t)&amp;amp;buf[0];
  NRF_PWM0-&amp;gt;SEQ[0].REFRESH = 0;
  NRF_PWM0-&amp;gt;SHORTS = 0;
  
  NRF_PWM0-&amp;gt;ENABLE = 1;
  NRF_PWM0-&amp;gt;TASKS_SEQSTART[0] = 1;
}

int main(void)
{

  // Start accurate HFCLK (XOSC)
  NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1;
  while (NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0) ;
  NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED = 0;
   
    bsp_board_init(BSP_INIT_LEDS);
    zcd_init();
    gpio_init();
    pwm_init();
    gpio_flag = false;
    

    while (1)
    {
        //if (gpio_flag == true)
        {
          gpio_flag = false;
          unsigned int val = duty;
          pwm_update_duty_cycle(val);
        }
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Mainly my zcd_init() function is not working . where I created an 50% duty cycle signal of 20 ms period and given to my gpio interrupt pin 22.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?</title><link>https://devzone.nordicsemi.com/thread/328414?ContentTypeID=1</link><pubDate>Tue, 07 Sep 2021 13:22:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:91a23abf-67ac-4fd0-b1aa-4141a24223a3</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I notice you are using app_pwm, which is not actually the PWM hardware peripheral instance, but instead&amp;nbsp;app_pwm is using several ppi, gpiote and timer to create a PWM signal:&lt;br /&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/lib_pwm.html"&gt;https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/lib_pwm.html&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You may get into trouble updating the PWM in such case since it will depend on the interrupt priority of those peripherals.&lt;/p&gt;
&lt;p&gt;I instead suggest&amp;nbsp;that you use the PWM hardware peripheral, which have no such dependencies.&lt;/p&gt;
&lt;p&gt;You can find an example here that show how you can setup the PWM peripheral:&lt;br /&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/39314/pwm-update_function/152602#152602"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/39314/pwm-update_function/152602#152602&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can find an example below where&amp;nbsp;pwm channel 1 is&amp;nbsp;inverted of&amp;nbsp;pwm channel 0:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-e7e4528908a64f45bf120ad5a23ef4b4/25464.main.c"&gt;/cfs-file/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-e7e4528908a64f45bf120ad5a23ef4b4/25464.main.c&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The above main.c is a modification of&amp;nbsp;\nRF5_SDK_17.0.2_d674dde\examples\peripheral\pwm_driver&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>