<?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>How to decode pwm?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/33318/how-to-decode-pwm</link><description>Hello everybody, 
 I am using sdk14.2, s132 with Nrf52832. I want to define the pin of the NRF52 as the input and decode the incoming pwm signal. For example, I have a signal at 100 Hz and the duty cycle is as follows; {70%, 80%, 90%, 60%, 50%, 40%, 30</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 13 Apr 2018 10:31:51 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/33318/how-to-decode-pwm" /><item><title>RE: How to decode pwm?</title><link>https://devzone.nordicsemi.com/thread/128211?ContentTypeID=1</link><pubDate>Fri, 13 Apr 2018 10:31:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:37f91ce3-cce8-4750-b5fb-bc26d673221a</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;I assumed the Period was known.if it is 100Hz, you can use this value as the period. If you need to measure the period, you must also capture the CC on the rising edge, to see how many timer ticks there are in a period.&lt;/p&gt;
&lt;p&gt;You can &amp;quot;short&amp;quot; several tasks to one event.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;NRF_PPI-&amp;gt;CH[0].EEP = (uint32_t)&amp;amp;NRF_GPIOTE-&amp;gt;EVENTS_IN[0];&lt;/p&gt;
&lt;p&gt;NRF_PPI-&amp;gt;CH[0].TEP = (uint32_t)&amp;amp;NRF_TIMER3-&amp;gt;TASKS_CAPTURE[0];&lt;/p&gt;
&lt;p&gt;NRF_PPI-&amp;gt;CH[0].FORK = (uint32_t)&amp;amp;NRF_TIMER3-&amp;gt;TASKS_STOP;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;this snippet will both capture the timer and stop the timer.&lt;/p&gt;
&lt;p&gt;Then you will have to use different events for rising and falling to capture both of them. On the rising, you will have to capture and&amp;nbsp;clear&amp;nbsp;the&amp;nbsp;timer.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to decode pwm?</title><link>https://devzone.nordicsemi.com/thread/128156?ContentTypeID=1</link><pubDate>Fri, 13 Apr 2018 06:42:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:916fa9e8-68e7-4609-8ab9-0793a6d24e33</guid><dc:creator>purgoufr</dc:creator><description>&lt;p&gt;The program code is as follows;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void timer_init_2()
{
	NRF_TIMER1-&amp;gt;TASKS_STOP = 1;
	NRF_TIMER1-&amp;gt;MODE = TIMER_MODE_MODE_Timer;
	NRF_TIMER1-&amp;gt;PRESCALER = 8; //8;	// Fhck / 2^8 
	NRF_TIMER1-&amp;gt;CC[0] =  62500;	// 62500 - 1s
	
	NRF_TIMER1-&amp;gt;BITMODE = (TIMER_BITMODE_BITMODE_16Bit &amp;lt;&amp;lt; TIMER_BITMODE_BITMODE_Pos);	
	
	NRF_TIMER1-&amp;gt;TASKS_CLEAR = 1;
	NRF_TIMER1-&amp;gt;INTENSET = (TIMER_INTENSET_COMPARE0_Enabled &amp;lt;&amp;lt; TIMER_INTENSET_COMPARE0_Pos);
	
	NRF_TIMER1-&amp;gt;EVENTS_COMPARE[0] = 0;
}

static void counter_init()
{
	NRF_TIMER2-&amp;gt;TASKS_STOP = 1;	
	NRF_TIMER2-&amp;gt;MODE = TIMER_MODE_MODE_Counter;
	NRF_TIMER2-&amp;gt;BITMODE = (TIMER_BITMODE_BITMODE_24Bit &amp;lt;&amp;lt; TIMER_BITMODE_BITMODE_Pos);
	NRF_TIMER2-&amp;gt;TASKS_CLEAR = 1;
	NRF_TIMER2-&amp;gt;EVENTS_COMPARE[0] = 0;
}

static void gpiote_init(uint32_t pin)
{
	NRF_GPIOTE-&amp;gt;CONFIG[0] 	= 	0x01 &amp;lt;&amp;lt; 0; 								// MODE: Event
	NRF_GPIOTE-&amp;gt;CONFIG[0] 	|= 	pin &amp;lt;&amp;lt; 8;								// Pin number
	NRF_GPIOTE-&amp;gt;CONFIG[0] 	|= 	NRF_GPIOTE_POLARITY_LOTOHI	&amp;lt;&amp;lt; 16;		// Event rising edge 	
}

static void ppi_timer_stop_counter_init()
{
	NRF_PPI-&amp;gt;CHEN |= 1 &amp;lt;&amp;lt; 0;
	*(&amp;amp;(NRF_PPI-&amp;gt;CH0_EEP)) = (uint32_t)&amp;amp;NRF_TIMER1-&amp;gt;EVENTS_COMPARE[0];
	*(&amp;amp;(NRF_PPI-&amp;gt;CH0_TEP)) = (uint32_t)&amp;amp;NRF_TIMER2-&amp;gt;TASKS_STOP;
	NRF_PPI-&amp;gt;CHENSET |= 1 &amp;lt;&amp;lt; 0;
}

static void ppi_gpiote_counter_init()
{
	NRF_PPI-&amp;gt;CHEN |= 1 &amp;lt;&amp;lt; 1;
	*(&amp;amp;(NRF_PPI-&amp;gt;CH1_EEP)) = (uint32_t)&amp;amp;NRF_GPIOTE-&amp;gt;EVENTS_IN[0];
	*(&amp;amp;(NRF_PPI-&amp;gt;CH1_TEP)) = (uint32_t)&amp;amp;NRF_TIMER2-&amp;gt;TASKS_COUNT;
	NRF_PPI-&amp;gt;CHENSET |= 1 &amp;lt;&amp;lt; 1;
}

void TIMER1_IRQHandler(void) 
{
	if (NRF_TIMER1-&amp;gt;EVENTS_COMPARE[0] != 0)
	{
		NRF_TIMER1-&amp;gt;EVENTS_COMPARE[0] = 0;
		NRF_TIMER2-&amp;gt;TASKS_CAPTURE[0] = 1;
				
		NRF_LOG_INFO(&amp;quot;cc: %dHz&amp;quot;, NRF_TIMER2-&amp;gt;CC[0]);
		
      	
		NRF_TIMER1-&amp;gt;TASKS_CLEAR = 1;
		NRF_TIMER2-&amp;gt;TASKS_CLEAR = 1;	
						
		NRF_TIMER2-&amp;gt;TASKS_START = 1;			
    }
}


int main(void)
{
    .
    .
    .
    
    // Start scanning for peripherals and initiate connection
    // with devices that advertise KALE UUID.
			NRF_LOG_INFO(&amp;quot;KEYFOB example started.&amp;quot;);
		
		 // Soft Device initialization..
			nrf_gpio_cfg_output(30);
			NVIC_EnableIRQ(TIMER1_IRQn);
			NVIC_SetPriority(TIMER1_IRQn, 7);	

			nrf_gpio_cfg_input(FREQ_MEASURE_PIN, NRF_GPIO_PIN_NOPULL);

			counter_init();
			timer_init_2();
			gpiote_init(FREQ_MEASURE_PIN);
			ppi_gpiote_counter_init();
			ppi_timer_stop_counter_init();

			NRF_TIMER1-&amp;gt;TASKS_START = 1;
			NRF_TIMER2-&amp;gt;TASKS_START = 1;
			
	for(;;)
	{
        if (NRF_LOG_PROCESS() == false)
        {
            nrf_pwr_mgmt_run();
        }
    }
}

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here, the frequency value is obtained from the number of rising edges.&amp;nbsp;To calculate the duty cycle it is necessary to count the falling edge as you know. I will calculate the duty cycle from the &amp;quot;(rising - falling)/Period&amp;quot; equation.&amp;nbsp;I could not figure out how to count both rising and falling.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to decode pwm?</title><link>https://devzone.nordicsemi.com/thread/128095?ContentTypeID=1</link><pubDate>Thu, 12 Apr 2018 13:59:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:49f4a104-6eeb-4fce-86bc-d919890bce13</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Can you please check out &lt;a href="https://devzone.nordicsemi.com/support-private/support/205105" target="_blank" rel="noopener noreferrer"&gt;this post&lt;/a&gt;? It is a post I handled today, but the answer would be pretty much the same in this case. You should see how it measures the time between a rise and fall on an input gpio.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If anything is unclear, or it doesn&amp;#39;t work, please let me know.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>