当前位置:网站首页>STM32__05—PWM控制直流电机
STM32__05—PWM控制直流电机
2022-07-02 02:24:00 【c语言的神】
一,TB6612电机驱动模块



直流电机属于大功率器件,GPIO无法直接驱动,需要电机驱动模块配合,才能驱动直流电机.
TB6612可以驱动2个直流电机。由IN1,IN2控制电机旋转方向,由PWM控制电机旋转速度。
二,代码部分
核心代码还是为PWM部分,直流电机初始化也只是多初始化2了两个GPIO口,用于产生电压差,驱动电机,并且可以通过2个GPIO口改变电机旋转方向。
接线图来源:江科大

PWM.c
#include "stm32f10x.h" // Device header
void PWM_Init(void)
{
//开启TIM2连接的总线APBB1
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIOA_InitStructure;
GPIOA_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIOA_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIOA_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIOA_InitStructure);
//选择内部时钟
TIM_InternalClockConfig(TIM2);
//时基单元初始化
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_TimeBaseStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStruct.TIM_Period=100-1; //ARR
TIM_TimeBaseStruct.TIM_Prescaler=36-1; //PSC
TIM_TimeBaseStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStruct);
TIM_OCInitTypeDef TIM_OCInitStructture;
TIM_OCStructInit(&TIM_OCInitStructture);
TIM_OCInitStructture.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructture.TIM_OCPolarity=TIM_OCPolarity_High ;
TIM_OCInitStructture.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructture.TIM_Pulse=90; //CCR
TIM_OC3Init(TIM2,&TIM_OCInitStructture);
TIM_Cmd(TIM2,ENABLE);
}
void PWM_SetCompare3(uint16_t Compare)
{
TIM_SetCompare3(TIM2,Compare);
}
Motor.c
#include "stm32f10x.h" // Device header
#include "PWM.h"
void Motor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIOA_InitStructure;
GPIOA_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIOA_InitStructure.GPIO_Pin=GPIO_Pin_4 | GPIO_Pin_5;
GPIOA_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIOA_InitStructure);
PWM_Init();
}
void Motor_SetSpeed(int8_t Speed)
{
if(Speed>=0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
PWM_SetCompare3(Speed);
}
else
{
GPIO_SetBits(GPIOA,GPIO_Pin_5);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
PWM_SetCompare3(-Speed);
}
}
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "Buzzer.h"
#include "OLED.h "
#include "Motor.h"
uint8_t KeyNum;
int8_t Speed;
int main()
{
OLED_Init();
Motor_Init();
Key_Init();
OLED_ShowString(1,1,"Speed:");
while(1)
{
KeyNum=Key_Getnum();
if(KeyNum==1)
{
Speed+=20;
if(Speed>100)
{
Speed=-100;
}
Motor_SetSpeed(Speed);
OLED_ShowSignedNum(1,7,Speed,3);
}
}
}
边栏推荐
- Which brand of running headphones is good? How many professional running headphones are recommended
- [punch in questions] integrated daily 5-question sharing (phase II)
- STM32F103 - two circuit PWM control motor
- 2022低压电工考试题模拟考试题库模拟考试平台操作
- CSDN article underlined, font color changed, picture centered, 1 second to understand
- Kibana controls es
- [reading notes] programmer training manual - practical learning is the most effective (project driven)
- 【做题打卡】集成每日5题分享(第二期)
- 超图iServer rest服务之feature查询
- query词权重, 搜索词权重计算
猜你喜欢

LFM信号加噪、时频分析、滤波

Software No.1

No programming code technology! Four step easy flower store applet

CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes

Cesium dynamic diffusion point effect

Opengauss database backup and recovery guide

Which brand of running headphones is good? How many professional running headphones are recommended

MySQL约束与多表查询实例分析

How does MySQL solve the problem of not releasing space after deleting a large amount of data

Deployment practice and problem solving of dash application development environment based on jupyter Lab
随机推荐
Es interview questions
Realize the code scanning function of a custom layout
Learning notes of software testing -- theoretical knowledge of software testing
Sword finger offer 31 Stack push in and pop-up sequence
pytest 测试框架
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
Sword finger offer 42 Maximum sum of continuous subarrays
JPM 2021 most popular paper released (with download)
Questions d'entrevue
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
【liuyubobobo-玩转Leetcode算法面试】【00】课程概述
Flutter un élément au milieu, l'élément le plus à droite
软件开发生命周期 --瀑布模型
es面试题
CSDN insertion directory in 1 second
JVM面试篇
[pit] how to understand "parameter fishing"
How to build and use redis environment
Post infiltration flow encryption
es面試題