Hi!
I'm working on a school project in C, using a PIC16F877A. Here is the general idea of the code (This is pretty basic stuff, I'm not an advanced C programmer):
Basically, this program performs different actions depending on the current step. Every step there has to be a button1 push to continue to the next step. This part of the code is 100% working as intended.
I want to add another button (button2), that whenever pushed, changes the "step" value to 3. In other words, if you press button2, I want it to skip right to case 3 in the main function. I have no clue as to how I can achieve this. I thought of using an interrupt on change on portB with button2 that would put 3 in the "step" variable (which is a global variable) but the problem is that after the interrupt function is completed, the program would go back to where it was before the interrupt i.e. in the middle of a case. I don't want it to finish the case it was in, I'd like it to jump straight to case 3. How would I go about doing that? I also thought of putting my whole while(1) in a function, then calling this function from within the interrupt function. While this would technically make the program work as I want it to, it kinda feels weird and I'm really not sure if this is a good idea. I mean, the interrupt function would never properly complete itself. Is this dangerous for my PIC ?
Is there a "cleaner" way to do this?
Thanks in advance!
Edit : I'm using MPlab with the CCS compiler in case that matters.
I'm working on a school project in C, using a PIC16F877A. Here is the general idea of the code (This is pretty basic stuff, I'm not an advanced C programmer):
Rich (BB code):
int step=0; //This is global
void main (void){
while(1){
switch(step){
case 0:do stuff
while(button1 not pressed); //Wait for button1 push
do more stuff
step = 1;
break;
case 1:do stuff
while(button1 not pressed); //Wait for button1 push
do more stuff
step = 2;
break;
case 2:do stuff
while(button1 not pressed); //Wait for button1 push
do more stuff
step = 3;
break;
case 3:do stuff
while(button1 not pressed); //Wait for button1 push
do more stuff
step = 0;
break;
default:step = 0;
break;
}
}
}
I want to add another button (button2), that whenever pushed, changes the "step" value to 3. In other words, if you press button2, I want it to skip right to case 3 in the main function. I have no clue as to how I can achieve this. I thought of using an interrupt on change on portB with button2 that would put 3 in the "step" variable (which is a global variable) but the problem is that after the interrupt function is completed, the program would go back to where it was before the interrupt i.e. in the middle of a case. I don't want it to finish the case it was in, I'd like it to jump straight to case 3. How would I go about doing that? I also thought of putting my whole while(1) in a function, then calling this function from within the interrupt function. While this would technically make the program work as I want it to, it kinda feels weird and I'm really not sure if this is a good idea. I mean, the interrupt function would never properly complete itself. Is this dangerous for my PIC ?
Is there a "cleaner" way to do this?
Thanks in advance!
Edit : I'm using MPlab with the CCS compiler in case that matters.
Last edited: