新闻中心

EEPW首页>嵌入式系统>设计应用> STM32用IO口控制步进电机的简单程序

STM32用IO口控制步进电机的简单程序

作者: 时间:2016-11-21 来源:网络 收藏
练习IO口库函数操作。
  1. //相序
  2. uint16_tphasecw[4]={0x2000,0x0001,0x0004,0x0008};//D-C-B-A
  3. uint16_tphaseccw[4]={0x0008,0x0004,0x0001,0x2000};//A-B-C-D

本文引用地址://m.amcfsurvey.com/article/201611/319100.htm
  1. //步进电机相关IO口初始化
  2. //IN4:PC13
  3. //IN3:PC0
  4. //IN2:PC2
  5. //IN1:PC3
  6. voidMoto_Init(void)
  7. {
  8. GPIO_InitTypeDefGPIO_InitStructure;
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//GPIOCLOCKENABLE
  10. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
  11. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
  12. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//50MHz速率
  13. GPIO_Init(GPIOC,&GPIO_InitStructure);
  14. GPIO_ResetBits(GPIOC,GPIO_Pin_13);//输出低电平
  15. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
  16. GPIO_Init(GPIOC,&GPIO_InitStructure);
  17. GPIO_ResetBits(GPIOC,GPIO_Pin_0);
  18. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
  19. GPIO_Init(GPIOC,&GPIO_InitStructure);
  20. GPIO_ResetBits(GPIOC,GPIO_Pin_2);
  21. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
  22. GPIO_Init(GPIOC,&GPIO_InitStructure);
  23. GPIO_ResetBits(GPIOC,GPIO_Pin_3);
  24. }

  1. //电机正转
  2. voidMotorcw(void)
  3. {
  4. uint8_ti;
  5. for(i=0;i<4;i++)
  6. {
  7. GPIO_Write(GPIOC,phasecw[i]);
  8. delay_ms(4);
  9. }
  10. }
  11. //电机反转
  12. voidMotorccw(void)
  13. {
  14. uint8_ti;
  15. for(i=0;i<4;i++)
  16. {
  17. GPIO_Write(GPIOC,phaseccw[i]);
  18. delay_ms(4);
  19. }
  20. }
  21. //点击停止
  22. voidMotorStop(void)
  23. {
  24. GPIO_Write(GPIOC,0x0000);
  25. }


记得使能IO口时钟。



关键词:STM32IO口步进电

评论


技术专区

关闭