<?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 Read Generated Compare Event and Turn LED On</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/40618/how-to-read-generated-compare-event-and-turn-led-on</link><description>I want to turn an LED on when the Counter in a Timer peripheral is equal to 10. 
 
 
 I tried reading from EVENTS_COMPARE[0] (TIMER0 -&amp;gt; EVENTS_COMPARE[0]) in the if argument, but the led is not turning on. One solution I looked at was in the PPI example</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 19 Nov 2018 21:05:07 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/40618/how-to-read-generated-compare-event-and-turn-led-on" /><item><title>RE: How to Read Generated Compare Event and Turn LED On</title><link>https://devzone.nordicsemi.com/thread/158073?ContentTypeID=1</link><pubDate>Mon, 19 Nov 2018 21:05:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7288d7a0-5795-4559-a4a6-11c0d815aa35</guid><dc:creator>mbards</dc:creator><description>&lt;p&gt;Thank you for your answer. Very helpful.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I got the GPIOTE + PPI working, but I&amp;#39;m having trouble with the interrupt example. It doesn&amp;#39;t look like my TIMER0_IRQHandler function is ever entered (I put a break point on line 45 and debugged. Nothing). I set TIMER_ENABLED to 0 in sdk_config because I was getting a &amp;#39;multiple definition of `TIMER0_IRQHandler&amp;#39; error when TIMER_ENABLED was set to 1.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/** @file
* @brief Example template project.
* @defgroup nrf_templates_example Example Template
*
* NOTE: To get build to work, I right clicked nRF_micro-ecc folder and excluded it from build
*/

#include &amp;quot;main.h&amp;quot;

#define TIMER0_BASE 0x40008000UL // TIMER0 - 0x40008000UL
NRF_TIMER_Type* TIMER0 = (NRF_TIMER_Type*) TIMER0_BASE; // NRF_TIMER_Type has register structure
#define GPIOTE_BASE 0x40006000UL
NRF_GPIOTE_Type* GPIOTE = (NRF_GPIOTE_Type*) GPIOTE_BASE; // NRF_GPIOTE_Type has register structure

void timer_init(NRF_TIMER_Type* timer) {
    /*https://devzone.nordicsemi.com/f/nordic-q-a/20765/multiple-definition-of-spi1_twi1_irqhandler/81078#
      Setup timer for PWM
    */

    timer -&amp;gt; MODE &amp;amp;= 0; // Timer mode
    timer -&amp;gt; BITMODE |= 0x0; // 0x1: Overflows after 16 bits
    timer -&amp;gt; CC[0] |= 0x2710; // Set value in CC[0] to 10000
    timer -&amp;gt; PRESCALER |= 0x9; // Counts at 31.250 kHz (!! being set to d??)
    timer -&amp;gt; SHORTS |= 0x1; // On compare[0] event, CLEAR task autofires
    timer -&amp;gt; INTENSET |= (0x1 &amp;lt;&amp;lt; 16); // Enable interrupt in register for Compare[0] event

    NVIC_EnableIRQ(TIMER0_IRQn); // Enables interrupt in NVIC interrupt controllers

    timer -&amp;gt; TASKS_START = 1;
}

void timer_pin_ppi(NRF_GPIOTE_Type* gpiote) {
    // MODE: task, PSEL: 0, POLARITY: Toggle, OUTINIT: High
    gpiote -&amp;gt; CONFIG[0] = (0x3 &amp;lt;&amp;lt; 0) |
                          (LED_1 &amp;lt;&amp;lt; 8) |
                          (0x3 &amp;lt;&amp;lt; 16)|
                          (0x1 &amp;lt;&amp;lt; 20);    

    NRF_PPI -&amp;gt; CH[0].EEP = (uint32_t) &amp;amp;TIMER0 -&amp;gt; EVENTS_COMPARE[0]; // Connect TIMER0 event to PPI channel 0
    NRF_PPI -&amp;gt; CH[0].TEP = (uint32_t) &amp;amp;gpiote -&amp;gt; TASKS_OUT[0]; // Connect GPIOTE task to PPI channel 0
    NRF_PPI -&amp;gt; CHEN = (0x1 &amp;lt;&amp;lt; 0); // Enable PPI channel 0
}

void TIMER0_IRQHandler(void) {
    if ((TIMER0 -&amp;gt; EVENTS_COMPARE[0] != 0) &amp;amp;&amp;amp; (TIMER0 -&amp;gt; INTENSET &amp;amp; 0xF000 != 0)) {
        nrf_gpio_pin_toggle(LED_1);
        TIMER0 -&amp;gt; EVENTS_COMPARE[0] = 0; // clear Compare[0]
        TIMER0 -&amp;gt; TASKS_START = 1;
    }
}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    timer_init(TIMER0); // Setup Timer
//    timer_pin_ppi(GPIOTE); // Setup PPI for timer/LEDs

    while(true) {
        
    }
}
/** @} */
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read Generated Compare Event and Turn LED On</title><link>https://devzone.nordicsemi.com/thread/157920?ContentTypeID=1</link><pubDate>Mon, 19 Nov 2018 11:30:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1a7c4e7-9b92-4c91-b91b-32c71e2150bd</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;quot;TIMER0 -&amp;gt; TASKS_START;&amp;quot; should be &amp;quot;&amp;nbsp;TIMER0 -&amp;gt; TASKS_START = 1;&amp;quot;, same goes for TASKS_STOP.&lt;/p&gt;
&lt;p&gt;Since you mention that you want to do this as bare metal as possible, I took the liberty of removing BSP from your main.c, and using the correct defines/bitfields:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void timerinit(NRF_TIMER_Type* timer) {
    /*
      Setup timer for PWM
    */
    timer-&amp;gt;MODE= TIMER_MODE_MODE_Timer &amp;lt;&amp;lt; TIMER_MODE_MODE_Pos; // Timer mode
    timer -&amp;gt; BITMODE = TIMER_BITMODE_BITMODE_32Bit &amp;lt;&amp;lt; TIMER_BITMODE_BITMODE_Pos;
    //timer -&amp;gt; CC[0] |= 0x0A; // Set value in CC[0] to 10
    timer -&amp;gt; CC[0] = 500000; // Set value in CC[0] to 500 ms
    timer -&amp;gt; PRESCALER = 4 &amp;lt;&amp;lt; TIMER_PRESCALER_PRESCALER_Pos; // Counts at 1 MHz
    timer-&amp;gt;SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE0_CLEAR_Pos;
}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    nrf_gpio_cfg_output(LED_1);
    timerinit(NRF_TIMER0);
    NRF_TIMER0-&amp;gt;TASKS_START = 1;
    while(true) {
        if (NRF_TIMER0-&amp;gt;EVENTS_COMPARE[0]) {
            // Clear event
            NRF_TIMER0-&amp;gt;EVENTS_COMPARE[0] = 0;
            nrf_gpio_pin_toggle(LED_1);
        }
    }
}&lt;/pre&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This is a polling type of application. If you want to go interrupt driven, there&amp;#39;s an example here you can borrow some lines from:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/NordicPlayground/nrf51-TIMER-examples/blob/master/timer_example_timer_mode/main.c"&gt;https://github.com/NordicPlayground/nrf51-TIMER-examples/blob/master/timer_example_timer_mode/main.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you want to move into using GPIOTE+PPI, you can look at this example that outputs a clock pulse:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/NordicPlayground/nrf51-8-mhz-gpio-clock/blob/master/main.c"&gt;https://github.com/NordicPlayground/nrf51-8-mhz-gpio-clock/blob/master/main.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>