View previous topic :: View next topic |
Author |
Message |
POPE19
Joined: 27 Jun 2017 Posts: 71
|
Help with doing tasks when a button is pushed |
Posted: Thu Sep 21, 2017 6:18 am |
|
|
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: 19498
|
|
Posted: Thu Sep 21, 2017 6:27 am |
|
|
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
|
|
Posted: Thu Sep 21, 2017 6:47 am |
|
|
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
|
|
Posted: Thu Sep 21, 2017 8:08 am |
|
|
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
|
|
Posted: Thu Sep 21, 2017 8:16 am |
|
|
Got it, thanks a lot for the suggestion guys. |
|
|
|