当前位置:网站首页>【跟着江科大学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);
}
}
好好努力 慢慢成长
边栏推荐
- Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
- Wechat applet - Advanced chapter component packaging - Implementation of icon component (I)
- Huawei cloud database DDS products are deeply enabled
- 一文读懂数仓中的pg_stat
- Niuke real problem programming - Day11
- Pytorch model trains practical skills and breaks through the bottleneck of speed
- How to enable radius two factor / two factor (2fa) identity authentication for Anheng fortress machine
- 数据库如何进行动态自定义排序?
- 什么是数据泄露
- With 8 modules and 40 thinking models, you can break the shackles of thinking and meet the thinking needs of different stages and scenes of your work. Collect it quickly and learn it slowly
猜你喜欢
广州开发区让地理标志产品助力乡村振兴
暑期安全很重要!应急安全教育走进幼儿园
Summer safety is very important! Emergency safety education enters kindergarten
Ctfshow, information collection: Web3
Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
C 6.0 language specification approved
Niuke real problem programming - Day12
Niuke real problem programming - day18
Niuke real problem programming - day15
15、文本编辑工具VIM使用
随机推荐
What is the process of ⼀ objects from loading into JVM to being cleared by GC?
Ctfshow, information collection: web13
@Introduction and three usages of controlleradvice
CTFshow,信息搜集:web10
Ffmpeg --- image processing
Attribute keywords serveronly, sqlcolumnnumber, sqlcomputecode, sqlcomputed
Ctfshow, information collection: web4
Comparable and comparator of sorting
PD virtual machine tutorial: how to set the available shortcut keys in the parallelsdesktop virtual machine?
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
Summer safety is very important! Emergency safety education enters kindergarten
Huawei cloud database DDS products are deeply enabled
一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
13 ux/ui/ue best creative inspiration websites in 2022
Electronic remote error
CTFshow,信息搜集:web1
How does the database perform dynamic custom sorting?
CTFshow,信息搜集:web2
Ctfshow, information collection: web8