黄工无刷电机学习
直播中

那些年儿ing

11年用户 1224经验值
擅长:84784
私信 关注
[问答]

怎么实现直流有刷电机的驱动?

怎么实现直流有刷电机的驱动?

回帖(1)

安瑞娟

2021-10-18 14:22:56
一、前期准备
单片机:STM32F407VET6
开发环境:MDK5.14
库函数:STM32Cube_FW_F4_V1.16.0



二、实验效果
1)按键KEY1按下,正转->反转->停止,循环下去;
2)按键KEY2按下,PWM增加5%,一直加到100%;
3)按键KEY3按下,PWM减小5%,一直减到0%。
4)PWM波为20K,正转启动要到30%,反转启动要到50%,
三、驱动原理
直流有刷电机的驱动十分简单,通电即可转动。运用H桥可以直接驱动电机正反转。





当Q1、Q4导通,电机正转;Q2、Q3导通电机反转。驱动电路使用淘宝上的H桥模块





逻辑输入的IN1、IN2为OUT1与OUT2的控制脚。IN1、IN2电平相反时候,电机实现正反转;IN1、IN2电平相同时,电机停转。
CubeMX TIM2配置如下:






四、驱动代码
motor.h


#ifndef __MOTOR_H__
#define        __MOTOR_H__
#include "stm32f4xx_hal.h"
#include "user_gpio.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"


#define                KEY1        PEin(10)
#define                KEY2        PEin(11)
#define                KEY3        PEin(12)


extern uint8_t PWMCapture1,PWMCapture2;
extern uint8_t MoterMode;


extern void Moter_StartPWM(void);
extern void Motor_Test(void);
#endif


motor.c


#include "motor.h"


extern TIM_HandleTypeDef htim2;
uint8_t PWMCapture1  = 30, PWMCapture2 = 30;
uint8_t MoterMode = 0;


void Moter_StartPWM(void)
{
        HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
        HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
       
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 100);
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 100);
}


void Motor_Test(void)
{
        if (KEY1 == 0)                        //正转
        {
                HAL_Delay(5);
                if (KEY1 == 0)
                {
                        while(KEY1 == 0);
                        MoterMode ++;
                }
        }
       
        switch(MoterMode)
        {
                case 1:                //正转
                        PWMCapture2 = 0;
                        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, PWMCapture2);
                       
                        if (KEY2 == 0)                       
                        {
                                HAL_Delay(5);
                                if (KEY2 == 0)
                                {
                                        while(KEY2 == 0);
                                        PWMCapture1 += 5;
                                        if (PWMCapture1 >= 100)                //Duty Cycle Max = 100%
                                        {
                                                PWMCapture1 = 100;
                                        }
                                        printf("CH1 Duty Cycle = %02d%%rn", PWMCapture1);
                                }
                        }
                       
                        if (KEY3 == 0)                       
                        {
                                HAL_Delay(5);
                                if (KEY3 == 0)
                                {
                                        while(KEY3 == 0);
                                        PWMCapture1 -= 5;
                                        if (PWMCapture1 <= 5)                //Duty Cycle Min = 5%
                                        {
                                                PWMCapture1 = 5;
                                        }
                                        printf("CH1 Duty Cycle = %02d%%rn", PWMCapture1);
                                }
                        }
                       
                        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, PWMCapture1);
                        break;
                case 2:                //反转
                        PWMCapture1 = 0;
                        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, PWMCapture2);
                       
                        if (KEY2 == 0)                       
                        {
                                HAL_Delay(5);
                                if (KEY2 == 0)
                                {
                                        while(KEY2 == 0);
                                        PWMCapture2 += 5;
                                        if (PWMCapture2 >= 100)                //Duty Cycle Max = 100%
                                        {
                                                PWMCapture2 = 100;
                                        }
                                        printf("CH2 Duty Cycle = %02d%%rn", PWMCapture2);
                                }
                        }
                       
                        if (KEY3 == 0)                       
                        {
                                HAL_Delay(5);
                                if (KEY3 == 0)
                                {
                                        while(KEY3 == 0);
                                        PWMCapture2 -= 5;
                                        if (PWMCapture2 <= 5)                //Duty Cycle Min = 5%
                                        {
                                                PWMCapture2 = 5;
                                        }
                                        printf("CH2 Duty Cycle = %02d%%rn", PWMCapture2);
                                }
                        }
                       
                        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, PWMCapture2);
                        break;
                default:
                        MoterMode = 0;
                        break;
        }
}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分