当前位置:网站首页>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);
}
}
}
边栏推荐
- How to execute an SQL in MySQL
- Architecture evolution from MVC to DDD
- What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
- Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing
- essay structure
- How to use redis ordered collection
- The wave of layoffs in big factories continues, but I, who was born in both non undergraduate schools, turned against the wind and entered Alibaba
- 【liuyubobobo-玩转Leetcode算法面试】【00】课程概述
- 【做题打卡】集成每日5题分享(第二期)
- Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing
猜你喜欢

Comparative analysis of MVC, MVP and MVVM, source code analysis

【liuyubobobo-玩转Leetcode算法面试】【00】课程概述

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

Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing

Sword finger offer 62 The last remaining number in the circle

How to execute an SQL in MySQL

As a software testing engineer, will you choose the bank post? Laolao bank test post

Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
![[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology

golang---锁
随机推荐
[deep learning] Infomap face clustering facecluster
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
leetcode373. 查找和最小的 K 对数字(中等)
【带你学c带你飞】3day第2章 用C语言编写程序(练习 2.3 计算分段函数)
Sword finger offer 42 Maximum sum of continuous subarrays
Logging only errors to the console Set system property ‘log4j2. debug‘ to sh
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
Realize the code scanning function of a custom layout
【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
软件开发生命周期 --瀑布模型
Leetcode face T10 (1-9) array, ByteDance interview sharing
Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing
MySQL constraints and multi table query example analysis
The wave of layoffs in big factories continues, but I, who was born in both non undergraduate schools, turned against the wind and entered Alibaba
Which kind of sports headphones is easier to use? The most recommended sports headphones
CSDN article underlined, font color changed, picture centered, 1 second to understand
Bash bounce shell encoding
[question] - why is optical flow not good for static scenes
Infix expression to suffix expression (computer) code