GeBalanceBot/Hardware/Firmware/GeBalanceBot_Firmware v1.0/HARDWARE/MOTO/motor.c

99 lines
2.8 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/******************** (C) COPYRIGHT 2024 GeekRebot *****************************
* File Name : moto.c
* Current Version : V1.0 & ST 3.5.0
* Author : zhanli 719901725@qq.com
* Date of Issued : 2023.04.06 zhanli: Create
* Comments : 直流减速电机控制
********************************************************************************/
#include "motor.h"
// PWM在timer3里配置
void Motor_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /*开启GPIO的外设时钟*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; /*选择要控制的GPIO引脚*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*设置引脚模式为通用推挽输出*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*设置引脚速率为50MHz */
GPIO_Init(GPIOB, &GPIO_InitStructure); /*调用库函数初始化GPIO*/
}
//左电机
void Motor_CtlLeft(char state)
{
if(state == GO)//左前电机前进
{
GPIO_SetBits(GPIOB,GPIO_Pin_12);
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
}
if(state == BACK)//左前电机后退
{
GPIO_SetBits(GPIOB,GPIO_Pin_13);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}
if(state == STOP)//停转
{
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
}
}
//右电机
void Motor_CtlRight(char state)
{
if(state == GO)//左前电机前进
{
GPIO_SetBits(GPIOB,GPIO_Pin_15);
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
}
if(state == BACK)//左前电机后退
{
GPIO_SetBits(GPIOB,GPIO_Pin_14);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
}
if(state == STOP)//停转
{
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
}
}
//***************************前进***************************//
//只要配置()的状态就可以改变电机转动方向
void Car_Go(void)
{
//左电机 前 //右电机 前
Motor_CtlLeft(GO); Motor_CtlRight(GO);
}
void Car_Back(void)
{
//左电机 后 //右电机 后
Motor_CtlLeft(BACK); Motor_CtlRight(BACK);
}
//***************************向左转圈***************************//
void Car_TurnLeft(void)
{
//左电机 后 //右电机 前
Motor_CtlLeft(BACK); Motor_CtlRight(GO);
}
//***************************向右转圈***************************//
void Car_TurnRight(void)
{
//左电机 前 //右电机 后
Motor_CtlLeft(GO); Motor_CtlRight(BACK);
}
//***************************停车***************************//
void Car_Stop(void)
{
//左电机 停 //右电机 停
Motor_CtlLeft(STOP); Motor_CtlRight(STOP);
}