当前位置:网站首页>【跟着江科大学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);
}
}
好好努力 慢慢成长
边栏推荐
- Full details of efficientnet model
- CTFshow,信息搜集:web10
- Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
- Attribute keywords serveronly, sqlcolumnnumber, sqlcomputecode, sqlcomputed
- Ctfshow, information collection: web10
- Es log error appreciation -maximum shards open
- Today's sleep quality record 78 points
- 拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条...
- 电脑Win7系统桌面图标太大怎么调小
- Lidar knowledge drops
猜你喜欢
Ctfshow, information collection: web14
Ctfshow, information collection: web8
What is the process of ⼀ objects from loading into JVM to being cleared by GC?
CTFshow,信息搜集:web3
CTFshow,信息搜集:web13
「2022年7月」WuKong编辑器更版记录
Yyds dry goods inventory # solve the real problem of famous enterprises: cross line
Niuke real problem programming - day16
CTFshow,信息搜集:web10
CTFshow,信息搜集:web7
随机推荐
Huawei cloud database DDS products are deeply enabled
Pandora IOT development board learning (HAL Library) - Experiment 12 RTC real-time clock experiment (learning notes)
Today's sleep quality record 78 points
Summer safety is very important! Emergency safety education enters kindergarten
Niuke real problem programming - day14
@ComponentScan
Webrtc audio anti weak network technology (Part 1)
Ctfshow, information collection: web10
CTFshow,信息搜集:web14
Niuke real problem programming - day13
What is the process of ⼀ objects from loading into JVM to being cleared by GC?
Introduction and use of Kitti dataset
Used by Jetson AgX Orin canfd
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
「2022年7月」WuKong编辑器更版记录
Simple steps for modifying IP of sigang electronic scale
⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
Bye, Dachang! I'm going to the factory today
Ctfshow, information collection: web1
Classification of regression tests