0
votes

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.

1
By "After the second waitFor method called from stepForward", do you mean that the second waitForSeconds completes successfully (and thus you see all the outputs go high 500ms after the PORTB1 goes high)?slugonamission
Also, in MPLAB, have you enabled interrupts? I believe INTCON 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, add INTCON = 0; at the top of your program.slugonamission
Probably it's not completed successfully, because it's not going into 3rd assignment of PORTB.Uğurcan Şengit
I've added INTCON = 0, but it didn't helped at all.Uğurcan Şengit
Probably operation PORTB = 0b00000100; is leading the CPU to a error handler state.Felipe Lavratti

1 Answers

1
votes

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.