当前位置:网站首页>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);
}
}
}
边栏推荐
- What is the MySQL column to row function
- An analysis of circuit for quick understanding
- Sword finger offer 31 Stack push in and pop-up sequence
- Architecture evolution from MVC to DDD
- How to turn off the LED light of Rog motherboard
- Open that kind of construction document
- STM32F103——两路PWM控制电机
- [C #] use regular verification content
- MySQL主从延迟问题怎么解决
- Sword finger offer 62 The last remaining number in the circle
猜你喜欢
MySQL constraints and multi table query example analysis
How to execute an SQL in MySQL
How to build and use redis environment
How to run oddish successfully from 0?
golang---锁
[question] - why is optical flow not good for static scenes
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
Cesium dynamic diffusion point effect
leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
Architecture evolution from MVC to DDD
随机推荐
es面试题
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表
A quick understanding of analog electricity
JVM面试篇
Post infiltration flow encryption
What is the function of the headphone driver
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
Architecture evolution from MVC to DDD
STM32F103 - two circuit PWM control motor
No programming code technology! Four step easy flower store applet
剑指 Offer II 031. 最近最少使用缓存
【毕业季】研究生学长分享怎样让本科更有意义
STM32F103——两路PWM控制电机
2022低压电工考试题模拟考试题库模拟考试平台操作
Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
离婚3年以发现尚未分割的共同财产,还可以要么
Sword finger offer 29 Print matrix clockwise
Deep learning: a solution to over fitting in deep neural networks
Sword finger offer II 031 Least recently used cache