CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Help with doing tasks when a button is pushed

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

Help with doing tasks when a button is pushed
PostPosted: Thu Sep 21, 2017 6:18 am     Reply with quote

I want to use pic16f1939 to perform 6 different tasks with the push of a button. Everything is performed in a same subroutine.
I want to do something like this:
Code:

if (condition == true)
{
     task 1
      {
       }
      wait for a push of button
      task 2
       {
        }
       wait for a push of a button
       task 3
        {
         }

        same way up to task 6
         & then restart

}

I have tried to execute it using sleep but when i am using ext_interrupt to wake pic i don't know how would i direct it to perform task 2,3,---- up to task 6. I used goto command but it is not allowing to jump to a label in subroutine.

I did some research and observed that we should avoid using Goto();

Any clue how would i perform that. I am tight on memory part so want to use as less memory as i can to perform this task.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Sep 21, 2017 6:27 am     Reply with quote

Is power critical?.

This is the only reason you'd want to use sleep.

The point about sleep is it allows you to turn off the CPU, and then have it come awake when wanted.

If you are using a button to wake up, the you need hardware debouncing on the button.

With hardware debouncing, you can have a subroutine that waits using sleep like:
Code:

void wait _for_button(void)
{
    //assuming the button is on INT_EXT
    clear_interrupt(INT_EXT);
    disable_interrupts(GLOBAL);
    enable_interrupts(INT_EXT);
    sleep();
    delay_cycles(1);
}

Which can be called where you show 'wait for push of button', and will come back when INT_EXT triggers (assuming no other interrupts are enabled).
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

PostPosted: Thu Sep 21, 2017 6:47 am     Reply with quote

No, power is not critical. I am also using timer1 interrupt. is it safe to use this with timer-1 interrupt.

I thought sleep because i want to hold the process at that line until i push a button & resumes back once i push a button.

Which is the best feasible solution ? This is for initial testing & setup and will be only used once in whole life of a pic in my application.
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Thu Sep 21, 2017 8:08 am     Reply with quote

Code:
#case

#define TASK_NONE 0
#define TASK_1 1
#define TASK_2 2
#define TASK_3 3
// etc, and...
#define TASK_END 4 // make this one greater than your last valid task

unsigned int8 task_FSM; // FSM = finite state machine

// do your processor initialization
task_FSM = TASK_NONE;

while (TRUE) {
   while (input(BUTTON)); // I'm assuming that pressing your button results
   // in a logic LOW input to the processor...this will cause the program
   // to wait until it is pressed
   switch (task_FSM) {
      case TASK_NONE:
         task_FSM++;
      case TASK_1:
         // do task 1 here
         break;
      case TASK_2:
         // do task 2 here
         break;
      case TASK_3:
         // do task 3 here
         break;
   }
   if (++task_FSM == TASK_END) {
      task_FSM = TASK_NONE;
   }
}
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

PostPosted: Thu Sep 21, 2017 8:16 am     Reply with quote

Got it, thanks a lot for the suggestion guys.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group