<?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 a sweeping tone (2.5KHz to 4.5KHZ at 4Hz rate) from GPIO.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/39116/generating-a-sweeping-tone-2-5khz-to-4-5khz-at-4hz-rate-from-gpio</link><description>HI, 
 In need to generate a sweeping tone to drive a piezzo element, i determined the tone should be sweeping back and forth between 2.5KHz and 4.5KHZ at an approximative rate of 4Hz, what is the best approach to achieve this? i am already using hardware</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 22 Oct 2018 18:52:46 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/39116/generating-a-sweeping-tone-2-5khz-to-4-5khz-at-4hz-rate-from-gpio" /><item><title>RE: Generating a sweeping tone (2.5KHz to 4.5KHZ at 4Hz rate) from GPIO.</title><link>https://devzone.nordicsemi.com/thread/153919?ContentTypeID=1</link><pubDate>Mon, 22 Oct 2018 18:52:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:19997dca-4050-4746-ab3c-0bb6e9296c0d</guid><dc:creator>lo</dc:creator><description>&lt;p&gt;HI,&lt;/p&gt;
&lt;p&gt;Thank you, pretty neat!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Generating a sweeping tone (2.5KHz to 4.5KHZ at 4Hz rate) from GPIO.</title><link>https://devzone.nordicsemi.com/thread/151362?ContentTypeID=1</link><pubDate>Wed, 03 Oct 2018 08:38:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d1fa3eb0-dff0-4b08-a7af-7b0a2f6254d7</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You can use the PWM peripheral&amp;#39;s &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/pwm.html?cp=2_1_0_46_1#concept_wxj_hnw_nr"&gt;Waveform Mode and EasyDMA&lt;/a&gt; to achieve this. From the documentation:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;quot;A special mode of operation is available when DECODER.LOAD is set to WaveForm. In this mode, up to three PWM channels can be enabled - OUT[0] to OUT[2]. In RAM, four values are loaded at a time: the first, second and third location are used to load the values, and the fourth RAM location is used to load the COUNTERTOP register. This way one can have up to three PWM channels with a frequency base that changes on a per PWM period basis. This mode of operation is useful for arbitrary wave form generation in applications such as LED lighting.&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;By configuring the waveform&amp;nbsp;correctly you can make frequency sweeps like this and have it run autonomously:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-c4e627b2d0ff4b61a551be13d3ad5e4f/pastedimage1538555648940v1.png" alt=" " /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Here is some code to get you started (SDK 15.2.0).&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;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 WAVE_FORM_LENGTH 8

// Declare a PWM driver instance structure
nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0);

// Function for initializing the PWM
void init_pwm(void)
{
    uint32_t err_code;
    // Declare a configuration structure and use a macro to instantiate it with default parameters.
    nrfx_pwm_config_t pwm_config = NRFX_PWM_DEFAULT_CONFIG;

    // We must override some of the parameters:
    pwm_config.output_pins[0] = LED_1; // Connect LED_1 on the nRF52832 DK to PWM Channel 0
    pwm_config.load_mode    = NRF_PWM_LOAD_WAVE_FORM; // Use individual duty cycle for each PWM channel
    pwm_config.base_clock   = NRF_PWM_CLK_1MHz;
    
    // Pass config structure into driver init() function 
    err_code = nrfx_pwm_init(&amp;amp;m_pwm0, &amp;amp;pwm_config, NULL);
    APP_ERROR_CHECK(err_code);
}



// Structure holding duty cycle and counter top values
static nrf_pwm_values_wave_form_t pwm_wave_form_values[WAVE_FORM_LENGTH];

// Genreate PWM values 
static void generate_pwm_wave_form(void)
{
    uint16_t counter_top = 100;
    
    for(int i=0; i&amp;lt;WAVE_FORM_LENGTH; i++)
    {
        pwm_wave_form_values[i].counter_top = counter_top;
        pwm_wave_form_values[i].channel_0 = counter_top / 2;
        counter_top = counter_top * 2;
    }
}

// Structure for defining a sequence of PWM duty cycles
static nrf_pwm_sequence_t pwm_sequence =
{
    .values.p_wave_form = pwm_wave_form_values,
    .length          = (sizeof(pwm_wave_form_values) / sizeof(uint16_t)),
    .repeats         = 1,
    .end_delay       = 0
};

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 HF Crystal to generate accurate clock frequency for PWM
    NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1;
    while(NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0)
    {
        ;
    }

    init_pwm();
    generate_pwm_wave_form();
    nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;pwm_sequence, 1, NRFX_PWM_FLAG_LOOP);

    for (;;)
    {
        // Wait for an event.
        __WFE();
    }
}

&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>