This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to execute the timer interrupt during button press/release event

Hello everyone,

I am trying to implement the button /timer interrupt handler function

Running Functionalities,

  1. button(1) is pressed for 3 sec and button handler interrupt executes the Timer 3sec_handler fucntion where inside the timer handler am calling the PWM_init(); so pwm pulses are getting generated

  2. button(1) is pressed for 5 sec and then button handler interrupt executes the Timer 5sec_handler fucntion where inside the timer 5sec handler am calling the PWM_uinit(); so pwm pulses are getting stopped

function (1) is executing very well but the real problem is when i pressing the button for 5 sec then the timer _3sec handler function also getting executed and reinitialize pwm_init () while timer crossing the 3sec PWM pulses are again generated and sooner timer reaches the 5sec stops the PWM

i wanted to make pwm pulses when i press the button for 3 sec and stops the pwm pulses pressing the buton for 5sec without regenerating pwm pulses so could anyone suggest me how to overcome this issue and suggest me what are best possible logics to implement the proper button and timer handler interrupt to start and stop pwm pulses

Note. same button is used for both functionalities but if i use two buttons then they are worjing very well but trying to use only one button for both functions any ideas or suggestions would be really helpful for me

Thank you

  • Just an Idea try it should work:

    just reverse the functionality of button i.e.,

    It should start generating pulses when holding button pressed for 5 Secs and Stop generating pulses when holding button pressed for 3 sec.

    or the otherway:

    You can check the status of PWM, If PWM is running the init function used for initialization should be skipped ( if...else).

  • 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

  • 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

  • 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'm trying to avoid the reinitialization of PWM, by using one extra variable

     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;
    			}
    	}
    
  • 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

Related