<?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>Generating Pulses</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/24855/generating-pulses</link><description>Hello, I am trying to generate pulses with the nRF52832 chip.I created a basic setup for test purposes; I simply connected a LED to the PWM output, but even though I put 0 into the sequence duty cycle values my LED still light up. My code is below. I</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 05 Sep 2017 09:01:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/24855/generating-pulses" /><item><title>RE: Generating Pulses</title><link>https://devzone.nordicsemi.com/thread/97884?ContentTypeID=1</link><pubDate>Tue, 05 Sep 2017 09:01:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3353100a-b547-463a-9934-84f5e41f9016</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;The PWM module implements a counter that is counting up to the countertop value you set. In my example code the PWM module is counting up to the value 100. I have set the flag &lt;code&gt;NRF_DRV_PWM_FLAG_LOOP&lt;/code&gt;, so it will restart the counting when it comes to 100 and start over from 0.&lt;/p&gt;
&lt;p&gt;While the PWM module is counting on it’s way to 100, it will reach the number set by the sequence value. This can in my example be minimum 0 and max 100. When this value is reached, the PWM output signal will be toggled/reversed. Let’s say that the sequence value is set to 60, and that the PWM started with a low signal(0 volt). It will then switch to output a high signal(e.g. 3V) when it reach 60, and be high(3V) until it reaches 100(countertop value).&lt;/p&gt;
&lt;p&gt;The bit[15] in seq_value determines if we start with the signal high or low(if we should have a rising or falling edge when we reach the seq_value). If you set the bit[15] to 0, the signal starts low, and goes high when you reach the seq_value. If you set the bit[15] to 1, the signal starts high and goes low.&lt;/p&gt;
&lt;p&gt;More information can be found &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/pwm.html?cp=2_1_0_46_1#concept_wxj_hnw_nr"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Generating Pulses</title><link>https://devzone.nordicsemi.com/thread/97883?ContentTypeID=1</link><pubDate>Mon, 04 Sep 2017 20:45:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:63c5afbc-639e-4d8e-960f-d6bfe849eae6</guid><dc:creator>Kerem</dc:creator><description>&lt;p&gt;I feel sorry to ask this but I did not understand the &amp;#39;polarity&amp;#39; concept. What exactly is it?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Generating Pulses</title><link>https://devzone.nordicsemi.com/thread/97881?ContentTypeID=1</link><pubDate>Mon, 04 Sep 2017 19:03:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:357d2a2b-607b-4349-9ced-3c9185ca7f78</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;If you are first starting you project now, I would recommend downloading the newest SDK from &lt;a href="http://developer.nordicsemi.com/nRF5_SDK/nRF5_SDK_v14.x.x/"&gt;here&lt;/a&gt;, and use SoftDevice S132 v5.0.0&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Generating Pulses</title><link>https://devzone.nordicsemi.com/thread/97882?ContentTypeID=1</link><pubDate>Mon, 04 Sep 2017 16:00:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89e87f86-be12-4df1-80a3-a78065386433</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I will strongly recommend to use the &lt;code&gt;nrf_drv_pwm&lt;/code&gt; functions, and not use the PWM HAL(Hardware access layer) functions directly. Note that you are overflowing the &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/pwm.html?cp=2_1_0_46_4_6#register.COUNTERTOP"&gt;COUNTERTOP&lt;/a&gt; register by using the value 64000. Max value is 32767.&lt;/p&gt;
&lt;p&gt;Also note that with the sequence duty cycle values the most significant bit[15] denotes the polarity. So if you want seq_value  0 to correspond to 0 V, you need to set bit 15 to 1. E.g. by using bitwise OR.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;seq_values-&amp;gt;channel_0 = duty_cycle | 0x8000;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Code example that shows how to set a duty cycle between 0 and 100 % that uses 10kHz PWM:&lt;/p&gt;
&lt;pre&gt;&lt;code&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;nrf_drv_clock.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;


#define OUTPUT_PIN 16

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
};


// Set duty cycle between 0 and 100%
void pwm_update_duty_cycle(uint8_t duty_cycle)
{

    // Check if value is outside of range. If so, set to 0%
    if(duty_cycle &amp;gt;= 100)
    {
        seq_values-&amp;gt;channel_0 = 0;
    }
    else
    {
        
        seq_values-&amp;gt;channel_0 = duty_cycle | 0x8000; // Change polarity 
    }
   
    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
            NRF_DRV_PWM_PIN_NOT_USED,             // 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    = 100,
        .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));

}


int main(void)
{

    // Start clock for accurate frequencies
    NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1; 
    // Wait for clock to start
    while(NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0) ;

    pwm_init();
    pwm_update_duty_cycle(40);

    for (;;)
    {

    }
}


/** @} */
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>