<?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>Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/69825/inverting-pwm-in-nrf52840-pinn</link><description>Hello, 
 
 I want to output in one pin 400kHz and inverse of 400kHz in the other pin. I have somehow managed to output the the 400kHz but haven&amp;#39;t managed to output its inverse. 
 
 Here is my code: 
 
 
 
 I was looking into the previous answers but I</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 29 Dec 2020 08:55:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/69825/inverting-pwm-in-nrf52840-pinn" /><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286796?ContentTypeID=1</link><pubDate>Tue, 29 Dec 2020 08:55:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9aa49c14-9495-480e-bddb-c2d107c699c1</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;The decoder load mode decides how a sequence is read from ram and spread to the compare register, so it is not related to duty cycle. The duty cycle is decided by compare value in sequence and top value.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You want to run 400KHZ 80% duty PWM on Pin26, and reverse output on Pin27, one solution is:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;NRF_PWM_CLK_4MHz //4M/10=400KHZ
sequence_values[]={2, 2|0x8000,0,10}; // (10-2)/10=80%
NRF_PWM_LOAD_WAVE_FORM//(See nrf52840 ps section 6.17.1)
NRF_PWM_MODE_UP //(See nrf52840 ps section 6.17.1)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here are the codes with the above settings:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nrf_drv_pwm.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_drv_clock.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot; 
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#define  led_external  26 
#define  led_reversed  27

static nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0);



static nrf_pwm_values_wave_form_t sequence_values[]={2, 2|0x8000,0,10};	


uint16_t value = 0;


static void pwm_waveform_init(void)
{
    nrfx_pwm_config_t const config0 = 
    {
      .output_pins = 
      {
          led_external,
          led_reversed,
          NRFX_PWM_PIN_NOT_USED,   
          NRFX_PWM_PIN_NOT_USED 
      },
      .irq_priority = APP_IRQ_PRIORITY_LOWEST, 
      .base_clock   = NRF_PWM_CLK_4MHz, 
      .count_mode   = NRF_PWM_MODE_UP,  
      .load_mode    = NRF_PWM_LOAD_WAVE_FORM, 
      .step_mode    = NRF_PWM_STEP_AUTO

    };
    APP_ERROR_CHECK(nrfx_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL));
}
static void pwm_play(void)
{
  nrf_pwm_sequence_t const seq0 = 
  {
    .values.p_wave_form  = sequence_values, 
    .length           = NRF_PWM_VALUES_LENGTH(sequence_values), 
    .repeats          = 1, 
    .end_delay        = 0 
  
  };
  (void)nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq0, 1, NRFX_PWM_FLAG_LOOP);

  }



static void log_init(void)
{
  ret_code_t err_code = NRF_LOG_INIT(NULL);
  APP_ERROR_CHECK(err_code);

  NRF_LOG_DEFAULT_BACKENDS_INIT();

  }





int main(void)
{
log_init();
bsp_board_init(BSP_INIT_LEDS);
pwm_waveform_init();
pwm_play();
NRF_LOG_INFO(&amp;quot;PWM application started!!&amp;quot;);
   
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286779?ContentTypeID=1</link><pubDate>Tue, 29 Dec 2020 01:35:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aedfabbd-02dd-4fff-bd38-e4d85e0d1669</guid><dc:creator>refer_pin</dc:creator><description>[quote userid="93921" url="~/f/nordic-q-a/69825/inverting-pwm-in-nrf52840-pinn/286681#286681"]50% with&amp;nbsp;&lt;em&gt;GROUPED&lt;/em&gt;&lt;span&gt;&amp;nbsp;mode&lt;/span&gt;[/quote]
&lt;p&gt;If I needed 80% duty cycle, is that still possible in the program? or do I have to change something like the GROUPED mode into another example Individual?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286681?ContentTypeID=1</link><pubDate>Mon, 28 Dec 2020 10:48:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c87b892c-4894-41ef-8056-030d20c08746</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I made one example that shows how to get 50% with&amp;nbsp;&lt;em&gt;GROUPED&lt;/em&gt;&lt;span&gt;&amp;nbsp;mode. Suggest you refer to &lt;a href="https://infocenter.nordicsemi.com/pdf/nRF52840_PS_v1.1.pdf"&gt;nrf52840 ps&lt;/a&gt;&amp;nbsp;section PWM and nRF52&amp;nbsp;nRF5 SDK example&amp;nbsp;pwm_driver to learn how to use PWM peripheral on nRF52 devices.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nrf_drv_pwm.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_drv_clock.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot; 
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;


#define m_top     20 
#define m_step    1 

#define  led_external  26 
#define  led_reversed  27|NRFX_PWM_PIN_INVERTED

static nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0);


uint16_t step = m_top / m_step;

static nrf_pwm_values_grouped_t sequence_values[]=    {
        { 0x8000, 0 },
        { 0, 0x8000 }
    };	


uint16_t value = 0;


static void pwm_common_init(void)
{
    nrfx_pwm_config_t const config0 = 
    {
      .output_pins = 
      {
          led_external,
          NRFX_PWM_PIN_NOT_USED, 
          led_reversed| NRFX_PWM_PIN_INVERTED,
          NRFX_PWM_PIN_NOT_USED 
      },
      .irq_priority = APP_IRQ_PRIORITY_LOWEST, 
      .base_clock   = NRF_PWM_CLK_16MHz, 
      .count_mode   = NRF_PWM_MODE_UP, 
      .top_value    = m_top, 
      .load_mode    = NRF_PWM_LOAD_GROUPED, 
      .step_mode    = NRF_PWM_STEP_AUTO

    };
    APP_ERROR_CHECK(nrfx_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL));
}
static void pwm_play(void)
{
  nrf_pwm_sequence_t const seq0 = 
  {
    .values.p_grouped  = sequence_values, 
    .length           = NRF_PWM_VALUES_LENGTH(sequence_values), 
    .repeats          = 0, 
    .end_delay        = 0 
  
  };
  (void)nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq0, 1, NRFX_PWM_FLAG_LOOP);

  }



static void log_init(void)
{
  ret_code_t err_code = NRF_LOG_INIT(NULL);
  APP_ERROR_CHECK(err_code);

  NRF_LOG_DEFAULT_BACKENDS_INIT();

  }





int main(void)
{
log_init();
bsp_board_init(BSP_INIT_LEDS);
pwm_common_init();
pwm_play();

  NRF_LOG_INFO(&amp;quot;PWM application started!!&amp;quot;);
   
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286623?ContentTypeID=1</link><pubDate>Mon, 28 Dec 2020 03:31:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:931729c6-9360-401b-95f5-62e043db0ce3</guid><dc:creator>refer_pin</dc:creator><description>&lt;p&gt;Thank you so far it&amp;#39;s working..a bit better, I can see the inverted PWM. Is there a possible way to get constant 50% duty cycle? The program is somehow making the pwm &amp;quot;glitchy&amp;quot;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286618?ContentTypeID=1</link><pubDate>Sun, 27 Dec 2020 23:36:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5671aa48-8db6-4a0f-85ab-44740685ca3e</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Try this, not tested:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;With &amp;quot;step&amp;quot; as the number of discrete PWM changes, Change
static nrf_pwm_values_common_t sequence_values[m_step * 2]; 	
to
static nrf_pwm_values_common_t sequence_values[step * 2]; 	

static void pwm_common_init(void)
{
Change
    for(int i = 0; i&amp;lt;m_step; i++)
to
    for(int i = 0; i&amp;lt;step*2; i+=2)
    {
Change
      value += step; 
      sequence_values[i] = value;
      sequence_values[i+1] = value;
to
      value += m_step; 
      sequence_values[i]   = value;
      sequence_values[i+1] = value | 0x8000; 
    }

    nrfx_pwm_config_t const config0 = 
    {
      .output_pins = 
      {
Change
          led_external,
          led_reversed| NRFX_PWM_PIN_INVERTED,
          NRFX_PWM_PIN_NOT_USED, 
          NRFX_PWM_PIN_NOT_USED 
To
          led_external,
          NRFX_PWM_PIN_NOT_USED, 
          led_reversed| NRFX_PWM_PIN_INVERTED,
          NRFX_PWM_PIN_NOT_USED 
      },
      .irq_priority = APP_IRQ_PRIORITY_LOWEST, 
      .base_clock   = NRF_PWM_CLK_16MHz, 
      .count_mode   = NRF_PWM_MODE_UP, 
      .top_value    = m_top, 
Change
      .load_mode    = NRF_PWM_LOAD_COMMON, 
To
      .load_mode    = NRF_PWM_LOAD_GROUPED, 
      .step_mode    = NRF_PWM_STEP_AUTO

    };
    APP_ERROR_CHECK(nrfx_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL));
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Not totally sure what waveform you want, but these changes allow a PWM output and inverted PWM output ramping from low duty cycle to max duty cycle. The pins order changes because of the mapping for the different modes; &lt;em&gt;GROUPED&lt;/em&gt; uses 2 entries per step controlling 2 pins each.&lt;/p&gt;
&lt;p&gt;In the code you posted it looks like you used &lt;em&gt;m_step&lt;/em&gt; erroneously instead of &lt;em&gt;step&lt;/em&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286609?ContentTypeID=1</link><pubDate>Sun, 27 Dec 2020 17:20:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:76106d20-1fe1-4f12-ba00-a196df2e9bd7</guid><dc:creator>refer_pin</dc:creator><description>[quote userid="65515" url="~/f/nordic-q-a/69825/inverting-pwm-in-nrf52840-pinn/286605#286605"]&lt;em&gt;GROUPED&lt;/em&gt; or &lt;em&gt;WAVEFORM&lt;/em&gt; or &lt;em&gt;SINGLE&lt;/em&gt;[/quote]
&lt;p&gt;Which one of them should I use? and how could I integrate them to my code? what changes will I make?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286605?ContentTypeID=1</link><pubDate>Sat, 26 Dec 2020 17:08:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:69f186da-ba10-4879-8b8c-bcea84ff22b7</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Sorry, I wasn&amp;#39;t being very clear. To get differential (or inverse or antiphase or full-bridge, all same thing) output don&amp;#39;t use&amp;nbsp;&lt;em&gt;NRF_PWM_LOAD_COMMON&lt;/em&gt; but instead &lt;em&gt;GROUPED&lt;/em&gt; or &lt;em&gt;WAVEFORM&lt;/em&gt; or &lt;em&gt;SINGLE&lt;/em&gt; as these each use a separate value for each pin. This requires a bigger buffer of course; the inverted value has the &amp;quot;| 0x8000&amp;quot; whereas the non-inverted output does not. Have a look at section &lt;em&gt;47.2 Decoder with Easy DMA&lt;/em&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286585?ContentTypeID=1</link><pubDate>Fri, 25 Dec 2020 04:32:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fd85f922-caa1-4f53-8fa1-47640bbd4766</guid><dc:creator>refer_pin</dc:creator><description>&lt;p&gt;Wouldn&amp;#39;t that invert both pwm frequency coming out from pin 0.26 and pin 0.27?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286510?ContentTypeID=1</link><pubDate>Wed, 23 Dec 2020 17:55:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4c3f94d9-aa9d-4894-97c7-bebf38ef84cf</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Try this, every sample for the inverted pin has to have most significant bit set:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;      sequence_values[m_step + i] = (m_top - value) | 0x8000; // Inverted output&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286507?ContentTypeID=1</link><pubDate>Wed, 23 Dec 2020 17:31:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aa7ee515-d9fb-4d74-8ec4-61ea51a18664</guid><dc:creator>refer_pin</dc:creator><description>[quote userid="93921" url="~/f/nordic-q-a/69825/inverting-pwm-in-nrf52840-pinn/286489#286489"]Please see the solution from a similar case: &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/34157/invert-pwm-pin-pwm-pwm-intialize"&gt;Invert PWM PIN, PWM, PWM intialize&lt;/a&gt;&amp;nbsp;.[/quote]
&lt;p&gt;Yeah, that&amp;#39;s where I got the &amp;quot;&lt;span&gt;seq_values-&amp;gt;channel_0 = duty_cycle | 0x8000&lt;/span&gt;&amp;quot; idea but I dont know how to add that to my program&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Inverting PWM in nRF52840 pinn</title><link>https://devzone.nordicsemi.com/thread/286489?ContentTypeID=1</link><pubDate>Wed, 23 Dec 2020 14:49:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7f5d90c4-8f38-4604-8a54-8927515f6e25</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What you have experienced is true.Please see the solution from a similar case: &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/34157/invert-pwm-pin-pwm-pwm-intialize"&gt;Invert PWM PIN, PWM, PWM intialize&lt;/a&gt;&amp;nbsp;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>