H桥简单24V直流电机GPIO驱动代码

主控: 雅特力AT32F403A, 主频100Mhz

驱动: GPIO口简单驱动

先展示主要的桥电路

头文件

c 复制代码
/***************************************************************************
  *             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代码

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;
        }
    }
}
相关推荐
国产HT1621B7 分钟前
外挂 YL1621 SSOP48 芯片 vs MCU 内置 LCD 控制器深度对比
科技·单片机·嵌入式硬件·mcu·lcd驱动
zlinear数据采集卡12 分钟前
数据采集卡从入门到精通(42):项目实战三——设备预测性维护系统,从布点到预警
开发语言·单片机·嵌入式硬件·安全·fpga开发
嵌入式阿蔡11 小时前
传感器_03:电机驱动——让 GPIO 当指挥官,别让 GPIO 当电源
网络·stm32·单片机·嵌入式硬件·嵌入式实时数据库
国科安芯13 小时前
ASL706S:让 MCU 不再“失忆跑飞“的守护芯片
分布式·单片机·嵌入式硬件·安全·fpga开发·架构
树的枝16 小时前
【STM32】03.中断和EXTI外设
笔记·stm32·单片机·嵌入式硬件
单片机杂货铺17 小时前
【单片机毕业设计选题】基于stm32单片机儿童公路车速度检测报警
stm32·单片机·嵌入式硬件·51单片机·硬件工程·课程设计
振南的单片机世界18 小时前
看门狗硬核拆解:1.6秒不喂狗,硬件强制“掀桌子”
arm开发·stm32·单片机·嵌入式硬件
嵌入式阿蔡19 小时前
Linux_01:交叉编译与开发环境实战——从单片机跨到 Linux 的第一道坎
linux·运维·网络·单片机·嵌入式硬件·嵌入式实时数据库
NYFEA20 小时前
徕飞 FCA系列全面上新,一站式I/O扩展国产替代方案
单片机·嵌入式硬件
国科安芯21 小时前
ASL622S:把“信号失真“挡在门外的航天级运放
网络·单片机·嵌入式硬件·架构·状态模式·低轨卫星星座