当前位置:网站首页>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);
}
}
}
边栏推荐
- es面試題
- Sword finger offer II 031 Least recently used cache
- [learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table
- Flutter un élément au milieu, l'élément le plus à droite
- Quality means doing it right when no one is looking
- how to add one row in the dataframe?
- Duplicate keys detected: ‘0‘. This may cause an update error. found in
- Deep learning: a solution to over fitting in deep neural networks
- 研发中台拆分过程的一些心得总结
- trading
猜你喜欢

How to run oddish successfully from 0?

Query word weight, search word weight calculation

RTL8189FS如何关闭Debug信息

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

Deployment practice and problem solving of dash application development environment based on jupyter Lab

A quick understanding of analog electricity

Architecture evolution from MVC to DDD

【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)
![[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)](/img/8e/a86a9724251718d98ce172a6a96e53.png)
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)

Leetcode face T10 (1-9) array, ByteDance interview sharing
随机推荐
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
How to use redis ordered collection
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
SQL server calculates the daily average and annual average of the whole province
CSDN article underlined, font color changed, picture centered, 1 second to understand
【毕业季】研究生学长分享怎样让本科更有意义
leetcode2312. 卖木头块(困难,周赛)
Vsocde has cli every time it is opened js
Software testing learning notes - network knowledge
Open那啥的搭建文档
Calculation (computer) code of suffix expression
离婚3年以发现尚未分割的共同财产,还可以要么
If you want to rewind the video picture, what simple methods can you use?
[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
[learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
Email picture attachment
【liuyubobobo-玩转Leetcode算法面试】【00】课程概述
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
golang---锁