当前位置:网站首页>[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
2022-07-07 15:06:00 【Related to the sun_】
Code :
PWM.h
#ifndef __PWM_H
#define __PWM_H
void PWM_Init();
void PWM_SetCompare3(uint16_t Compare);
#endif
Inside the motor Coil and magnet
, So in PWM Driven by , The buzzer will sound
When the SCM is set PWM When the frequency is within the range that our human ears can receive , We press and hold the rotating motor , You will hear an obvious buzzing ( The sound of buzzer )
Methods of elimination : enlarge PWM frequency , When PWM When the frequency is large enough , Beyond the scope of human ears , I can't hear it .Reduce prescaler , This will not affect the duty cycle
The radio frequency range of the human ear :20Hz To 20kHz.
It is amended as follows :TIM_TimeBaseInitStructure.TIM_Prescaler =36
- 1; // The frequency changes to 20kHz
PWM.c
#include "stm32f10x.h" // Device header
void PWM_Init()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); // Turn on timer 2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);// passageway 2 Clock enable function
GPIO_InitTypeDef GPIO_InitStructure; // Definition GPIO Initializing structure variables
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Set up GPIO For push-pull output mode
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // The speed is set to 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); // Proceed according to the above parameters GPIO The initialization
TIM_InternalClockConfig(TIM2);//TIM The time base unit of is controlled by the internal clock
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 The value of the automatic reloader
TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC Prescaler value Yes 72M(720000000) Conduct 720 frequency division namely 1K At different frequencies meter 1000 Number 1s Time for
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;// Repeat counter value
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;// Output polarity selection
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;// Output status enable
TIM_OCInitStructure.TIM_Pulse = 10;//CCR, That is, the duty cycle is 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);// passageway 2 Clock enable function
GPIO_InitTypeDef GPIO_InitStructure; // Definition GPIO Initializing structure variables
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; // Set up GPIO For push-pull output mode
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;// Motor direction control foot
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // The speed is set to 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); // Proceed according to the above parameters GPIO The initialization
PWM_Init();
}
void Motor_SetSpeed(int8_t Speed)
{
if(Speed >= 0) // Positive rotation ( Turn clockwise )
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
PWM_SetCompare3(Speed);
}
else// reverse ( Turn counter clockwise )
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
PWM_SetCompare3(-Speed);
}
}
main.c
Press the key to control the motor speed
Code here shows KeyNum = Key_GetNum(); What you get isB1
Pin value , So I put the button on this foot .
#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);
}
}
Work hard Grow slowly
边栏推荐
- Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- Change win10 Screensaver
- 数据库如何进行动态自定义排序?
- Several ways of JS jump link
- Today's sleep quality record 78 points
- The method of parsing PHP to jump out of the loop and the difference between continue, break and exit
- CTFshow,信息搜集:web2
- Pytorch model trains practical skills and breaks through the bottleneck of speed
- 【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
猜你喜欢
CTFshow,信息搜集:web3
Ctfshow, information collection: web5
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
Niuke real problem programming - day15
Ctfshow, information collection: web6
Ctfshow, information collection: web9
什麼是數據泄露
【深度学习】语义分割实验:Unet网络/MSRC2数据集
WebRTC 音频抗弱网技术(上)
随机推荐
Ctfshow, information collection: web13
"July 2022" Wukong editor update record
Several ways of JS jump link
Spatiotemporal deformable convolution for compressed video quality enhancement (STDF)
Navigation — 这么好用的导航框架你确定不来看看?
asp.netNBA信息管理系统VS开发sqlserver数据库web结构c#编程计算机网页源码项目详细设计
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
CPU与chiplet技术杂谈
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
Why do we use UTF-8 encoding?
智汀不用Home Assistant让小米智能家居接入HomeKit
缓冲区溢出保护
Pandora IOT development board learning (HAL Library) - Experiment 12 RTC real-time clock experiment (learning notes)
word中删除一整页
Computer win7 system desktop icon is too large, how to turn it down
上半年晋升 P8 成功,还买了别墅!
Concurrency Control & NoSQL and new database
防火墙基础之服务器区的防护策略
CTFshow,信息搜集:web5
Electronic remote error