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

Parents
  • 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).

  • 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;
    			}
    	}
    
Reply
  • 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;
    			}
    	}
    
Children
No Data
Related