当前位置:网站首页>【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
2022-07-07 13:02:00 【与太阳有关_】

代码:
PWM.h
#ifndef __PWM_H
#define __PWM_H
void PWM_Init();
void PWM_SetCompare3(uint16_t Compare);
#endif
电机里面也是线圈和磁铁,所以在PWM的驱动下,会发出蜂鸣器的声音
当单片机设置的PWM之频率在我们人耳所能接收的范围时,我们按住正在旋转的电机,会听到明显的嗡嗡声(蜂鸣器的声音)
消除的方法: 加大PWM频率,当PWM频率足够大时,超出人耳的范围,就听不到了。减小预分频器,这样才不会影响占空比
人耳的收音频率范围:20Hz到20kHz。
修改为:TIM_TimeBaseInitStructure.TIM_Prescaler =36- 1; //频率变为 20kHz
PWM.c
#include "stm32f10x.h" // Device header
void PWM_Init()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //开启定时器2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//通道2时钟使能函数
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体变量
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置GPIO为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度设置为 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //按照以上参数进行 GPIO的初始化
TIM_InternalClockConfig(TIM2);//TIM的时基单元由内部时钟控制
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR 自动重装器的值
TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC 预分频器的值 对72M(720000000)进行 720分频 即1K的频率下 计1000个数 1s的时间
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器的值
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性选择
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出状态使能
TIM_OCInitStructure.TIM_Pulse = 10;//CCR,即占空比为 10%
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
TIM_Cmd(TIM2,ENABLE);
}
void PWM_SetCompare3(uint16_t Compare)
{
TIM_SetCompare3(TIM2, Compare);
}
Motor.h
#ifndef __MOTOR_H
#define __MOTOR_H
void Motor_Init();
void Motor_SetSpeed(int8_t Speed);
#endif
Motor.c
#include "stm32f10x.h" // Device header
#include "PWM.h"
void Motor_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//通道2时钟使能函数
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体变量
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //设置GPIO为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;//电机方向控制脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度设置为 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //按照以上参数进行 GPIO的初始化
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_ResetBits(GPIOA, GPIO_Pin_4);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
PWM_SetCompare3(-Speed);
}
}
main.c
按键控制电机速度
此处代码显示 KeyNum = Key_GetNum(); 获取的是B1引脚的值,所以我把按键接在这个脚。
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "OLED.H"
#include "Motor.H"
uint8_t KeyNum,Speed;;
int main(void)
{
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_ShowNum(1,7,Speed,3);
}
}
好好努力 慢慢成长
边栏推荐
- Xiaomi's path of chip self-development
- [server data recovery] a case of RAID data recovery of a brand StorageWorks server
- Integer learning
- PAG experience: complete AE dynamic deployment and launch all platforms in ten minutes!
- "Baidu Cup" CTF competition 2017 February, web:include
- Win10 or win11 taskbar, automatically hidden and transparent
- Attribute keywords ondelete, private, readonly, required
- 【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
- Ctfshow, information collection: web2
- Ctfshow, information collection: web14
猜你喜欢

Five pain points for big companies to open source

Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched

“百度杯”CTF比赛 2017 二月场,Web:include

Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?

Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System

Deformable convolutional dense network for enhancing compressed video quality

Ctfshow, information collection: web1

CTFshow,信息搜集:web5

Niuke real problem programming - Day12

数学建模——什么是数学建模
随机推荐
CTFshow,信息搜集:web13
什么是数据泄露
13 ux/ui/ue best creative inspiration websites in 2022
Xiaomi's path of chip self-development
Navigation — 这么好用的导航框架你确定不来看看?
Read PG in data warehouse in one article_ stat
Niuke real problem programming - day13
Integer learning
Es log error appreciation -trying to create too many buckets
What is cloud primordial? This time, I can finally understand!
PG basics -- Logical Structure Management (locking mechanism -- table lock)
What is the process of ⼀ objects from loading into JVM to being cleared by GC?
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
防火墙基础之服务器区的防护策略
Today's sleep quality record 78 points
Compile advanced notes
Huawei cloud database DDS products are deeply enabled
Ctfshow, information collection: web10
How bad can a programmer be? Nima, they are all talents
Five pain points for big companies to open source
