如题,用STM32F103ZET6的战舰的板子通过ULN2003驱动步进电机,只能正转,不能反转,求解。PS:我是通过最简单的IO口电平切换来实现电机驱动,另外想问一下用ULN2003驱动步进电机是不是就不能通过PWM的方式驱动,
硬件连接如下
单片机:STM32F103ZET6
步进电机:28BYJ-48
驱动电路:ULN2003芯片的驱动板
引脚连接如下:
IN1:PC3
IN2:PC2
IN3:PC0
IN4:PC13 OUT1:步进电机4
OUT2:步进电机3
OUT3:步进电机2
OUT4:步进电机1 代码如下: - #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "stm32f10x.h"
- int j;
- //单四拍
- u16 phasecw1[4] ={0x0008,0x0040,0x0001,0x2000};// 逆时针
- u16 phaseccw2[4]={0x2000,0x0001,0x0040,0x0008};// 顺时针
- //步进电机初始化函数
- void Motor_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//开启时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- //默认ULN2003四路输入低电平,输出高电平
- GPIO_ResetBits(GPIOC,GPIO_Pin_13);
- GPIO_ResetBits(GPIOC,GPIO_Pin_0);
- GPIO_ResetBits(GPIOC,GPIO_Pin_2);
- GPIO_ResetBits(GPIOC,GPIO_Pin_3);
- }
-
- ////步进电机正转函数
- void Motorcw1(void)
- {
- {
- uint8_t i;
- for(i=0;i<4;i++)
- {
- GPIO_Write(GPIOC,phasecw1[i]);
- delay_ms(6);
- }
- }
- }
- ////步进电机反转函数
- void Motorcw2(void)
- {
- {
- uint8_t a;
- for(a=0;a<4;a++)
- {
- GPIO_Write(GPIOC,phaseccw2[a]);
- delay_ms(6);
- }
- }
- }
- //电机停止函数
- void MotorStop(void)
- {
- GPIO_Write(GPIOC,0x0000);
- }
- int main(void)
- {
- //函数初始化
- delay_init();
- Motor_Init();
-
- //电机循环执行
- while(1)
- {
- Motorcw1();
- }
- }
复制代码
|