<?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>Change in GPIO to start/stop timer and fire interrupt</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/8528/change-in-gpio-to-start-stop-timer-and-fire-interrupt</link><description>Hello all, 
 This is my first post to the developer zone. 
 I&amp;#39;m creating a product using RFduino, which uses the nRF51822 chip for Bluetooth LE. (See www.rfduino.com/.../rfduino.datasheet.pdf ) 
 I&amp;#39;m an iOS developer with some legacy assembler experience</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 04 Feb 2016 23:56:37 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/8528/change-in-gpio-to-start-stop-timer-and-fire-interrupt" /><item><title>RE: Change in GPIO to start/stop timer and fire interrupt</title><link>https://devzone.nordicsemi.com/thread/31181?ContentTypeID=1</link><pubDate>Thu, 04 Feb 2016 23:56:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:69db11d7-c3ac-4e0b-a840-c93289473177</guid><dc:creator>Tim</dc:creator><description>&lt;p&gt;Hi Osenjw. Glad it was helpful for you. See this post on the RFduino forum:
&lt;a href="http://forum.rfduino.com/index.php?topic=1202.msg4518#msg4518"&gt;forum.rfduino.com/index.php&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Change in GPIO to start/stop timer and fire interrupt</title><link>https://devzone.nordicsemi.com/thread/31180?ContentTypeID=1</link><pubDate>Thu, 04 Feb 2016 02:49:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e6333365-4c4e-4466-85a7-99490f71657d</guid><dc:creator>osenjw</dc:creator><description>&lt;p&gt;Tried it.  It worked well!  Thanks for the effort.  You saved me hours on almost the exact same question.  Have you thought of making a similar post on the RFduino forum?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Change in GPIO to start/stop timer and fire interrupt</title><link>https://devzone.nordicsemi.com/thread/31179?ContentTypeID=1</link><pubDate>Thu, 10 Sep 2015 02:42:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:531a6585-577a-4bf2-951f-02eabc51353b</guid><dc:creator>Tim</dc:creator><description>&lt;p&gt;Hi ...&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been making good progress on this and will post full working source, but for now a simple question.&lt;/p&gt;
&lt;p&gt;The code below uses GPIOTE to call an interrupt when a pin transitions from LoToHi and also from HiToLo. I&amp;#39;ve tested using two methods, using the RGB button shield and using a phototransistor+comparator (to convert phototransistor to digital 0/1). For the latter, a laser is aimed at the phototransistor and so the code is generating interrupts when the beam is broken and restored.&lt;/p&gt;
&lt;p&gt;In both cases it works generally well except occasionally I get an extra interrupt call. I&amp;#39;ll press the button and get a LoToHi interrupt call, then when I release the button I&amp;#39;ll get an unexpected LoToHi interrupt call immediately followed by a HiToLo interrupt call. Same behaviour when using the phototransistor+comparator.&lt;/p&gt;
&lt;p&gt;Any issues with my code? Is it possible that signal bounce is causing the issue? I understand bounce can be an issue especially with mechanical switches (i.e. RGB button shield), but I thought not so with a phototransistor+comparator.&lt;/p&gt;
&lt;p&gt;Code is here:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#include &amp;lt;RFduinoBLE.h&amp;gt;

int myPin = 5;

void setup() {
  Serial.begin(9600);
    
  pinMode(myPin, INPUT); // using GPIO 5 as input
  
  // Configure GPIOTE channel 0 as event that occurs when pin 5 changes from digital
  // hi to low.
  NRF_GPIOTE-&amp;gt;CONFIG[0] =  (GPIOTE_CONFIG_POLARITY_HiToLo &amp;lt;&amp;lt; GPIOTE_CONFIG_POLARITY_Pos)
              | (myPin &amp;lt;&amp;lt; GPIOTE_CONFIG_PSEL_Pos) // using GPIO 5 as input
              | (GPIOTE_CONFIG_MODE_Event &amp;lt;&amp;lt; GPIOTE_CONFIG_MODE_Pos);
  
  // Configure GPIOTE channel 1 as event that occurs when pin 5 changes from digital
  // low to hi.
  NRF_GPIOTE-&amp;gt;CONFIG[1] =  (GPIOTE_CONFIG_POLARITY_LoToHi &amp;lt;&amp;lt; GPIOTE_CONFIG_POLARITY_Pos)
              | (myPin &amp;lt;&amp;lt; GPIOTE_CONFIG_PSEL_Pos) // using GPIO 5 as input
              | (GPIOTE_CONFIG_MODE_Event &amp;lt;&amp;lt; GPIOTE_CONFIG_MODE_Pos);
  
  NRF_GPIOTE-&amp;gt;INTENSET = GPIOTE_INTENSET_IN0_Msk;
  NRF_GPIOTE-&amp;gt;INTENSET = GPIOTE_INTENSET_IN1_Msk;
  
  // Clear all events.
  NRF_GPIOTE-&amp;gt;EVENTS_IN[0] = 0;
  NRF_GPIOTE-&amp;gt;EVENTS_IN[1] = 0;
  NRF_GPIOTE-&amp;gt;EVENTS_IN[2] = 0;
  NRF_GPIOTE-&amp;gt;EVENTS_IN[3] = 0;
  
  // Enable GPIOTE interrupts and attach my handler.
  NVIC_EnableIRQ(GPIOTE_IRQn);
  attachInterrupt(GPIOTE_IRQn, MY_IRQHandler);
  
}

void MY_IRQHandler(void) {
  
  // Check which event caused interrupt.
  if (NRF_GPIOTE-&amp;gt;EVENTS_IN[0] == 1) {
    
    // Clear the event that caused the interrupt.
    NRF_GPIOTE-&amp;gt;EVENTS_IN[0] = 0;
    
    Serial.println(&amp;quot;interrupt called for HiToLow&amp;quot;);
    
  } else if (NRF_GPIOTE-&amp;gt;EVENTS_IN[1] == 1) {
    
    // Clear the event that caused the interrupt.
    NRF_GPIOTE-&amp;gt;EVENTS_IN[1] = 0;
    
    Serial.println(&amp;quot;interrupt called for LoToHi&amp;quot;);
    
  }
  
  
}

void loop(){
  
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Many thanks,&lt;/p&gt;
&lt;p&gt;Tim&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Change in GPIO to start/stop timer and fire interrupt</title><link>https://devzone.nordicsemi.com/thread/31178?ContentTypeID=1</link><pubDate>Wed, 05 Aug 2015 06:12:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8ec92d2c-b78a-4c52-afa1-dea0d14fb2f9</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;please thank him by also giving an upvote :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Change in GPIO to start/stop timer and fire interrupt</title><link>https://devzone.nordicsemi.com/thread/31177?ContentTypeID=1</link><pubDate>Wed, 05 Aug 2015 04:22:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a77d26b4-08e0-4e23-ab28-7f2c4d9c75e7</guid><dc:creator>Tim</dc:creator><description>&lt;p&gt;Thanks RK. That&amp;#39;s a great start for me.&lt;/p&gt;
&lt;p&gt;Tim&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Change in GPIO to start/stop timer and fire interrupt</title><link>https://devzone.nordicsemi.com/thread/31176?ContentTypeID=1</link><pubDate>Wed, 05 Aug 2015 02:12:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:56de30df-a062-4397-b3a4-652819b23ef6</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;That was a long question. First off you do have access to just about all the resources you need, click the SDK and Documentation link up there ^^^ and go to the Infocenter, there you have the nRF51 manual, the nRF51822 spec, the softdevice API, pretty much everything.&lt;/p&gt;
&lt;p&gt;Using timer interrupts probably isn&amp;#39;t going to work, the softdevice can preempt you pretty randomly. This is better with the latest softdevices and the rev 3 chips which block the CPU for much shorter intervals, so if your timer code runs at high priority and that&amp;#39;s about all you have, you may get enough service to poll your GPIO.&lt;/p&gt;
&lt;p&gt;Knowing about nothing about Arduino I&amp;#39;m guessing the analogRead() function does something slow every time as well.&lt;/p&gt;
&lt;p&gt;If you can use PPI instead of polling you can use an event to trigger the timer to start counting, and another event to stop it again, you interrupt on the second event and read the timer, see how far it counted, then reset it and do it again. PPI (it&amp;#39;s in the manual) is a way of having one event trigger a task without using the CPU, so it&amp;#39;s accurate.&lt;/p&gt;
&lt;p&gt;The problem then comes from the analog to digital piece, you really want a logic 0 or logic 1 from your sense circuit, then it would be easy to hook up a digital IO pin to start and stop the timer. Can you add a simple circuit to do that, convert your analog output into a 1/0?&lt;/p&gt;
&lt;p&gt;If not then the chip contains a low power comparator which will generate events on upward and downward crossings of a reference voltage. Those events can then be used to start and stop the timer. See the LPCOMP chapter in the manual. There are two things which come to mind about using that, one is that the normal analog digital converter must be disabled when using the LPCOMP and I have no idea how you do that with arduino code, and secondly the converter is fast, so your signal may bounce near the threshold giving multiple events each end. There&amp;#39;s ways to deal with that if you get to that point, either in hardware or software.&lt;/p&gt;
&lt;p&gt;So try reading the PPI chapter and the LPCOMP chapter and the TIMER chapter see if you can see how to use two PPI channels to take the LPCOMP UP and DOWN events and trigger the TIMER START and STOP tasks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>