当前位置:网站首页>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);
}
}
}
边栏推荐
- MySQL约束与多表查询实例分析
- leetcode2312. Selling wood blocks (difficult, weekly race)
- [technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
- 【带你学c带你飞】3day第2章 用C语言编写程序(练习 2.3 计算分段函数)
- What is the MySQL column to row function
- STM32F103 - two circuit PWM control motor
- LFM signal denoising, time-frequency analysis, filtering
- Webgpu (I): basic concepts
- Sword finger offer 31 Stack push in and pop-up sequence
- No programming code technology! Four step easy flower store applet
猜你喜欢

What kind of good and cost-effective Bluetooth sports headset to buy

JPM 2021 most popular paper released (with download)

What is the principle of bone conduction earphones and who is suitable for bone conduction earphones

Software No.1

A quick understanding of analog electricity

The basic steps of using information theory to deal with scientific problems are

A quick understanding of digital electricity
![[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)](/img/8e/a86a9724251718d98ce172a6a96e53.png)
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)

How to solve MySQL master-slave delay problem

Ar Augmented Reality applicable scenarios
随机推荐
【做题打卡】集成每日5题分享(第二期)
How to turn off debug information in rtl8189fs
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
Sword finger offer 42 Maximum sum of continuous subarrays
As a software testing engineer, will you choose the bank post? Laolao bank test post
SQL server calculates the daily average and annual average of the whole province
花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
JPM 2021 most popular paper released (with download)
Deployment practice and problem solving of dash application development environment based on jupyter Lab
【毕业季】研究生学长分享怎样让本科更有意义
STM32F103——两路PWM控制电机
Set status bar color
STM32F103 - two circuit PWM control motor
What is the function of the headphone driver
Types of exhibition items available in the multimedia interactive exhibition hall
Calculation (computer) code of suffix expression
Regular expression learning notes
【liuyubobobo-玩转Leetcode算法面试】【00】课程概述
Design and implementation of key value storage engine based on LSM tree
【带你学c带你飞】day 5 第2章 用C语言编写程序(习题2)