我在MPlab IDE中使用PIC16F84编程步进电机。在我称之为延迟方法后,我的程序将返回它的起点。为了更具特色,这里有一些代码片段。
驱动程序的主要方法
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);
}
前进方法
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--;
}
}
延迟系统的方法
void waitForSeconds(unsigned int miliSeconds){
//DelayUs(miliSeconds);
for(;miliSeconds > 0; miliSeconds--)
for(unsigned short x = 333; x > 0 ; x--){
}
}
从waitFor
方法调用第二个stepForward
方法后,程序将返回TRISB = 0;
方法的main
部分。
我是pic编程的新手,所以我的错很简单。我正在寻求帮助。 感谢。
答案 0 :(得分:1)
如果程序计数器意外跳回0,则PIC正在复位。复位的原因有很多,具体取决于PIC。一个常见的是看门狗超时,你似乎没有踢看门狗,所以你在配置位中禁用它吗?状态寄存器位4将告诉您是否发生了看门狗超时。