当前位置:网站首页>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);
}
}
}
边栏推荐
- Cesium dynamic diffusion point effect
- Provincial election + noi Part IV graph theory
- leetcode2312. 卖木头块(困难,周赛)
- [liuyubobobo play with leetcode algorithm interview] [00] Course Overview
- Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
- Is bone conduction earphone better than traditional earphones? The sound production principle of bone conduction earphones is popular science
- Quality means doing it right when no one is looking
- Sword finger offer 31 Stack push in and pop-up sequence
- Email picture attachment
- JVM interview
猜你喜欢
How to turn off the LED light of Rog motherboard
The basic steps of using information theory to deal with scientific problems are
JPM 2021 most popular paper released (with download)
LFM signal denoising, time-frequency analysis, filtering
研发中台拆分过程的一些心得总结
If you want to rewind the video picture, what simple methods can you use?
No programming code technology! Four step easy flower store applet
Cesium dynamic diffusion point effect
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
随机推荐
Cesium dynamic diffusion point effect
Medical management system (C language course for freshmen)
Pat a-1165 block reversing (25 points)
How does MySQL solve the problem of not releasing space after deleting a large amount of data
Opengauss database backup and recovery guide
how to add one row in the dataframe?
Webgpu (I): basic concepts
LeetCode刷题(十)——顺序刷题46至50
LFM信号加噪、时频分析、滤波
[opencv] - comprehensive examples of five image filters
Sword finger offer 29 Print matrix clockwise
研发中台拆分过程的一些心得总结
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
2022安全员-C证考试题及模拟考试
MySQL主从延迟问题怎么解决
Leetcode face T10 (1-9) array, ByteDance interview sharing
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing
[pit] how to understand "parameter fishing"
leetcode2310. The one digit number is the sum of integers of K (medium, weekly)