当前位置:网站首页>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);
}
}
}
边栏推荐
- Iterative unified writing method of binary tree
- 2022安全员-C证考试题及模拟考试
- MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent
- CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
- AR增强现实可应用的场景
- leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
- 剑指 Offer 29. 顺时针打印矩阵
- Post infiltration flow encryption
- Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
- es面试题
猜你喜欢

OpenCASCADE7.6编译

Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved

LeetCode刷题(十)——顺序刷题46至50

超图iServer rest服务之feature查询

leetcode2310. 个位数字为 K 的整数之和(中等,周赛)

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

JVM interview

花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案

Redis环境搭建和使用的方法

Ar Augmented Reality applicable scenarios
随机推荐
OpenCASCADE7.6编译
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
JPM 2021 most popular paper released (with download)
Software testing learning notes - network knowledge
If you want to rewind the video picture, what simple methods can you use?
How to solve MySQL master-slave delay problem
Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing
how to come in an investnent bank team
C # use system data. The split mixed mode assembly is generated for the "v2.0.50727" version of the runtime, and it cannot be loaded in the 4.0 runtime without configuring other information
剑指 Offer 31. 栈的压入、弹出序列
golang---锁
CoordinatorLayout + TabLayout + ViewPager2(里面再嵌套一个RecyclerView),RecyclerView的滑动冲突解决
Infix expression to suffix expression (computer) code
Realize the code scanning function of a custom layout
JVM interview
Learning notes of software testing -- theoretical knowledge of software testing
* and & symbols in C language
What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
Which brand of running headphones is good? How many professional running headphones are recommended
Data analysis on the disaster of Titanic