当前位置:网站首页>【跟着江科大学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);
}
}
好好努力 慢慢成长
边栏推荐
- C 6.0 language specification approved
- 【OBS】RTMPSockBuf_Fill, remote host closed connection.
- Read PG in data warehouse in one article_ stat
- How does the database perform dynamic custom sorting?
- 什么是数据泄露
- Lidar knowledge drops
- Classification of regression tests
- [机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
- Cocoscreator resource encryption and decryption
- Bits and Information & integer notes
猜你喜欢
Niuke real problem programming - Day12
IDA pro逆向工具寻找socket server的IP和port
数学建模——什么是数学建模
CTFshow,信息搜集:web6
Data Lake (IX): Iceberg features and data types
CTFshow,信息搜集:web1
Niuke real problem programming - day20
【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
《微信小程序-进阶篇》组件封装-Icon组件的实现(一)
Niuke real problem programming - day13
随机推荐
C# 6.0 语言规范获批
PAG experience: complete AE dynamic deployment and launch all platforms in ten minutes!
Wechat applet - Advanced chapter component packaging - Implementation of icon component (I)
Bill Gates posted his resume 48 years ago: "it's not as good-looking as yours."
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
PD virtual machine tutorial: how to set the available shortcut keys in the parallelsdesktop virtual machine?
Stm32cubemx, 68 sets of components, following 10 open source protocols
Jetson AGX Orin CANFD 使用
Niuke real problem programming - day14
CTFshow,信息搜集:web12
什么是数据泄露
Es log error appreciation -trying to create too many buckets
“百度杯”CTF比赛 2017 二月场,Web:include
word中删除一整页
Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched
Why do we use UTF-8 encoding?
Several ways of JS jump link
Discussion on CPU and chiplet Technology
Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
How does the database perform dynamic custom sorting?