当前位置:网站首页>STM32__ 05 - PWM controlled DC motor
STM32__ 05 - PWM controlled DC motor
2022-07-02 02:28:00 【The God of C language】
One ,TB6612 Motor drive module



DC motor belongs to high-power device ,GPIO Cannot drive directly , The motor drive module is required to cooperate , To drive the DC motor .
TB6612 Can drive 2 One DC motor . from IN1,IN2 Control the rotation direction of the motor , from PWM Control the rotation speed of the motor .
Two , Code section
The core code is still PWM part , DC motor initialization is only multi initialization 2 Two GPIO mouth , Used to generate voltage difference , Drive motor , And through 2 individual GPIO Change the rotation direction of the motor .
Source of wiring diagram : Jiang Keda

PWM.c
#include "stm32f10x.h" // Device header
void PWM_Init(void)
{
// Turn on TIM2 Connected bus 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);
// Select the internal clock
TIM_InternalClockConfig(TIM2);
// Time base unit initialization
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);
}
}
}
边栏推荐
- Vsocde has cli every time it is opened js
- Duplicate keys detected: ‘0‘. This may cause an update error. found in
- Set status bar color
- Cesium dynamic diffusion point effect
- Architecture evolution from MVC to DDD
- [graduation season] graduate seniors share how to make undergraduate more meaningful
- LFM信号加噪、时频分析、滤波
- Opencascade7.6 compilation
- How to hide the scroll bar of scroll view in uniapp
- 剑指 Offer 47. 礼物的最大价值
猜你喜欢

leetcode2309. 兼具大小写的最好英文字母(简单,周赛)

Which kind of sports headphones is easier to use? The most recommended sports headphones

leetcode2312. Selling wood blocks (difficult, weekly race)

pytest 测试框架

What is the MySQL column to row function

No programming code technology! Four step easy flower store applet

How to batch add background and transition effects to videos?

Webgpu (I): basic concepts

【OpenCV】-5种图像滤波的综合示例

Types of exhibition items available in the multimedia interactive exhibition hall
随机推荐
2022 safety officer-c certificate examination questions and mock examination
How to hide the scroll bar of scroll view in uniapp
Learning notes of software testing -- theoretical knowledge of software testing
How does MySQL solve the problem of not releasing space after deleting a large amount of data
Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing
CSDN article underlined, font color changed, picture centered, 1 second to understand
As a software testing engineer, will you choose the bank post? Laolao bank test post
Leetcode face T10 (1-9) array, ByteDance interview sharing
leetcode2312. 卖木头块(困难,周赛)
MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent
Calculation (computer) code of suffix expression
2022安全员-C证考试题及模拟考试
2022 low voltage electrician test question simulation test question bank simulation test platform operation
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
2022低压电工考试题模拟考试题库模拟考试平台操作
【OpenCV】-5种图像滤波的综合示例
Sword finger offer 62 The last remaining number in the circle
Summary of some experiences in the process of R & D platform splitting
Software development life cycle -- waterfall model
What is the function of the headphone driver