Program returns to its main point unexpectedly at Pic C in MPlab

百般思念 提交于 2019-12-25 05:08:51

问题


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:


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.



来源:https://stackoverflow.com/questions/23085280/program-returns-to-its-main-point-unexpectedly-at-pic-c-in-mplab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!