Question

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.

Was it helpful?

Solution

If the program counter jumps back to 0 unexpectedly, the PIC is resetting. There are many causes of reset, depending on the PIC. A common one is watchdog timeout, and you don't seem to be kicking the watchdog, so have you disabled it in the config bits? Status register bit 4 will tell you if a watchdog timeout occurred.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top