当前位置:网站首页>STM32 general timer output PWM control steering gear
STM32 general timer output PWM control steering gear
2022-07-03 09:55:00 【Fake iron man】
stm32 The general timers are TIM2、TIM3、TIM4、TIM5, Each timer has four independent channels that can be used as : Input capture 、 Output comparison 、PWM Output 、 Single pulse mode output, etc .stm32 In addition to the basic timer , Other timers can output PWM.PWM It is commonly used to control steering gear and motor . I learned it today stm32 How to output the general timer of single chip microcomputer PWM Control of steering gear , It is summarized as follows :
Step summary :
To configure GPIO Structure ( here GPIO It is a pin with multiplexing function )-> Configure the general timer structure -> Configure timer output PWM Structure -> Enable preload register -> Enable timer -> To configure PWM It's worth -> Control of steering gear
ServoMotor.c:
#include "stm32f10x.h"
#include "ServoMotor.h"
void ServoMotor_Config(void)
{
GPIO_InitTypeDef GPIO_MOTORInitStructure;
TIM_TimeBaseInitTypeDef Tim_MOTORInitStructure;
TIM_OCInitTypeDef Time_PWMInitStructure;
// Turn on the clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
// Partial remapping
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
// To configure GPIO Structure
GPIO_MOTORInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_MOTORInitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_MOTORInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_MOTORInitStructure);
// Configure the general timer structure
Tim_MOTORInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
Tim_MOTORInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
Tim_MOTORInitStructure.TIM_Period = 200-1;
Tim_MOTORInitStructure.TIM_Prescaler = 7200-1;
TIM_TimeBaseInit(TIM3,&Tim_MOTORInitStructure);
// Configure timer output PWM Structure
Time_PWMInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
Time_PWMInitStructure.TIM_OutputState = TIM_OutputState_Enable;
Time_PWMInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM3,&Time_PWMInitStructure);
// Enable preload register
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
// Enable timer
TIM_Cmd(TIM3,ENABLE);
}
main.c:
#include "stm32f10x.h"
#include "ServoMotor.h"
void delay(uint16_t time)
{
uint16_t i = 0;
while(time -- )
{
i = 12000;
while(i -- );
}
}
int main(void)
{
ServoMotor_Config();
uint16_t pwm = 155;
while(1)
{
for(pwm = 195; pwm >=175; pwm-=5)
{
TIM_SetCompare2(TIM3,pwm);
delay(500);
}
}
}
If there is a mistake , Please criticize and correct me
边栏推荐
- UCI and data multiplexing are transmitted on Pusch (Part 4) --small block lengths
- MYSQL数据库底层基础专栏
- Idea remote breakpoint debugging jar package project
- 2020-08-23
- 03 FastJson 解决循环引用
- 单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
- 2021-01-03
- Learn the contents of 5g toolbox supporting NR through the NR resources provided by MATLAB
- [successful graduation] [1] - visit [student management information system]
- 01仿B站项目业务架构
猜你喜欢
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
单片机学到什么程度能找到工作,这个标准不好量化
Successful graduation [3]- blog system update...
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
How MySQL modifies null to not null
Vector processor 9_ Basic multilevel interconnection network
端午节快乐!—— canvas写的粽子~~~~~
Stm32f407 key interrupt
Matlab reads hexadecimal numbers and converts them into signed short
随机推荐
Epoll read / write mode in LT and et modes
STM32 general timer 1s delay to realize LED flashing
UCI and data multiplexing are transmitted on Pusch - determine the bit number of harqack, csi1 and csi2 (Part II)
Matlab reads hexadecimal numbers and converts them into signed short
Introduction to chromium embedded framework (CEF)
万字手撕七大排序(代码+动图演示)
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 3 --blinker_ MIOT_ Light (lighting technology app control + Xiaoai classmate control)
Vector processor 9_ Basic multilevel interconnection network
01仿B站项目业务架构
Uniapp realizes global sharing of wechat applet and custom sharing button style
Idea remote breakpoint debugging jar package project
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
MYSQL数据库底层基础专栏
内存数据库究竟是如何发挥内存优势的?
Exception handling of arm
UCI and data multiplexing are transmitted on Pusch - Part I
Successful graduation [3]- blog system update...
Stm32f04 clock configuration
JS foundation - prototype prototype chain and macro task / micro task / event mechanism