<?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>PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/14694/pwm-frequency-measurement-on-nrf52-with-sdk-11</link><description>Hi, 
 I need to measure the frequency of a PWM signal with 50% duty cycle through one of the GPIO pins. The frequency of input PWM signal ranges between 100Hz to 1000Hz. 
 I am hoping to measure the PWM frequency in the following way:
I&amp;#39;ll wait for</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 04 Jul 2016 05:55:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/14694/pwm-frequency-measurement-on-nrf52-with-sdk-11" /><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56081?ContentTypeID=1</link><pubDate>Mon, 04 Jul 2016 05:55:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:48d66261-c00d-4d5d-a752-6a488480ee94</guid><dc:creator>&amp;#216;yvind Karlsen</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You can set up an application timer, as shown in &lt;a href="https://devzone.nordicsemi.com/tutorials/19/"&gt;this tutorial&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56080?ContentTypeID=1</link><pubDate>Thu, 30 Jun 2016 21:34:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:75cd904e-30af-476e-b94a-9c4b372f2fb3</guid><dc:creator>Mark</dc:creator><description>&lt;p&gt;Bret, I want to go back to the initial answer you posted &amp;quot;It will be a lot more accurate to let the PWM transitions cause an interrupt and have the ISR increment a counter. Then let the whole works run for, say, 5 seconds. Divide the number of counts by 5 and you will have Hz.&amp;quot;&lt;/p&gt;
&lt;p&gt;So Based on the following code I can have the ISR increment a counter. But how can I start and stop the ISR within a specified period of time? Does the following code work reliably?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static uint32_t IRQcount = 0;
static const uint32_t oneSecond = 1000000;

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    IRQcount++;
}
/**
  * @brief Function for configuring: PIN_IN pin for input, PIN_OUT pin for output, 
  * and configures GPIOTE to give an interrupt on pin change.
  */
static void gpio_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;

    err_code = nrf_drv_gpiote_in_init(PIN_IN, &amp;amp;in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(PIN_IN, false);
}

/**
  * @brief Function for application main entry.
  */
int main(void)
{
    gpio_init();

    const int period_30ms = 30000;
    uint16_t i;
    bool time_start = false;
    uint32_t freq_value = 0;

        while (true)
        {
            if(time_start == true)
            {
                       nrf_drv_gpiote_in_event_enable(PIN_IN, true);
                       for(i = 0; i &amp;lt; 30000; i++);
                       nrf_drv_gpiote_in_event_enable(PIN_IN, false);
                       freq_value = IRQcount*oneSecond/period_30ms; //frequency in Hz
                       IRQcount = 0;
                       time_start = false;
            }
            nrf_delay_ms(1000);
            time_start = true;
        }
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56079?ContentTypeID=1</link><pubDate>Fri, 24 Jun 2016 00:45:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6016a170-e2f4-469d-aaec-8d1fe6f29c55</guid><dc:creator>Bret Foreman</dc:creator><description>&lt;p&gt;So if you set your pre-scaler to increment the timer every 1 uS, then your 10mS time will be 10,000 ticks. A 16-bit timer can count up to 64K ticks so you are good.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56078?ContentTypeID=1</link><pubDate>Fri, 24 Jun 2016 00:34:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3f80751e-8b00-406f-ab65-f662d3f15a5d</guid><dc:creator>Bret Foreman</dc:creator><description>&lt;p&gt;You calculate the uS for each tick according to how you set up the timer. The timer has a pre-scaler that divides the system clock down into counter ticks. You set up that pre-scaler such that your timer doesn&amp;#39;t overflow before your event occurs. In your case, your longest time between events is 10mS (100Hz). Does that make sense?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56077?ContentTypeID=1</link><pubDate>Fri, 24 Jun 2016 00:14:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:789ebf9a-33fe-4df4-a7ec-6e93bbd57c03</guid><dc:creator>Mark</dc:creator><description>&lt;p&gt;OK, I see... Yeah, even Arduino doesn&amp;#39;t use a timer to actually measure the rise or fall time. It counts the number of ticks(increments) that happen from 0V to 5V, and converts them to microseconds. Now, I noticed SDK 11 has a function &lt;code&gt;nrf_drv_timer_us_to_ticks()&lt;/code&gt; but didn&amp;#39;t see any &lt;code&gt;nrf_drv_timer_ticks_to_us()&lt;/code&gt;. How can I make such function?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56071?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2016 20:11:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b78207d8-afd9-4f96-b859-3cc1cb590c12</guid><dc:creator>Bret Foreman</dc:creator><description>&lt;p&gt;I am familiar with the Arduino IDE. With the Nordic, you are usually using a SoftDevice, a kind of operating system that Arduino does not have. With the SoftDevice, accurately measuring fast events gets complicated. Here&amp;#39;s how to do it: &lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk51.v10.0.0%2Fhardware_driver_timer.html&amp;amp;cp=4_0_1_2_10"&gt;infocenter.nordicsemi.com/index.jsp&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56070?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2016 19:20:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ec93c255-0c02-4262-ae27-be56d1408089</guid><dc:creator>Mark</dc:creator><description>&lt;p&gt;I don&amp;#39;t care how fast the frequency varies, but I need to measure it every millisecond, and within 1% accuracy.&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t think I need to know about FM demodulation. If I could use a timer to measure how fast the GPIO pin goes from 0V to 3.3V and vice versa in the next cycle, that should be accurate enough for me.&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t know how familiar you are with Arduino IDE, but there&amp;#39;s a &lt;a href="https://www.arduino.cc/en/Reference/PulseIn"&gt;PulseIn()&lt;/a&gt; function that measures PWM frequency pretty good. At least it was pretty accurate for me when I was measuring PWM frequencies from 100Hz to 500Hz.&lt;/p&gt;
&lt;p&gt;I just want to accomplish the same goal with nRF52, and it seems to be pretty complicated!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56076?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2016 18:46:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1b6d8ae2-436a-4528-adfb-be1e4d812fe6</guid><dc:creator>Bret Foreman</dc:creator><description>&lt;p&gt;Ah, your OP didn&amp;#39;t mention that you want to measure a &lt;em&gt;varying&lt;/em&gt; frequency. How fast can it vary, how often do you need to measure it, and what accuracy do you need? By the way, periodically measuring a varying frequency is essentially FM demodulation. You might browse that topic in Wikipedia to see the various ways it can be done and understand their benefits and drawbacks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56075?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2016 18:39:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:84270ed9-b3c5-45a4-9ead-1d00455e7cc9</guid><dc:creator>Mark</dc:creator><description>&lt;p&gt;Well, as I stated earlier the PWM frequency changes between 100Hz and 1000Hz. The frequency can go from 100Hz to 1000Hz in 5, 6, 7, or even 10 seconds; so there is a slow ramp up for frequency. If I try to measure the PWM frequency within 5 seconds it won&amp;#39;t be accurate and I will see an average value of 500Hz. Whereas if I try to measure the frequency as the state of GPIO pin changes I can measure frequency within 1 to 10 milliseconds.&lt;/p&gt;
&lt;p&gt;I used to measure frequencies with 8-bit ATmega328p processor and accuracy was never an issue for me, and it was always accurate. Why would I encounter lower accuracy if I try to measure frequency in a window of faster than 5 seconds?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56074?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2016 17:45:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f63fd8dc-1655-467d-9cd3-a10314007518</guid><dc:creator>Bret Foreman</dc:creator><description>&lt;p&gt;I should add that people use this technique all the time for measuring the frequency of external periodic events. It&amp;#39;s a standard approach.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56073?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2016 17:43:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bda56611-7c8f-4a61-9326-75828ab61777</guid><dc:creator>Bret Foreman</dc:creator><description>&lt;p&gt;Mark, you wouldn&amp;#39;t be stalling the CPU. You would just increment a counter inside the ISR, then exit the ISR. The whole process will take a couple of uS each time the PWM transition occurs. How quickly do you need your result? You could count for less than 5 seconds if you are willing to accept lower accuracy in the result. It&amp;#39;s a direct, linear trade-off between accuracy and measurement time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56072?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2016 17:32:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:53793cff-b2b4-4986-9bf8-607d074e826e</guid><dc:creator>Mark</dc:creator><description>&lt;p&gt;Isn&amp;#39;t 5 seconds a long time?&lt;/p&gt;
&lt;p&gt;How can I take advantage of PPI so I won&amp;#39;t stall CPU while measuring frequency?&lt;/p&gt;
&lt;p&gt;Could you please describe in more details how I can setup GPIO pin, timer, and ISR in order to measure frequency?&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM Frequency Measurement on nRF52 with SDK 11</title><link>https://devzone.nordicsemi.com/thread/56069?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2016 01:50:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:015e42f4-d52d-4044-aeec-18bf59696f0c</guid><dc:creator>Bret Foreman</dc:creator><description>&lt;p&gt;It will be a lot more accurate to let the PWM transitions cause an interrupt and have the ISR increment a counter. Then let the whole works run for, say, 5 seconds. Divide the number of counts by 5 and you will have Hz.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>