当前位置:网站首页>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);
}
}
}
边栏推荐
- Questions d'entrevue
- Ar Augmented Reality applicable scenarios
- Is bone conduction earphone better than traditional earphones? The sound production principle of bone conduction earphones is popular science
- Es interview questions
- Calculation (computer) code of suffix expression
- Start from scratch - Web Host - 01
- 【深度学习】infomap 人脸聚类 facecluster
- 使用开源项目【Banner】实现轮播图效果(带小圆点)
- How to execute an SQL in MySQL
- How to solve MySQL master-slave delay problem
猜你喜欢

Golang lock

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

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

What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality

LFM signal denoising, time-frequency analysis, filtering

If you want to rewind the video picture, what simple methods can you use?

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

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

leetcode2305. 公平分发饼干(中等,周赛,状压dp)

Software development life cycle -- waterfall model
随机推荐
Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
软件开发生命周期 --瀑布模型
Questions d'entrevue
Post infiltration flow encryption
How to turn off debug information in rtl8189fs
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
Flutter un élément au milieu, l'élément le plus à droite
How to run oddish successfully from 0?
STM32F103 - two circuit PWM control motor
How to build and use redis environment
STM32F103——两路PWM控制电机
What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
How to use redis ordered collection
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
No programming code technology! Four step easy flower store applet
Sword finger offer 31 Stack push in and pop-up sequence
C write TXT file
Is bone conduction earphone better than traditional earphones? The sound production principle of bone conduction earphones is popular science
How to hide the scroll bar of scroll view in uniapp