找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2641|回复: 3
收起左侧

STM32控制步进电机

[复制链接]
ID:308810 发表于 2018-5-2 18:52 | 显示全部楼层 |阅读模式
这是我这几天写的,通过stm32来控制步进电机的转动,可以直接引用,注意修改引脚即可:
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"

uint8_t phasecw[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09} ;
uint8_t phaseccw[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08} ;



void delay_ms(uint16_t time)
{   
   uint16_t i=0;  
   while(time--)
   {
      i=3000;  
      while(i--) ;   
   }
}


void Step_Motor_GPIO_Init(void
{
  GPIO_InitTypeDef GPIO_InitStruct;         

        *(unsigned int *)0X40021018 |= (1<<3);
       
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHZ;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(GPIOB,&GPIO_InitStruct);
       
}

void SetMotor(unsigned char InputData
{
    if(InputData & 0x08)
    {
        GPIO_ResetBits(GPIOB,GPIO_Pin_6);   
    }
    else
    {
        GPIO_SetBits(GPIOB,GPIO_Pin_6);
    }
    if(InputData & 0x04)
    {
        GPIO_ResetBits(GPIOB,GPIO_Pin_7);   
    }
   else
    {
        GPIO_SetBits(GPIOB,GPIO_Pin_7);
    }
    if(InputData & 0x02)
    {
        GPIO_ResetBits(GPIOB,GPIO_Pin_8);   
    }
    else
    {
        GPIO_SetBits(GPIOB,GPIO_Pin_8);
    }
   if(InputData & 0x01)
    {
       GPIO_ResetBits(GPIOB,GPIO_Pin_9);   
    }
   else
    {
       GPIO_SetBits(GPIOB, GPIO_Pin_9);
    }
}


void motorNCircle(int n
{
   int i=0;
   int j=0;
   int k=0;
   for(j=0;j<n;j++)
   {
      for(i=0;i<64*8;i++)     
      {
          for(k=0;k<8;k++)
          {
             SetMotor(phaseccw[k]);  
             delay_ms(1);      
          }
      }
    }
}

int main(void)
{
   Step_Motor_GPIO_Init();
         while(1)
  {
                motorNCircle(3);
  }
}




void SystemInit(void)
{
        /*让编译器不报错*/
}


评分

参与人数 1黑币 +5 收起 理由
362618281 + 5 绝世好帖!

查看全部评分

回复

使用道具 举报

ID:319662 发表于 2018-5-2 21:58 | 显示全部楼层
正在学习用32做激光雷达避障小车,学习一下思路哈哈哈
回复

使用道具 举报

ID:260557 发表于 2018-5-8 09:32 | 显示全部楼层
很给力
回复

使用道具 举报

ID:350346 发表于 2018-6-15 18:10 | 显示全部楼层
厉害了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表