<?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>nrf52840 PWM Driver update duty cycle</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/53950/nrf52840-pwm-driver-update-duty-cycle</link><description>Hello, 
 SDK: 15.3.0 
 Hardware: nRF52840-DK 
 I am attempting to make a simple dimmer with 2 pins/LEDs. I have used the following post. 
 https://devzone.nordicsemi.com/f/nordic-q-a/38607/nrf5-sdk15-0---pwm-driver-just-set-a-fixed-duty-cycle 
 https</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 05 Nov 2019 10:09:52 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/53950/nrf52840-pwm-driver-update-duty-cycle" /><item><title>RE: nrf52840 PWM Driver update duty cycle</title><link>https://devzone.nordicsemi.com/thread/218494?ContentTypeID=1</link><pubDate>Tue, 05 Nov 2019 10:09:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f3b4742-c8df-4ae0-b5d9-aee18e4ae234</guid><dc:creator>H&amp;#229;kon</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;This method for changing duty cycle values is OK, it&amp;#39;s simple and clear.&lt;br /&gt;This&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/39314/pwm-update_function/152441#152441"&gt;post&lt;/a&gt;&amp;nbsp;uses this method as well to implement a neat RGB pwm dimmer using only one PMW instance.&amp;nbsp;&lt;br /&gt;It could be nice to take a look there.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52840 PWM Driver update duty cycle</title><link>https://devzone.nordicsemi.com/thread/218333?ContentTypeID=1</link><pubDate>Mon, 04 Nov 2019 16:08:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0e8c4bf9-4f0d-4252-9d9b-a66b6f2aca86</guid><dc:creator>BEplane</dc:creator><description>&lt;p&gt;I was able to solve the the dimming problem with changing the math operation around. &lt;/p&gt;
&lt;p&gt;Do you have any input on this method vs nrfx_pwm_sequence_values_update?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52840 PWM Driver update duty cycle</title><link>https://devzone.nordicsemi.com/thread/218324?ContentTypeID=1</link><pubDate>Mon, 04 Nov 2019 15:41:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:91ec06aa-dce5-4f77-b4d8-9241dcdb1978</guid><dc:creator>H&amp;#229;kon</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I printed the duty cycle from the update_pwm function. It seems the equation output returned 0 a lot of times.&amp;nbsp;&lt;br /&gt;This is because dividing two integers where duty_cycle is between 0 and 100, with&amp;nbsp;100 will result in 0 (when duty_cycle is less than 100).&lt;br /&gt;Either use float/double or do as my example where I devide PWM_TOP with 100 first, then multiply with duty_cycle.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Could you try me code example attached and see if this suit your needs?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA &amp;quot;AS IS&amp;quot; AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/** @file
 * @defgroup pwm_example_main main.c
 * @{
 * @ingroup pwm_example
 *
 * @brief PWM Example Application main file.
 *
 * This file contains the source code for a sample application using PWM.
 */

#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 PWM_TOP_VALUE 1000

static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);

static nrf_pwm_values_individual_t m_LED_seq_values;
static nrf_pwm_sequence_t const m_LED_seq =
{
    .values.p_individual = &amp;amp;m_LED_seq_values,
    .length          = NRF_PWM_VALUES_LENGTH(m_LED_seq_values),
    .repeats         = 0,
    .end_delay       = 0
};

static void update_LED(uint8_t duty_cycle)
{
    uint16_t pwm_count = PWM_TOP_VALUE/100;
    if(duty_cycle &amp;lt;= 100)
        {
            pwm_count = pwm_count * duty_cycle;
        }
    else
    {
        NRF_LOG_ERROR(&amp;quot;Duty cycle input not valid&amp;quot;);
    }
    
    //nrf_pwm_values_common_t const *p_pwm_count = pwm_count;
    //nrfx_pwm_sequence_values_update(&amp;amp;m_pwm0,0, p_pwm_count);
    NRF_LOG_INFO(&amp;quot;pwm_count: %d&amp;quot;, pwm_count);
    m_LED_seq_values.channel_0 = pwm_count;
    //nrf_drv_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;m_LED_seq, 1, NRF_DRV_PWM_FLAG_LOOP);
}


static void single_LED_dimmer_init(void)
{
    NRF_LOG_INFO(&amp;quot;LED dimmer init&amp;quot;);

    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // 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,  //&amp;lt; Interrupt priority.
        .base_clock   = NRF_PWM_CLK_2MHz,         //&amp;lt; Base clock frequency.
        .count_mode   = NRF_PWM_MODE_UP,          //&amp;lt; Operating mode of the pulse generator counter.
        .top_value    = PWM_TOP_VALUE,            //&amp;lt; Value up to which the pulse generator counter counts.
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,  //&amp;lt; Mode of loading sequence data from RAM.
        .step_mode    = NRF_PWM_STEP_AUTO         //&amp;lt; Mode of advancing the active sequence.
    };
    
    m_LED_seq_values.channel_0 = PWM_TOP_VALUE; // Initially set to 100 %
    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL));
    nrf_drv_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;m_LED_seq, 1, NRF_DRV_PWM_FLAG_LOOP);
}


int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();


    NRF_LOG_INFO(&amp;quot;PWM example started.&amp;quot;);

    // Start with Demo 1, then switch to another one when the user presses
    // button 1 or button 2 (see the &amp;#39;bsp_evt_handler&amp;#39; function).

    single_LED_dimmer_init();
    nrf_delay_ms(2000);
    update_LED(10);
    nrf_delay_ms(2000);
    update_LED(50);
    nrf_delay_ms(2000);
    update_LED(100);
    nrf_delay_ms(2000);
    update_LED(0);


    for (;;)
    {
        // Wait for an event.
        __WFE();

        // Clear the event register.
        __SEV();
        __WFE();

        NRF_LOG_FLUSH();
    }
}


/** @} */
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52840 PWM Driver update duty cycle</title><link>https://devzone.nordicsemi.com/thread/218268?ContentTypeID=1</link><pubDate>Mon, 04 Nov 2019 14:27:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bf2490cf-981e-45c5-bf25-84dd20c541d3</guid><dc:creator>BEplane</dc:creator><description>&lt;p&gt;I am able to build and run the one of the methods, although the LED is not being dimmed. I have tested previously that sequence value = 16k is ~50% duty cycle and the LED is dimmed as expected, but this was by changing the value and recompiling. What I am seeing with this code is the LED is 100%, but for a duty cycle of 50% the LED is turned off. &lt;/p&gt;
&lt;p&gt;SDK: 15.3.0&lt;/p&gt;
&lt;p&gt;Hardware: nRF52840-DK&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&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_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#include &amp;quot;nrf_delay.h&amp;quot;

static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);


static uint8_t m_used = 0;


nrf_pwm_values_individual_t seq_values_test[] = { 16384};
nrf_pwm_sequence_t const seq_test =
{
    .values.p_individual = seq_values_test,
    .length          = NRF_PWM_VALUES_LENGTH(seq_values_test),
    .repeats         = 0,
    .end_delay       = 0
};


static void update_LED(uint16_t duty_cycle)
{
    uint16_t pwm_count = 32768 *(duty_cycle / 100);
    //nrf_pwm_values_common_t const *p_pwm_count = pwm_count;
    //nrfx_pwm_sequence_values_update(&amp;amp;m_pwm0,0, p_pwm_count);
    
    seq_values_test-&amp;gt;channel_0 = pwm_count;
    nrf_drv_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq_test, 1, NRF_DRV_PWM_FLAG_LOOP);
}

static void demo3(void)
{
    NRF_LOG_INFO(&amp;quot;Demo 3&amp;quot;);

    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // 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_2MHz, //NRF_PWM_CLK_125kHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 32768, //25000,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL));
}

static void init_bsp()
{
    APP_ERROR_CHECK(nrf_drv_clock_init());
    nrf_drv_clock_lfclk_request(NULL);

    APP_ERROR_CHECK(app_timer_init());

}


void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
{
    bsp_board_leds_on();
    app_error_save_and_stop(id, pc, info);
}


int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    init_bsp();

    NRF_LOG_INFO(&amp;quot;PWM example started.&amp;quot;);

    // Start with Demo 1, then switch to another one when the user presses
    // button 1 or button 2 (see the &amp;#39;bsp_evt_handler&amp;#39; function).
    demo3();
    nrf_delay_ms(2000);
    update_LED(100);
    nrf_delay_ms(2000);
    update_LED(50);
    nrf_delay_ms(2000);
    update_LED(100);
    nrf_delay_ms(2000);
    update_LED(50);
    NRF_LOG_FLUSH();


    for (;;)
    {
        // Wait for an event.
        __WFE();

        // Clear the event register.
        __SEV();
        __WFE();

        NRF_LOG_FLUSH();
    }
    
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>