舵机驱动-GPIO MG995 STM32

应为TIM都用光了,只能使用GPIO,使用GPIO 好处就是很好移植。程序如下:

省略GPIO等初始化。PIN设置为

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;              
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

#ifndef _MOTOR_STEP_H_
#define _MOTOR_STEP_H_
#define MIN_DEGREE (0)
#define MAX_DEGREE (90)

#include "stm32f10x.h"

s8 MotorMoveDegree(u8 degree);

#endif
#include "motor_step.h"
#include "ioctrl.h"
#include "base_conf.h"
#define US_NUM (445)
static void Delay_100us(u16 us)//only for sysclock is 36M, Measured by OscillOscope,not a accurate delay
{
    u16 i=US_NUM;
    while(us--)
    {
        while(i--)
        __NOP();
        i=US_NUM;
    }
}

s8 MotorMoveDegree(u8 degree)
{
    u16 ON_us,OFF_us;
      u8 i=15;
        if(degree>MAX_DEGREE)return -1;
      ON_us = (u16)(25*degree/MAX_DEGREE);
      OFF_us = 200-ON_us;
      while(i--)
        {
            MotorStepSignal_ON();
            Delay_100us(ON_us);
            MotorStepSignal_OFF();
            Delay_100us(OFF_us);
        }
      return 0;    
}
void main()
{
 //system_init();
     while(1)
    {
           MotorMoveDegree(90);
        Delay_ms(100);
        MotorMoveDegree(60);
        Delay_ms(100);

    }
}

 

posted @ 2018-06-01 17:03  =天赋=  阅读(644)  评论(0编辑  收藏  举报