<?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>Correct way to change frequency with APP_PWM.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/68342/correct-way-to-change-frequency-with-app_pwm</link><description>I&amp;#39;m trying to create a siren tone using the pwm library, that ramps up and down between 2kHz and 3kHz. 
 Initially I started with the example provided in SDK v15.3.0 nRF5_SDK\examples\peripheral\pwm_library using this with the nrf52840 DK. 
 Example was</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 17 Nov 2020 14:41:41 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/68342/correct-way-to-change-frequency-with-app_pwm" /><item><title>RE: Correct way to change frequency with APP_PWM.</title><link>https://devzone.nordicsemi.com/thread/280402?ContentTypeID=1</link><pubDate>Tue, 17 Nov 2020 14:41:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c7cc58ae-f7ac-4cd9-acf3-9f26461d2235</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Great to hear that you figured it out, and thanks for sharing your code &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Correct way to change frequency with APP_PWM.</title><link>https://devzone.nordicsemi.com/thread/280251?ContentTypeID=1</link><pubDate>Tue, 17 Nov 2020 07:52:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ef7c7806-d30f-439f-aeee-5ad5d4cd9755</guid><dc:creator>lomby</dc:creator><description>&lt;p&gt;Thankfully its not a complex sound wave. I managed to work one of the examples as per your suggestion.&lt;/p&gt;
&lt;p&gt;This seemed to work fine - and couldn&amp;#39;t reproduce the janky pulse widths in my first attempt.&lt;br /&gt;If this helps anyone else, an example would look something like this.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;nrf_drv_timer.h&amp;quot;
#include &amp;quot;nrf_drv_ppi.h&amp;quot;
#include &amp;quot;nrf_drv_gpiote.h&amp;quot;

static const nrf_drv_timer_t m_ext_timer0 = NRF_DRV_TIMER_INSTANCE(0);

void timer_0_isr_handler(nrf_timer_event_t event_type, void * p_context)
{
    if(event_type == NRF_TIMER_EVENT_COMPARE1) 
    { 
      /* update new sequence here */
    }
}

static void _init_gpiote(uint32_t pin)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_out_config_t out_cfg = GPIOTE_CONFIG_OUT_TASK_TOGGLE(true);
    err_code = nrf_drv_gpiote_out_init((nrf_drv_gpiote_pin_t)pin,&amp;amp;out_cfg);
    APP_ERROR_CHECK(err_code);
}

static void _init_ppi(uint32_t pin)
{
    ret_code_t err_code;

    uint32_t mark_evt_addr;
    uint32_t gpiote_task_addr;
    nrf_ppi_channel_t mark_ppi_channel;

    uint32_t period_evt_addr;
    uint32_t gpiote_task_addr1;
    nrf_ppi_channel_t period_ppi_channel;
    
    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    // Set the pulse width channel.
    err_code = nrf_drv_ppi_channel_alloc(&amp;amp;mark_ppi_channel);
    APP_ERROR_CHECK(err_code);

    mark_evt_addr = nrf_drv_timer_event_address_get(&amp;amp;m_ext_timer0, NRF_TIMER_EVENT_COMPARE0);
    gpiote_task_addr = nrf_drv_gpiote_clr_task_addr_get(pin);

    err_code = nrf_drv_ppi_channel_assign(mark_ppi_channel, mark_evt_addr, gpiote_task_addr);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_ppi_channel_enable(mark_ppi_channel);
    APP_ERROR_CHECK(err_code);

    //Set the channel for the total period
    err_code = nrf_drv_ppi_channel_alloc(&amp;amp;period_ppi_channel);
    APP_ERROR_CHECK(err_code);

    period_evt_addr = nrf_drv_timer_event_address_get(&amp;amp;m_ext_timer0, NRF_TIMER_EVENT_COMPARE1);
    gpiote_task_addr1 = nrf_drv_gpiote_set_task_addr_get(pin);

    err_code = nrf_drv_ppi_channel_assign(period_ppi_channel, period_evt_addr, gpiote_task_addr1);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_ppi_channel_enable(period_ppi_channel);
    APP_ERROR_CHECK(err_code);

}

static void _init_timer(void)
{
    ret_code_t err_code;

    nrf_drv_timer_config_t timer_cfg =    {
        .frequency          = NRF_TIMER_FREQ_16MHz,
        .mode               = NRF_TIMER_MODE_TIMER,
        .bit_width          = NRF_TIMER_BIT_WIDTH_32,
        .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
        .p_context          = NULL
    };

    err_code = nrf_drv_timer_init(&amp;amp;m_ext_timer0, &amp;amp;timer_cfg, timer_0_isr_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_timer_compare(
        &amp;amp;m_ext_timer0,
        NRF_TIMER_CC_CHANNEL0,
        2400, //duty @30%
        false
    );

    //2khz waveform @ 16MHz - clear cc1 short, and interrupt for sequence update.
    nrf_drv_timer_extended_compare(
        &amp;amp;m_ext_timer0, 
        NRF_TIMER_CC_CHANNEL1, 
        8000, 
        NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, 
        true
    );

}

int main(void)
{
    _init_gpiote(30);
    _init_ppi(30);
    _init_timer();

    nrf_drv_gpiote_out_task_enable(30);
    nrf_drv_timer_enable(&amp;amp;m_ext_timer0);

    while (true)
    {
        // Do Nothing - GPIO can be toggled without software intervention.
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;At any rate, thanks for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Correct way to change frequency with APP_PWM.</title><link>https://devzone.nordicsemi.com/thread/280139?ContentTypeID=1</link><pubDate>Mon, 16 Nov 2020 14:30:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be4f475f-aa8d-4348-a481-6fff83791736</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I would not recommend using the app_pwm driver for this, for a couple of reasons.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Essentially app_pwm was developed for the older nRF51 devices which did not have a dedicated PWM controller, so it&amp;#39;s running PWM using a TIMER, but it is not designed to change the PWM frequency dynamically, only the duty cycle, making it less suitable for your application.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If I am correct you are simply looking at outputting a square wave with varying frequency, you&amp;#39;re not&amp;nbsp;looking at generating more advanced sound waves?&lt;/p&gt;
&lt;p&gt;If so I would definitely recommend the TIMER-&amp;gt;PPI-&amp;gt;GPIOTE method. Essentially you have to set up a GPIOTE channel in OUT mode with toggle enabled, and connect a compare task of the timer to the out task of the GPIOTE through PPI. Then you just need to change the compare value of the timer to change the sound frequency.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Correct way to change frequency with APP_PWM.</title><link>https://devzone.nordicsemi.com/thread/279995?ContentTypeID=1</link><pubDate>Sat, 14 Nov 2020 02:33:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:74b241d7-5218-4692-b656-867818abef41</guid><dc:creator>lomby</dc:creator><description>&lt;p&gt;Fair call. Updated the OP.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Correct way to change frequency with APP_PWM.</title><link>https://devzone.nordicsemi.com/thread/279958?ContentTypeID=1</link><pubDate>Fri, 13 Nov 2020 15:43:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bd38c458-6165-49c9-a063-2e78dbbb0770</guid><dc:creator>awneil</dc:creator><description>[quote userid="95166" url="~/f/nordic-q-a/68342/correct-way-to-change-frequency-with-app_pwm"]fixed duty PWM[/quote]
&lt;p&gt;&amp;quot;PWM&amp;quot; = Pulse Width Modulation - so, if your not varying the duty, it&amp;#39;s not PWM!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>