当前位置:网站首页>STM32__05—PWM控制直流电机
STM32__05—PWM控制直流电机
2022-07-02 02:24:00 【c语言的神】
一,TB6612电机驱动模块
直流电机属于大功率器件,GPIO无法直接驱动,需要电机驱动模块配合,才能驱动直流电机.
TB6612可以驱动2个直流电机。由IN1,IN2控制电机旋转方向,由PWM控制电机旋转速度。
二,代码部分
核心代码还是为PWM部分,直流电机初始化也只是多初始化2了两个GPIO口,用于产生电压差,驱动电机,并且可以通过2个GPIO口改变电机旋转方向。
接线图来源:江科大
PWM.c
#include "stm32f10x.h" // Device header
void PWM_Init(void)
{
//开启TIM2连接的总线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);
//选择内部时钟
TIM_InternalClockConfig(TIM2);
//时基单元初始化
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);
}
}
}
边栏推荐
- JVM面试篇
- Flutter un élément au milieu, l'élément le plus à droite
- CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
- Sword finger offer 31 Stack push in and pop-up sequence
- What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
- Types of exhibition items available in the multimedia interactive exhibition hall
- 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
- how to come in an investnent bank team
- CSDN article underlined, font color changed, picture centered, 1 second to understand
- 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
猜你喜欢
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
leetcode2312. Selling wood blocks (difficult, weekly race)
No programming code technology! Four step easy flower store applet
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
How to solve MySQL master-slave delay problem
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
Ar Augmented Reality applicable scenarios
LFM signal denoising, time-frequency analysis, filtering
随机推荐
query词权重, 搜索词权重计算
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
Flutter un élément au milieu, l'élément le plus à droite
If you want to rewind the video picture, what simple methods can you use?
Calculation (computer) code of suffix expression
Open that kind of construction document
Software testing learning notes - network knowledge
Sword finger offer 42 Maximum sum of continuous subarrays
剑指 Offer 31. 栈的压入、弹出序列
[deep learning] Infomap face clustering facecluster
leetcode373. 查找和最小的 K 对数字(中等)
[punch in questions] integrated daily 5-question sharing (phase II)
设置状态栏颜色
Duplicate keys detected: ‘0‘. This may cause an update error. found in
JPM 2021 most popular paper released (with download)
[learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
JVM面试篇
The basic steps of using information theory to deal with scientific problems are
Architecture evolution from MVC to DDD
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)