想来定时器PWM实现呼吸灯也是挺有意思的一件事情。其实呼吸灯的原理还是挺简单的,就是不断的调整信号的占空比,来实现信号灯的逐渐变亮变暗的效果。 查看Datasheet,正好板卡上的LED脚PA5对应TIM2_CH1,这样实现起来就方便多了。 这次必须说一下时钟,程序使用的传统的HSI 16M时钟。预分频器16,这样实现100HZ的频率就能计算周期和占空比了。剩下的就都是套路了。 引脚初始化: - GPIO_InitStruct.Pin = GPIO_PIN_5;
' D. c3 x8 B% s; e3 _9 O - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
" c/ h% m7 d3 z8 r7 h - GPIO_InitStruct.Pull = GPIO_PULLUP;
6 H2 }5 O" c9 j! f- N4 o1 f - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;- P; `- j8 v3 |( g- A
- GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
# y) o7 u( v. F/ z - 0 d% k4 A( E4 D9 I+ e. C6 V
- // GPIO_InitStruct.Pin = GPIO_PIN_5;
- \- Z9 p- g' Z6 i' L - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
复制代码
" T2 o7 B3 Y* |7 P8 G4 h定时器初始化如下: - __HAL_RCC_TIM2_CLK_ENABLE();1 I8 T+ M( |! V2 j# D# e8 b
- // TIM_HandleTypeDef htim1;: Q- N" b0 } B7 W4 z
- TIM_MasterConfigTypeDef sMasterConfig;
# P' p1 A' E0 r+ U5 ^5 R - TIM_OC_InitTypeDef sConfigOC;
9 m! v o1 N$ s/ h" C$ P; u3 } - TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;2 _( U3 k6 z" \3 B' Z# ^
- 1 V \7 S- `7 [# M) F
- /* USER CODE BEGIN TIM1_Init 1 */
$ L& r* h4 j1 C8 Q0 J7 ~$ q7 ~# K. ` - ; }7 ^0 B4 z0 p7 j' l& m
- /* USER CODE END TIM1_Init 1 */
+ v/ r. l7 W6 v( F$ M" T2 A - htim1.Instance = TIM2;/ }$ B/ ~' k' U0 I# a, [7 s
- htim1.Init.Prescaler = 16;//PRESCALER_VALUE;7 o7 i5 Y8 \" B( ~# u
- htim1.Init.CounterMode = TIM_COUNTERMODE_UP;9 q# ~7 P ?2 n; |4 u# z n" \* P
- htim1.Init.Period = 10000-1;//PERIOD_VALUE;' u. J* D' H9 W2 t$ C. r
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;, @4 D) s( c2 x* C2 \& Z/ Z$ s; Z
- htim1.Init.RepetitionCounter = 0;$ F1 D( a w$ v
- htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;, c$ ?! z" l( c1 ^6 Z9 o
- if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
8 n' k0 ?) g* Y s0 K) U - {
# C( }; |1 ?& i! U9 m9 e' i3 u# K$ [ - Error_Handler();* `3 ?! H0 v( {% `# O" O' u4 ^
- }8 W' i& Y# f4 G( Y
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;; Y5 F% Q' s- b- J" Q) s
- sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
7 M- \& m$ k$ @ - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- s w* y4 f7 X0 {) } - if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
5 m5 V6 o# e9 W6 ` - {. W$ Z. Q+ b+ U& p
- Error_Handler();1 B& u( F' p1 q
- }3 s: f5 w x) L
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
+ y1 J: h7 M- ]4 e - sConfigOC.Pulse = 5000-1;//PULSE1_VALUE;0 Z4 J: p$ g: d$ I3 m
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;* E7 u( Q9 J3 ~" h& L6 l
- sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
! X) K K4 m4 { - sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
+ s3 J/ ^# \* N0 U - sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
+ M+ W" c& V c4 x" I/ H0 H. g - sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
& i" F# i# H' j' \; d( t* Z7 T4 d - if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)7 q& u) K$ N, x; Y# O% f
- {
5 p0 @/ B1 k. e5 U - Error_Handler();
3 {5 o8 L8 H2 N - }
0 @0 I' S' {6 h - HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
复制代码 . N" G! q7 l8 r# [8 }4 b! b" V/ A
在while循环中不断的修改占空比,部分修改如下: - Value+=200;
" ?7 Q+ u6 j5 r - HAL_Delay(20);" w& B Y+ m- _3 Q M" E$ Z
- PWM_Chang(Value);; E8 M- N# k1 ]
- if(Value>=9000)
& F" j2 M$ _1 c) j+ r - flag =1;
复制代码 ) h5 L, Y% ]( g8 s. H
最终效果如图所示: ( U) {8 o, D5 I6 a
8 a: y1 f1 L. @- D; I S7 f |
不如把示波器的波形图也贴上来
是的