频率2.097Mhz , TIM2定时1s触发一次中断8 S/ v3 Y1 _- y
1.TIM2初始化$ a( O0 u- D/ m; j
HAL_TIM_Base_Init>HAL_TIM_ConfigClockSource>HAL_TIMEx_MasterConfigSynchronization! n u3 V7 j+ t
3 u9 a! U2 i' @$ b- s- /**- c/ e$ B F3 C6 u) f6 o$ j# g
- * @brief TIM2 Initialization Function# S- D% T" `) `( J. f) c- I
- * @param None- W0 |, v8 u7 m, k) B
- * @retval None
/ c3 i; O( \$ ^ L - */
8 |1 @0 J. G8 u( k) g# Q. ~4 S; u - TIM_HandleTypeDef htim2;4 b$ c' t- G, F& h' g2 N& C$ }9 w
- void MX_TIM2_Init(uint16_t time) h) a3 x! C- J2 l, Q) E% D. X
- {3 `- h; a8 A3 P _
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};% M# a% ^$ e' O8 s2 E+ n
- TIM_MasterConfigTypeDef sMasterConfig = {0};( x' [7 |7 h1 V8 q% P% @1 A
- 6 B7 |" ?/ M1 n0 C. C; D
- htim2.Instance = TIM2;
/ O) L9 I) t4 W# O) l. N - htim2.Init.Prescaler = 2000;
, ]9 t4 Z- r' Q& l - htim2.Init.CounterMode = TIM_COUNTERMODE_UP;) v% g: A& F4 |: `) ~8 P
- htim2.Init.Period = time - 1;# l% r* l! U) k7 \% _) Q
- htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;& u* }2 d N' ]3 ^9 v
- htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
# h6 N0 \6 m# O- \/ ~: C - if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
2 C. h& N9 ^. k5 F+ n - {
1 j$ ^: l5 g1 j7 t; V" m - Error_Handler();
9 j/ t& y2 p I1 e% L1 c. M- C - }
5 G$ p2 D) s" l- ~ - sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;1 [% j: m0 G" `9 r; ]
- if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)9 W$ g' T6 X$ G5 x- G6 g# _7 u
- {
& z. G5 ^- _, p, C - Error_Handler();/ k- W0 w/ c# B2 ?0 v' M' ]
- }
6 {" g" S2 ^/ v( Z# m - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;: P9 o2 n% S2 I \, T3 m3 ~7 f
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
) ~" D% ~% T) y0 v- ` - if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK), y3 @/ T8 \9 _( b! T0 A* \
- { H+ t* {# @1 O) B
- Error_Handler();
/ y6 n* h7 f4 u9 [$ T7 J ~ - }7 R" G7 a2 n7 M; J2 _$ o! U
- }
复制代码
) ~- u" D' j6 j% l% _0 c2.TIM2 中断处理函数7 L/ w F! {3 Y( L: x6 Y; \7 ]
- /**% e+ U" J3 |' f' B/ J
- * @brief This function handles TIM2 global interrupt.0 y$ x5 \3 y1 M4 L2 q6 g4 v! W F
- */9 [/ d' I2 Y9 ]! K0 V' ^# [
- void TIM2_IRQHandler(void)
2 a. h& m5 t" J' b, A( z7 \& \5 P - {% m3 r2 S9 J" G1 c$ W1 }! n; c
- HAL_TIM_IRQHandler(&htim2);
% l8 m; I6 Q$ s) ^ - }
复制代码 7 E$ x$ _$ ~( T" i/ C
3.主函数main
1 G+ ^& L: ~% P& ?' }3 [# a2 L- int main(void)
% F' K/ Y& b1 Q2 D$ z - {
1 x# C( W& a( J! K) L4 H) x - HAL_Init();0 y9 B% a3 k7 @2 i
- /* Configure the system clock */0 k1 ?1 `( V& E. o$ ^- x& v
- SystemClock_Config();: p" d2 X3 \ a& l: F5 B, R
- LED_Init();//LED初始化4 [; n( a) l$ W0 ]$ P$ Y
- MX_TIM2_Init(1001); //1s一次% E# a1 A6 e! `) M; b% [# V
- HAL_TIM_Base_Stop_IT(&htim2);
0 J" S9 m: Z1 L# e; H$ i - HAL_TIM_Base_Start_IT(&htim2);+ L0 F, g3 I G9 b9 s* \3 I
- / i3 H1 }( e4 n9 P# o' F
- while(1)5 m8 c" s4 u: X' s
- {}# z( E6 ]+ |3 f. h
- }
复制代码
' l' m( h3 f7 j2 `4.TIM中断回调处理函数) d7 U1 k0 x" I ?, I6 A
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
7 G0 _$ P* c2 N% r! t - {
) E {3 X5 x/ E, ]8 k+ H. _& N - if(htim == &htim2)
" p/ L' w( I1 Y$ ?! e4 x - {
& P* v1 ^9 I z, W( Q8 ? - HAL_GPIO_TogglePin(GPIOB,GPIO_PIN8);
- i6 m4 \7 t) P3 i% D - }% h7 k8 j: @ L- p2 k% W
- }9 ^+ L4 { K; S" Y/ @
复制代码 . i) U: s: T# k1 O; c9 U
|