基于STM32 HAL库 TB6612模块的直流电机驱动

目录

CubeMX配置

调试配置​

时钟配置

定时器配置

配置GPIO

代码

motor.h

motor.c

配置

调试配置

时钟配置

定时器配置

打开定时器3的PWM生成模式,预分频系数72-1,自动重装载系数1000-1,会生成一个周期为1ms,占空比精度为0.1%的PWM波。

配置GPIO

选择推挽输出,用于确定电机转向。

代码

motor.h

#ifndef __MOTOR_H__
#define __MOTOR_H__

void motor_Control(int a,int speed);
void motor_init();

#endif

motor.c

#include "motor.h"     
#include "tim.h"

void motor_init(){
  HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
  HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);
  HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
}

void motor_Control(int a,int speed){

	if(a==1)
    {
		
		if(speed>=0)
		{
			if(speed>=1000)
            {
			    speed=1000;
			}
		
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
		    __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1,speed);
		
		}
		else
		{	if(speed<=-1000)
            {
			    speed=-1000;
			}
		
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,GPIO_PIN_RESET);
			__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1,-speed);
		}
		
	}
	
	if(a==2)
    {
	    if(speed>=0)
		{
			if(speed>=1000)
            {
			    speed=1000;
			}
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
		    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);
			__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_2,speed);
		
		}
		else
		{	
            if(speed<=-1000)
            {
			    speed=-1000;
			}
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
			__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_2,-speed);
		}
	}
	
	if(a==3)
    {
	    if(speed>=0)
		{
			if(speed>=1000)
            {
			    speed=1000;
			}
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET);
			__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_3,speed);
		
		}
		else
		{	
            if(speed<=-1000)
            {
			    speed=-1000;
		    }
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
			__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_3,-speed);
		}
	}
	
	if(a==4)
    {
	    if(speed>=0)
		{
			if(speed>=1000)
            {
			    speed=1000;
			}
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
			__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_4,speed);
		
		}
		else
		{	
            if(speed<=-1000)
            {
			    speed=-1000;
			}
			
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5 ,GPIO_PIN_RESET);
			__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_4,-speed);
		}
	}
}







  • 62
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用HAL库来驱动STM32F103ZET6微控制器上的TB6612芯片来控制直流电机。下面是一个简单的步骤: 1. 初始化引脚:首先,你需要初始化用于控制TB6612的引脚。根据你的需求,配置GPIO引脚为输出模式,并设置引脚的初始状态。 2. 配置PWM输出:如果你想使用PWM信号来控制电机的速度,你需要配置一个定时器为PWM模式,并将其与相关引脚关联。 3. 编写控制代码:使用HAL库编写代码来控制TB6612芯片。这包括设置引脚状态以启动/停止电机,以及设置PWM信号的占空比以控制速度。 以下是一个示例代码片段,展示了如何使用HAL库来驱动TB6612芯片控制直流电机: ```c #include "stm32f1xx_hal.h" // 定义引脚和定时器 #define MOTOR_A_IN1_Pin GPIO_PIN_0 #define MOTOR_A_IN1_GPIO_Port GPIOA #define MOTOR_A_IN2_Pin GPIO_PIN_1 #define MOTOR_A_IN2_GPIO_Port GPIOA #define MOTOR_A_PWM_Pin GPIO_PIN_8 #define MOTOR_A_PWM_GPIO_Port GPIOB #define MOTOR_A_PWM_TIM TIM4 // 初始化引脚和定时器 void MX_GPIO_Init(void); void MX_TIM4_Init(void); // 启动电机 void motor_start(void) { HAL_GPIO_WritePin(MOTOR_A_IN1_GPIO_Port, MOTOR_A_IN1_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(MOTOR_A_IN2_GPIO_Port, MOTOR_A_IN2_Pin, GPIO_PIN_RESET); HAL_TIM_PWM_Start(&MOTOR_A_PWM_TIM, MOTOR_A_PWM_Pin); } // 停止电机 void motor_stop(void) { HAL_GPIO_WritePin(MOTOR_A_IN1_GPIO_Port, MOTOR_A_IN1_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(MOTOR_A_IN2_GPIO_Port, MOTOR_A_IN2_Pin, GPIO_PIN_RESET); HAL_TIM_PWM_Stop(&MOTOR_A_PWM_TIM, MOTOR_A_PWM_Pin); } // 设置电机速度 void motor_set_speed(uint16_t speed) { __HAL_TIM_SET_COMPARE(&MOTOR_A_PWM_TIM, MOTOR_A_PWM_Pin, speed); } int main(void) { HAL_Init(); MX_GPIO_Init(); MX_TIM4_Init(); while (1) { // 启动电机 motor_start(); // 设置速度为50%占空比 motor_set_speed(500); // 延时2秒 HAL_Delay(2000); // 停止电机 motor_stop(); // 延时2秒 HAL_Delay(2000); } } ``` 请注意,以上代码仅为示例,你需要根据自己的硬件连接和需求进行适当的修改。同时,确保已正确配置时钟和定时器以及在CubeMX中生成代码。 希望对你有所帮助!如有任何疑问,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值