BILIBILILI链接(直接戳跳转)
K007 基于51/STM32+TB6612实现按键控制电机正反转和加减速
按键 K_Start_Stop停止和启动电机,默认上电停止
按键 K_Syntropy_Reverse 切换两电机转向
按键 K_UP_Speed 加速按键,按下加速
按键 K_Down_Speed 减速按键,按下减速
此程序主要基于STM32F103C8T6驱动TB6612电机一些动作,动作改变是用四个按键控制两个电机启动停止,同向反向和加减速。
TB6612 -----------------------------STM32F103C8T6
VCC--5V
VM--7-10V
GND
STBY-------------------------------------3.3/5V
PWMA-------------------------------------GPIOA_6
AIN_1------------------------------------GPIOB_12
AIN_2------------------------------------GPIOB_13
BIN_3------------------------------------GPIOB_14
BIN_4------------------------------------GPIOB_15
PWMB-------------------------------------GPIOA_7
//按键
K_Start_Stop ---------------------GPIOB_4 //启停按键 GPIOB_Pin_4 0x0010 //?GPIOB_Pin_3
K_Syntropy_Reverse ---------------------GPIOB_5 //两电机同向反向按键 GPIOB_Pin_5 0x0020
K_UP_Speed ---------------------GPIOB_6 //两电机同时加速按键 GPIOB_Pin_6 0x0040
K_Down_Speed ---------------------GPIOB_7 //两电机同时减速按键 GPIOB_Pin_7 0x0080
TB6612 -----------------------------STC89C52RC
VCC--5V
VM--7-10V
GND
STBY-------------------------------------3.3/5V
PWMA-------------------------------------P1^4
AIN_1------------------------------------P1^3
AIN_2------------------------------------P1^2
BIN_3------------------------------------P1^1
BIN_4------------------------------------P1^0
PWMB-------------------------------------P1^5
//按键
K_Start_Stop ---------------------P2^0 //启停按键
K_Syntropy_Reverse ---------------------P2^1 //两电机同向反向按键
K_UP_Speed ---------------------P2^2 //两电机同时加速按键
K_Down_Speed ---------------------P2^3 //两电机同时减速按键
TB6612和L298N驱动方式基本一致,以下以32代码为例,
/*--------------------初始化PWM输出口引脚-------------------------*/
void GENERAL_TIM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 输出比较通道1 GPIO 初始化
RCC_APB2PeriphClockCmd(GENERAL_TIM_CH1_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = GENERAL_TIM_CH1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GENERAL_TIM_CH1_PORT, &GPIO_InitStructure);
// 输出比较通道2 GPIO 初始化
RCC_APB2PeriphClockCmd(GENERAL_TIM_CH2_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = GENERAL_TIM_CH2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GENERAL_TIM_CH2_PORT, &GPIO_InitStructure);
}
/* ---------------- PWM信号 周期和占空比的计算--------------- */
// ARR :自动重装载寄存器的值
// CLK_cnt:计数器的时钟,等于 Fck_int / (psc+1) = 72M/(psc+1)
// PWM 信号的周期 T = ARR * (1/CLK_cnt) = ARR*(PSC+1) / 72M
// 占空比P=CCR/(ARR+1)
void GENERAL_TIM_Mode_Config(void)
{
// 开启定时器时钟,即内部时钟CK_INT=72M
GENERAL_TIM_APBxClock_FUN(GENERAL_TIM_CLK,ENABLE);
/*--------------------时基结构体初始化-------------------------*/
// 配置周期,这里配置为100K
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// 自动重装载寄存器的值,累计TIM_Period+1个频率后产生一个更新或者中断
TIM_TimeBaseStructure.TIM_Period=GENERAL_TIM_Period;
// 驱动CNT计数器的时钟 = Fck_int/(psc+1)
TIM_TimeBaseStructure.TIM_Prescaler= GENERAL_TIM_Prescaler;
// 时钟分频因子 ,配置死区时间时需要用到
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
// 计数器计数模式,设置为向上计数
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
// 重复计数器的值,没用到不用管
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
// 初始化定时器
TIM_TimeBaseInit(GENERAL_TIM, &TIM_TimeBaseStructure);
/*--------------------输出比较结构体初始化-------------------*/
// 占空比配置
uint16_t CCR1_Val = 0;
uint16_t CCR2_Val = 0;
// uint16_t CCR3_Val = 0;
// uint16_t CCR4_Val = 0;
TIM_OCInitTypeDef TIM_OCInitStructure;
// 配置为PWM模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// 输出使能
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// 输出通道电平极性配置
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// 输出比较通道 1
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OC1Init(GENERAL_TIM, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
// 输出比较通道 2
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(GENERAL_TIM, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
// 使能计数器
TIM_Cmd(GENERAL_TIM, ENABLE);
}
void GENERAL_TIM_Init(void)
{
GENERAL_TIM_GPIO_Config();
GENERAL_TIM_Mode_Config();
}
void Motor_GPIO_Init(void)
{
GPIO_InitTypeDef Motor_GPIO_InitStructure;
/*开启按键端口的时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//选择按键的引脚
Motor_GPIO_InitStructure.GPIO_Pin = IN1_L;
// 设置按键的引脚为推挽输出
Motor_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
Motor_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &Motor_GPIO_InitStructure);
//选择按键的引脚
Motor_GPIO_InitStructure.GPIO_Pin = IN2_L;
GPIO_Init(GPIOB, &Motor_GPIO_InitStructure);
//选择按键的引脚
Motor_GPIO_InitStructure.GPIO_Pin = IN3_R;
GPIO_Init(GPIOB, &Motor_GPIO_InitStructure);
//选择按键的引脚
Motor_GPIO_InitStructure.GPIO_Pin = IN4_R;
GPIO_Init(GPIOB, &Motor_GPIO_InitStructure);
}
void Key_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*开启按键端口的时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//选择按键的引脚
GPIO_InitStructure.GPIO_Pin = K_Start_Stop;
// 设置按键的引脚为上拉输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//选择按键的引脚
GPIO_InitStructure.GPIO_Pin = K_Syntropy_Reverse;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//选择按键的引脚
GPIO_InitStructure.GPIO_Pin = K_UP_Speed;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//选择按键的引脚
GPIO_InitStructure.GPIO_Pin = K_Down_Speed;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Scan_key(void)
{
uint16_t Key_Dat=0;
Key_Dat=GPIO_ReadInputData(GPIOB);
Key_Dat=Key_Dat&0x00F0;
Key_Dat=Key_Dat^0x00F0;
if(Key_Dat==K_Start_Stop)
{
DelayMs(10);
if(Key_Dat==K_Start_Stop)
{
Flag_Start_Stop=~Flag_Start_Stop;
}
while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4));
}
else if(Key_Dat==K_Syntropy_Reverse)
{
DelayMs(10);
if(Key_Dat==K_Syntropy_Reverse)
{
Flag_Syntropy_Reverse=~Flag_Syntropy_Reverse;
}
while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5));
}
else if(Key_Dat==K_UP_Speed)
{
DelayMs(10);
if(Key_Dat==K_UP_Speed)
{
Speed=Speed+5;
if(Speed>=999)
{
Speed=999;
}
}
while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6));
}
else if(Key_Dat==K_Down_Speed)
{
DelayMs(10);
if(Key_Dat==K_Down_Speed)
{
Speed=Speed-5;
if(Speed<=10)
{
Speed=10;
}
}
while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7));
}
}
/************************同向转动**************************/
void turn_Syntropy(void)
{
TIM_SetCompare1(GENERAL_TIM, Speed); //
IN1_L_H;
IN2_L_L;
TIM_SetCompare2(GENERAL_TIM, Speed); //
IN3_R_H;
IN4_R_L;
}
/************************反向转动**************************/
void turn_Reverse(void)
{
TIM_SetCompare1(GENERAL_TIM, Speed); //
IN1_L_H;
IN2_L_L;
TIM_SetCompare2(GENERAL_TIM, Speed); //
IN3_R_L;
IN4_R_H;
}
/************************停止**************************/
void turn_Stop(void)
{
TIM_SetCompare1(GENERAL_TIM, 0);//
TIM_SetCompare2(GENERAL_TIM, 0);//
}
/**
* @brief 主函数
* @param 无
* @retval 无
*/
int main(void)
{
/* 定时器初始化 */
GENERAL_TIM_Init();
DelayInit();
Key_GPIO_Init();
turn_Stop();
Motor_GPIO_Init();
// turn_Syntropy();
while(1)
{
Scan_key();
if(Flag_Start_Stop)
{
if(!Flag_Syntropy_Reverse)
{
turn_Syntropy();
}
else
{
turn_Reverse();
}
}
else
{
turn_Stop();
}
}
}
搬砖不易,跪求三连(点赞,收藏或者说说心里话(不足之处也欢迎指点哦),关注)。
三连后截图,上门找群主,企鹅群号:1041406448
截图私发群主,”逼问“资料下落,不给就锤他