当前位置:网站首页>STM32 - PWM learning notes
STM32 - PWM learning notes
2022-07-26 03:00:00 【The poorest are beggars and immortals will always come out】
One 、 Can produce PWM Timer introduction
STM32 In addition to TIM6 and 7. Other timers can be used to generate PWM Output . One of the advanced timers TIM1 and TIM8 It can produce as many as 7 On the road PWM Output . And universal timers can also generate up to 4 On the road PWM Output .
Two 、 All the way PWM The corresponding channel




3、 ... and 、 Related configuration
1、 Channels used IO Mouth configuration ( Configure according to the selected channel )
// Configure output I/O Pin
// Set this pin to multiplex output function , Output TIM3 CH2 Of PWM Pulse shape GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexing push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);// initialization GPIO2、 Timer configuration
// Timer configuration
// initialization TIM3
TIM_TimeBaseStructure.TIM_Period = arr; // Set the value of the auto reload register cycle for the next update event load activity
TIM_TimeBaseStructure.TIM_Prescaler =psc; // Set as TIMx Prescaled value of clock frequency divisor
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // Set the clock split :TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM Upcount mode
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // according to TIM_TimeBaseInitStruct The parameter specified in TIMx Unit of time base 3、PWM Output mode configuration
Among them, we mainly pay attention to the first 、 The third configuration .
The first configuration :
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1 / TIM_OCMode_PWM2;
Configure the output comparison mode , When it comes to PWM1 when : The current value of the counter Output the effective level when it is lower than the set value
When it comes to PWM2 when : The current value of the counter Output the effective level when it is higher than the set value
The third configuration :
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
Configure whether the effective level is high level or low level ,TIM_OCPolarity_High: The active level is high
TIM_OCPolarity_Low: The active level is low
// Timer channel configuration
// initialization TIM3 Channel2 PWM Pattern
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // Select timer mode :TIM Pulse width modulation mode 2( Model 2 : When counting, when the counter value exceeds the set value, the effective level is output , Output invalid level when it is lower than )
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Compare output enable
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Output polarity :TIM High output polarity ( The high level is the effective level , Or is the low level the effective level )
TIM_OC2Init(TIM3, &TIM_OCInitStructure); // according to T The specified parameter initializes the peripheral TIM3 OC2Full configuration :
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// Clock configuration
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Enable timer 3 The clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); // Can make GPIO Peripherals and AFIO Multiplexing function module clock
// Remap
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3 Partial remapping TIM3_CH2->PB5
// Configure output I/O Pin
// Set this pin to multiplex output function , Output TIM3 CH2 Of PWM Pulse shape GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexing push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);// initialization GPIO
// Timer configuration
// initialization TIM3
TIM_TimeBaseStructure.TIM_Period = arr; // Set the value of the auto reload register cycle for the next update event load activity
TIM_TimeBaseStructure.TIM_Prescaler =psc; // Set as TIMx Prescaled value of clock frequency divisor
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // Set the clock split :TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM Upcount mode
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // according to TIM_TimeBaseInitStruct The parameter specified in TIMx Unit of time base
// Timer channel configuration
// initialization TIM3 Channel2 PWM Pattern
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // Select timer mode :TIM Pulse width modulation mode 2( Model 2 : When counting, when the counter value exceeds the set value, the effective level is output , Output invalid level when it is lower than )
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Compare output enable
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Output polarity :TIM High output polarity ( The high level is the effective level , Or is the low level the effective level )
TIM_OC2Init(TIM3, &TIM_OCInitStructure); // according to T The specified parameter initializes the peripheral TIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); // Can make TIM3 stay CCR2 Pre loaded registers on ( effect : When changing the duty cycle , Can it take effect immediately )
TIM_Cmd(TIM3, ENABLE); // Can make TIM3
}边栏推荐
- 26 points that must be paid attention to for stability test
- Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
- Case: using kept+haproxy to build a Web Cluster
- 织梦提示你设定了字段为联动类型如何解决
- Basics - network and server
- 规范自己debug的流程
- What if the test / development programmer gets old? Lingering cruel facts
- Win11麦克风权限的开启方法
- assert _Aligns
- 如何用U盘进行装机?
猜你喜欢

Detailed explanation of extended physics informedneural networks paper
![[SQL] CASE表达式](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[SQL] CASE表达式

【C语言】深入理解 整型提升 和 算术转换

From the annual reports of major apps, we can see that user portraits - labels know you better than you do
![[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)](/img/b8/56c4541602c5a6e787e2455f80febd.png)
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)

The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track

Anti electronic ink screen st7302

Arthas view the source code of the loaded class (JAD)

Parallelloopbody in opencv

【方向盘】使用IDEA的60+个快捷键分享给你,权为了提效(Live Template&Postfix Completion篇)
随机推荐
Longest Substring Without Repeating Characters
如何加速矩阵乘法
测试/开发程序员老了怎么办?挥之不去残酷的事实......
Image recognition (VI) | activation function
Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!
GoLang 抽奖系统 设计
[steering wheel] how to transfer the start command and idea: VM parameters, command line parameters, system parameters, environment variable parameters, main method parameters
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
案例:使用keepalived+Haproxy搭建Web群集
OxyCon 2022 网络抓取前沿大会即将开启!
How can users create data tables on Web pages and store them in the database
基础知识-网络与服务器
当点击Play以后,EditorWindow中的变量会被莫名其妙销毁.
Longest Substring Without Repeating Characters
Annotation development management third-party beans
[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
Annotation development
Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法
[C language] deeply understand integer lifting and arithmetic conversion
[translation] safety. Value of sboms