I'm programming a stepper motor with PIC16F84 in MPlab IDE. My program returns it's starting point after I call it's delay method. To be more spesific, some of code snippets here.
main method to drive program
int main(int argc, char** argv) {
TRISB = 0; // PORT B as output port
PORTB = 0x0F;
stepForward(25);
activateRelay();
waitForSeconds(3000);
deActivateRelay();
stepBackward(50);
//Since step forward method steps for 100, this will return to initial state
stepForward(25);
return (EXIT_SUCCESS);
}
Step forward method
void stepForward(unsigned int stepCount){
while(0 < stepCount) {
PORTB = 0b00000001;
waitForSeconds(500);
PORTB = 0b00000010;
waitForSeconds(500);
PORTB = 0b00000100;
waitForSeconds(500);
PORTB = 0b00001000;
waitForSeconds(500);
stepCount--;
}
}
And the method for delaying system
void waitForSeconds(unsigned int miliSeconds){
//DelayUs(miliSeconds);
for(;miliSeconds > 0; miliSeconds--)
for(unsigned short x = 333; x > 0 ; x--){
}
}
After the second waitFor
method called from stepForward
method, program returns into TRISB = 0;
part of the main
method.
I'm new at pic programming, so my fault would be very easy one. I'm looking for help. Thanks.
waitFor
method called fromstepForward
", do you mean that the secondwaitForSeconds
completes successfully (and thus you see all the outputs go high 500ms after the PORTB1 goes high)? – slugonamissionINTCON
can be set by the device programmer, which could enabled interrupts. Since there isn't an ISR, this could potentially cause a crash as you describe. As a sanity check, addINTCON = 0;
at the top of your program. – slugonamissionPORTB = 0b00000100;
is leading the CPU to a error handler state. – Felipe Lavratti