以下是proteus仿真图,看起来电没从ULN2003输出的感觉
以下是主要的代码,挺简单的逻辑
- void step(int dir)
- {
- if(dir == 0) // 正转
- {
- GPIO_SetBits(GPIOB, IN1_PIN);
- GPIO_ResetBits(GPIOB, IN2_PIN);
- GPIO_ResetBits(GPIOB, IN3_PIN);
- GPIO_ResetBits(GPIOB, IN4_PIN);
- delay_ms(5);
-
- GPIO_ResetBits(GPIOB, IN1_PIN);
- GPIO_SetBits(GPIOB, IN2_PIN);
- GPIO_ResetBits(GPIOB, IN3_PIN);
- GPIO_ResetBits(GPIOB, IN4_PIN);
- delay_ms(5);
-
- GPIO_ResetBits(GPIOB, IN1_PIN);
- GPIO_ResetBits(GPIOB, IN2_PIN);
- GPIO_SetBits(GPIOB, IN3_PIN);
- GPIO_ResetBits(GPIOB, IN4_PIN);
- delay_ms(5);
-
- GPIO_ResetBits(GPIOB, IN1_PIN);
- GPIO_ResetBits(GPIOB, IN2_PIN);
- GPIO_ResetBits(GPIOB, IN3_PIN);
- GPIO_SetBits(GPIOB, IN4_PIN);
- delay_ms(5);
- }
- else if(dir == 1) // 反转
- {
- GPIO_ResetBits(GPIOB, IN1_PIN);
- GPIO_ResetBits(GPIOB, IN2_PIN);
- GPIO_ResetBits(GPIOB, IN3_PIN);
- GPIO_SetBits(GPIOB, IN4_PIN);
- delay_ms(5);
-
- GPIO_ResetBits(GPIOB, IN1_PIN);
- GPIO_ResetBits(GPIOB, IN2_PIN);
- GPIO_SetBits(GPIOB, IN3_PIN);
- GPIO_ResetBits(GPIOB, IN4_PIN);
- delay_ms(5);
- GPIO_ResetBits(GPIOB, IN1_PIN);
- GPIO_SetBits(GPIOB, IN2_PIN);
- GPIO_ResetBits(GPIOB, IN3_PIN);
- GPIO_ResetBits(GPIOB, IN4_PIN);
- delay_ms(5);
- GPIO_SetBits(GPIOB, IN1_PIN);
- GPIO_ResetBits(GPIOB, IN2_PIN);
- GPIO_ResetBits(GPIOB, IN3_PIN);
- GPIO_ResetBits(GPIOB, IN4_PIN);
- delay_ms(5);
- }
- }
- void StepMotor()
- {
- SystemInit();
- StepMotor_Init();
- step(0); // 正转
- step(0);
- step(0);
- step(0);
- delay_ms(500);
- step(1); // 反转
- step(1);
- step(1);
- step(1);
- delay_ms(500);
- }
|