您好!
过去几天、我一直尝试使用 ULN2003驱动器来控制步进电机(BYJ48)、但没有成功、步进电机移动速度非常缓慢、振动回和回波、下面是我非常简单的代码:
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// Stepper Pin 1 = GPIO_PIN3, Pin2 = GPIO_PIN4, etc.
GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN3 | GPIO_PIN4 | GPIO_PIN5 | GPIO_PIN6);
while(1)
{
for(int i = 0; i < 12; i++)
{
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
}
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
for(int j = 0; j < 12; j++)
{
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
}
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN6);
__delay_cycles(1000);
};
// __bis_SR_register(LPM0_bits); // Enter LPM0
}
其中 GPIO_PIN3为 IN1、GPIO_PIN4 = IN2、GPIO_PIN5 = IN3、GPIO_PIN6 = IN4。
我还使用外部电源为电机供电。
我只想让步进电机连续360度旋转。 我正在关注此视频以供参考... https://www.electronicwings.com/ti-launchpad/stepper-motor-interfacing-with-msp-exp430g2-ti-launchpad
它基于 Arduino、但代码是等效的。
非常感谢您的帮助

