主控: 雅特力AT32F403A, 主频100Mhz
驱动: GPIO口简单驱动
先展示主要的桥电路
头文件
/***************************************************************************
* Copyright notice & Disclaimer
*
**************************************************************************
* @file 文件名: motor.h
* @date 日 期: 2023-11-23
* @version 版本号: v1.0
* @auther 作 者: 吴川流
**************************************************************************
* @brief 简 述:
* 电机控制文件
*************************************************************************/
#ifndef _MOTOR_H__
#define _MOTOR_H__
#include "../main.h"
#define PWM_N_LBW PDOut(8)
#define PWM_E_LBW PBOut(15)
typedef enum
{
MOTOR_STOP,
MOTOR_ZZ, //正转
MOTOR_FZ, //反转
} motor_state;
typedef struct {
__IO motor_state nState; //需要进入的状态
motor_state cState; //当前的状态
__IO uint8_t motorWorkFg : 1; //电机工作状态
uint8_t mStateSW : 1; //电机状态切换
uint8_t mStopping : 1; //电机停止状态中
} motor_type;
//电机速度匹配的PWM百分比
extern __IO uint8_t PwmDutyPre;
extern motor_type motorType;
/*
* 走丝电机使能并设置转动方向
* */
static inline void MotorEnaAndSetDir(motor_state mstate)
{
motorType.motorWorkFg = 1;
motorType.nState = mstate;
}
/*
* 电机停止
* */
static inline void MotorDis(void)
{
motorType.motorWorkFg = 0;
motorType.nState = MOTOR_STOP;
}
/*
* 送丝正转
* 参数传入正转占空比百分比
* */
#define MOTOR_ZZ_RUN_() do { \
__NOP(); \
__NOP(); \
pwmn_duty_set_(0); \
sys_nopLoop(16); \
__NOP(); \
__NOP(); \
PWM_E_LBW = 0; \
sys_nopLoop(25); \
pwme_duty_set_(PwmDutyPre); \
__NOP(); \
__NOP(); \
PWM_N_LBW = 1; \
__NOP(); \
__NOP(); \
}while(0)
/*
* 退丝反转
* 参数传入正转占空比百分比
* */
#define MOTOR_FZ_RUN_() do { \
__NOP(); \
__NOP(); \
pwme_duty_set_(0); \
sys_nopLoop(16);\
__NOP(); \
__NOP(); \
PWM_N_LBW = 0; \
sys_nopLoop(25); \
pwmn_duty_set_(PwmDutyPre); \
__NOP(); \
__NOP(); \
PWM_E_LBW = 1; \
__NOP(); \
__NOP(); \
} while(0)
/*
* 电机刹车
* */
#define MOTOR_BRAKE_() do \
{ \
__NOP(); \
__NOP(); \
pwmn_duty_set_(0); \
sys_nopLoop(10); \
pwme_duty_set_(0); \
sys_nopLoop(10); \
PWM_N_LBW = 1; \
__NOP(); \
__NOP(); \
PWM_E_LBW = 1; \
__NOP(); \
__NOP(); \
} while(0)
/*
* 电机停止
* */
#define MOTOR_STOP_() do \
{ \
__NOP(); \
__NOP(); \
pwmn_duty_set_(0); \
sys_nopLoop(10); \
pwme_duty_set_(0); \
sys_nopLoop(10); \
PWM_E_LBW = 0; \
__NOP(); \
__NOP(); \
PWM_N_LBW = 0; \
__NOP(); \
__NOP(); \
} while(0)
#endif //_MOTOR_H__
C代码
#include "motor.h"
__IO uint8_t PwmDutyPre;
motor_type motorType = {
.cState = MOTOR_STOP,
.nState = MOTOR_STOP,
.motorWorkFg = 0,
.mStateSW = 0,
.mStopping = 0
};
/*
* 电机驱动控制函数
* */
void motorController(void)
{
static uint8_t gapTime = 0;
static uint8_t stopStep = 0;
if (motorType.motorWorkFg && (!motorType.mStopping))
{
if ((motorType.nState == MOTOR_ZZ) && (!motorType.mStateSW))
{
//电机正转
stopStep = 0;
if (motorType.cState == MOTOR_STOP)
{
MOTOR_ZZ_RUN_();
motorType.cState = MOTOR_ZZ;
}
else if (motorType.cState == MOTOR_FZ)
{
motorType.mStateSW = 1;
goto motor_dis_gt;
}
}
else if ((motorType.nState == MOTOR_FZ) && (!motorType.mStateSW))
{
//电机反转
stopStep = 0;
if (motorType.cState == MOTOR_STOP)
{
MOTOR_FZ_RUN_();
motorType.cState = MOTOR_FZ;
}
else if (motorType.cState == MOTOR_ZZ)
{
motorType.mStateSW = 1;
goto motor_dis_gt;
}
}
else
goto motor_dis_gt;
}
else
{
motor_dis_gt:
if (stopStep == 0)
{
motorType.mStopping = 1;
gapTime = 0;
MOTOR_BRAKE_();
stopStep = 1;
}
else if (stopStep == 1)
{
if (gapTime >= 20)
{
MOTOR_STOP_();
stopStep = 2;
}
else
gapTime++;
}
else if (stopStep == 2)
{
gapTime = 0;
motorType.cState = MOTOR_STOP;
motorType.mStopping = 0;
if (!motorType.mStateSW)
motorType.nState = MOTOR_STOP;
else
motorType.mStateSW = 0;
stopStep = 3;
}
}
}