<?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 execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/12230/how-to-execute-the-timer-interrupt-during-button-press-release-event</link><description>Hello everyone, 
 I am trying to implement the button /timer interrupt handler function 
 Running Functionalities, 
 
 
 button(1) is pressed for 3 sec and button handler interrupt executes the Timer 3sec_handler fucntion where inside the timer handler</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 01 Mar 2016 14:27:08 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/12230/how-to-execute-the-timer-interrupt-during-button-press-release-event" /><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46284?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 14:27:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:176633bb-4de7-41e1-ad6a-f7a5facbd001</guid><dc:creator>Faizan</dc:creator><description>&lt;p&gt;I&amp;#39;m happy that it worked for you.
Third variable is used to avoid reinitialisation of the code, you can go through the code, i have mentioned in comment. How the third variable is being used.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46283?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 14:05:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c59f5668-a953-42ec-ad40-27c2879c5ca4</guid><dc:creator>MARTIN FR</dc:creator><description>&lt;p&gt;Thank you so much its worked but i am not able  understand the concept  could you please describe me in detail about  this third variable functionality&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46282?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 12:33:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:976da40a-7499-4bcb-88ca-f8f01f058aca</guid><dc:creator>Faizan</dc:creator><description>&lt;p&gt;Sorry I don&amp;#39;t know how to upload / attach a document, so i pasted the code&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46281?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 12:30:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c81f68ea-8f04-4ab0-a67f-d83a3f4ce8f5</guid><dc:creator>Faizan</dc:creator><description>&lt;p&gt;/* Please try this hope it should work */&lt;br /&gt;
#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;app_button.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;app_pwm.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;app_fifo.h&amp;quot;
#include &amp;quot;app_trace.h&amp;quot;
#include &amp;quot;app_uart.h&amp;quot;
#include &amp;quot;nrf_drv_gpiote.h&amp;quot;
#define TIMEOUT1 3000    //3s
#define TIMEOUT2 5000    //5s&lt;/p&gt;
&lt;p&gt;#define APP_TIMER_PRESCALER             0  // Value of the RTC1 PRESCALER register.
#define APP_TIMER_MAX_TIMERS            4  // Maximum number of simultaneously created timers. // NORDIC One for debounce, one for long press detect
#define APP_TIMER_OP_QUEUE_SIZE         4  // Size of timer operation queues.
#define BUTTON_DEBOUNCE_DELAY		        50 // Delay from a GPIOTE event until a button is reported as pushed.&lt;/p&gt;
&lt;p&gt;#define LONG_BUTTON_PRESS_TIME_MS       5000
#define BUTTON_PRESS_TIME_MS            3000&lt;/p&gt;
&lt;p&gt;#define MS_TO_TICKS(ms)                 (ms*32768/1000)
static app_timer_id_t                   m_timer_3_sec_id;
static app_timer_id_t                   m_timer_5_sec_id;&lt;/p&gt;
&lt;p&gt;#define PWM_OUTPUT1 7
#define PWM_OUTPUT2 6&lt;/p&gt;
&lt;p&gt;#define MAX_TEST_DATA_BYTES     (15U)                /&lt;strong&gt;&amp;lt; max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /&lt;/strong&gt;&amp;lt; UART TX buffer size. */
#define UART_RX_BUF_SIZE 1                           /**&amp;lt; UART RX buffer size. */
APP_PWM_INSTANCE(PWM0,0);                   // Create the instance &amp;quot;PWM1&amp;quot; using TIMER1.&lt;/p&gt;
&lt;p&gt;static volatile bool ready_flag;            // A flag indicating PWM status.
static volatile bool pwm_flag;   			//PWM State indicator; 0 for not-initialised, 1 for initialised&lt;/p&gt;
&lt;p&gt;void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
ready_flag = true;
}
void uart_error_handle(app_uart_evt_t * p_event)
{
if (p_event-&amp;gt;evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(p_event-&amp;gt;data.error_communication);
}
else if (p_event-&amp;gt;evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(p_event-&amp;gt;data.error_code);
}
}
static void PWM_init(void)
{&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;nrf_gpio_pin_set(10);//clear the LED
nrf_gpio_pin_set(6);//clear the LED
printf(&amp;quot; PWM Initialization:\n&amp;quot;);
ret_code_t err_code;

/* 2-channel PWM */
app_pwm_config_t pwm0_cfg = APP_PWM_DEFAULT_CONFIG_2CH(100UL,6,10);

/* Switch the polarity of the second channel. */
pwm0_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;

/* Initialize and enable PWM. */
err_code = app_pwm_init(&amp;amp;PWM0,&amp;amp;pwm0_cfg,NULL);
APP_ERROR_CHECK(err_code);

app_pwm_enable(&amp;amp;PWM0);
app_pwm_channel_duty_set(&amp;amp;PWM0, 0, 50);
nrf_delay_ms(500);
app_pwm_disable(&amp;amp;PWM0);
nrf_drv_gpiote_out_task_disable(6);
nrf_gpio_pin_clear(6); //clear the LED                              
nrf_gpio_pin_clear(10);//clear the LED
app_pwm_uninit(&amp;amp;PWM0);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;static void PWM_uinit(void)
{&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;app_pwm_disable(&amp;amp;PWM0);
nrf_drv_gpiote_out_task_disable(6);
nrf_gpio_pin_clear(6); //clear the LED                              
nrf_gpio_pin_clear(10);//clear the LED
app_pwm_uninit(&amp;amp;PWM0);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}
/*&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Handler to be called when button is pushed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;param[in]   pin_no 			The pin number where the event is genereated&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;param[in]   button_action 	Is the button pushed or released
*/
static void button_handler(uint8_t pin_no, uint8_t button_action)
{
if(button_action == APP_BUTTON_PUSH)
{
switch(pin_no)
{			
case BUTTON_1:
printf(&amp;quot;button is pressed :\n&amp;quot;);    						
app_timer_start(m_timer_3_sec_id, MS_TO_TICKS(3000), NULL); // NORDIC: Start 3 second single shot timer
app_timer_start(m_timer_5_sec_id, MS_TO_TICKS(5000), NULL); // NORDIC: Start 5 second single shot timer&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; 	default:
 		break;
 }                                                          
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; }
}&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;/**&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Initialize the clock.
*/
void init_clock()
{&lt;/p&gt;
&lt;p&gt; printf(&amp;quot; CLOCK initialization:\n&amp;quot;);
NRF_CLOCK-&amp;gt;LFCLKSRC            = (CLOCK_LFCLKSRC_SRC_Xtal &amp;lt;&amp;lt; CLOCK_LFCLKSRC_SRC_Pos);
NRF_CLOCK-&amp;gt;EVENTS_LFCLKSTARTED = 0;
NRF_CLOCK-&amp;gt;TASKS_LFCLKSTART    = 1;
while (NRF_CLOCK-&amp;gt;EVENTS_LFCLKSTARTED == 0); // Wait for clock to start
}	&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;/**&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Initialize all four LEDs on the nRF51 DK.
*/
void init_leds()
{
printf(&amp;quot;LED initialization:\n&amp;quot;);
nrf_gpio_range_cfg_input(BUTTON_1, BUTTON_2, NRF_GPIO_PIN_PULLUP);//
nrf_gpio_range_cfg_output(LED_3, LED_4);&lt;/p&gt;
&lt;p&gt; nrf_gpio_pin_set(LED_4);//clear the LED&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;}	&lt;/p&gt;
&lt;p&gt;/*&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Handler to be called on single shot timer event.
*/
static void timer_handler_3_sec(void * p_context)
{&lt;/p&gt;
&lt;p&gt; if(!nrf_gpio_pin_read(BUTTON_1))
{
if(0 == PWM_flag)
{	
PWM_init();
printf(&amp;quot;Button is pressed for 3 seconds:\n&amp;quot;);
nrf_gpio_pin_clear(LED_3); //clear the LED
PWM_flag = 1;
}
else
{
//Will come in this loop, when ever the PWM is initialise and the button is pressed for 5 second or more
printf(&amp;quot;PWM is already initialised\n&amp;quot;);
}&lt;/p&gt;
&lt;p&gt; }&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;}
/*&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Handler to be called on single shot timer event.
*/
static void timer_handler_5_sec(void * p_context)
{
if(!nrf_gpio_pin_read(BUTTON_1))
{&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; if(1 == PWM_flag)
 	{
 		PWM_uinit();
 		printf(&amp;quot;Button is pressed for 5 seconds:\n&amp;quot;);
 		nrf_gpio_pin_set(LED_3); //clear the LED  
 		PWM_flag = 0;
 	}
 else
 	{
 		//Never expected to come in this section
 		printf(&amp;quot;PWM is already uninitialised\n&amp;quot;);
 	}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; }
}&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;/**@brief Function for initializing the button handler module.
*/
static void buttons_init(void)
{&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;printf(&amp;quot;BUTTON initialization:\n&amp;quot;);
uint32_t err_code;
// Button configuration structure.
static app_button_cfg_t p_button[] = {  {BUTTON_1, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler},
	{BUTTON_2, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler}};

// Macro for initializing the application timer module.
// It will handle dimensioning and allocation of the memory buffer required by the timer, making sure that the buffer is correctly aligned. It will also connect the timer module to the scheduler (if specified).
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL);    
app_timer_create(&amp;amp;m_timer_3_sec_id, APP_TIMER_MODE_SINGLE_SHOT, timer_handler_3_sec); // NORDIC Create single shot timer triggering interrupt handler timer_handler_3_sec once
app_timer_create(&amp;amp;m_timer_5_sec_id, APP_TIMER_MODE_SINGLE_SHOT, timer_handler_5_sec); // NORDIC Create single shot timer triggering interrupt handler timer_handler_3_sec once


// Initializing the buttons.
err_code = app_button_init(p_button, sizeof(p_button) / sizeof(p_button[0]), BUTTON_DEBOUNCE_DELAY);
APP_ERROR_CHECK(err_code);

// Enabling the buttons.										
err_code = app_button_enable();
APP_ERROR_CHECK(err_code);
while(true)
{
	// Do nothing.
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;/**&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Function for application main entry.
*/
int main(void)
{	uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_ENABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud38400
};&lt;/p&gt;
&lt;p&gt; APP_UART_FIFO_INIT(&amp;amp;comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_error_handle,
APP_IRQ_PRIORITY_LOW,
err_code);&lt;/p&gt;
&lt;p&gt; APP_ERROR_CHECK(err_code);
printf(&amp;quot;initialization all functions :\n&amp;quot;);
nrf_gpio_pin_set(LED_3); //clear the LED&lt;br /&gt;
init_leds();
init_clock();
buttons_init();&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46280?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 12:02:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:01c54757-d505-4dbd-8177-1983d0d5a9f3</guid><dc:creator>MARTIN FR</dc:creator><description>&lt;p&gt;I have uploaded my code please have a look and let me know am using sdk 9 without softdevice&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46279?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 11:41:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4ae76b8-c1af-4a0e-9ce9-4bbf7bb035cd</guid><dc:creator>Faizan</dc:creator><description>&lt;p&gt;the third variable x will block reinitialisation of the PWM if it is already initialised.  or else you can share the code and i can modify it for u or u can mail me at zfaizanz@gmail.com&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46278?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 11:20:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:61e4aa6b-6b19-4522-bc67-f6038af8f99b</guid><dc:creator>MARTIN FR</dc:creator><description>&lt;p&gt;Thank you for your reply actually pm pulses are getting started because i am calling the PWM_init(); in the timer _3sec_handler function so the pwm pulses are getting generated and then where int eh 5sec_timer handler am calling the pwm_unit(); so it has to stop the PWM moreover i didnot find any relationship between another variable X and the pwm_init(); so how could be possible for the third variable to control the PWM function if you need i can upload the code&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46277?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 11:08:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4c5ce526-94eb-4da0-ad2b-12784f9fd659</guid><dc:creator>Faizan</dc:creator><description>&lt;p&gt;&lt;strong&gt;Try this Below is the pseudo code, this should help you; In your case the Button pressed condition for 3 secs will be true in both 3 seconds and 5 seconds situation, So here in below code i&amp;#39;m trying to avoid the reinitialization of PWM, by using one extra variable&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; variable x = 0; // Initially its zero; Indication PWM is not initialised.

if (button_pressed == 3second)
	{
//Button is pressed for 3 second
		if(x != 1)
			{
	
			//Since X is not equal to 1
			//Initialise PWM and start generating PWM
	
				Start PWM generation;

			//Set x = 1; indicating PWM has been initialised

				set x = 1;
			}	
	}

if (button_pressed == 5second)
	{
//button is pressed for 5second

		if(x == 1)
			{
			//Since x ==1
			//Indicating PWM is being generated and is initialised
			//Stop PWM generation

				Stop PWM generation;
			
			//Set x = 0;  to indicate PWM  generation has been stopped
				set x = 0;
			}
	}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46276?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 10:28:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:308507d7-2260-489d-ba8e-1b2a5f03046d</guid><dc:creator>MARTIN FR</dc:creator><description>&lt;p&gt;Thank you for your reply(faizan) Actually my aim to start a pwm pulses only if i press the button for 3 seconds and stop for 5sec button press moreover i will implement additonal functionalities for 3sec button press ( when button is pressed for 3sec start pwm pulses and aslo start advertising ) so i cannot able to reciprocate the idea if possible could you suggest me some other ideas to execute the same function&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46275?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 10:26:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:38f971fc-d421-4025-afd8-7d75b7afbfc5</guid><dc:creator>karan</dc:creator><description>&lt;p&gt;Hello prabhu durai could you try this way call the pwm_init (); in the button release vent check the 3sec_timerat the button release event&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to execute the timer interrupt during button press/release event</title><link>https://devzone.nordicsemi.com/thread/46274?ContentTypeID=1</link><pubDate>Tue, 01 Mar 2016 10:18:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3b5eb161-102f-4d50-86e9-cf69eb7af639</guid><dc:creator>Faizan</dc:creator><description>&lt;p&gt;Just an Idea try it should work:&lt;/p&gt;
&lt;p&gt;just reverse the functionality of button i.e.,&lt;/p&gt;
&lt;p&gt;It should start generating pulses when holding button pressed for 5 Secs and  Stop generating pulses when holding button pressed  for 3 sec.&lt;/p&gt;
&lt;p&gt;or the otherway:&lt;/p&gt;
&lt;p&gt;You can check the status of PWM, If PWM is running the init function used for initialization should be skipped ( if...else).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>