频率2.097Mhz , TIM2定时1s触发一次中断! U6 S: C3 m) X; \
1.TIM2初始化( t8 T! P0 Y! B; ~2 a! V8 R: N" p
HAL_TIM_Base_Init>HAL_TIM_ConfigClockSource>HAL_TIMEx_MasterConfigSynchronization
7 W B" ~" C) B. \/ s/ b ^5 b
4 _+ b" Z( ^0 }* p0 `- /**& S, q; }8 P: i
- * @brief TIM2 Initialization Function+ `% D5 G$ @( l) t) z0 d/ S
- * @param None2 A# _0 H( r0 Y X
- * @retval None
9 {/ Q( w) w/ E( t8 d3 W - */# W: n+ w9 I. j5 X3 q
- TIM_HandleTypeDef htim2;
3 J: G1 l# S. K1 g - void MX_TIM2_Init(uint16_t time)
, F, L# U6 p- q+ W8 i - {
0 V M- f7 w2 L; e d* f% }% E - TIM_ClockConfigTypeDef sClockSourceConfig = {0};
. ^8 j) B, Y6 M) d - TIM_MasterConfigTypeDef sMasterConfig = {0};+ ?1 b1 ]6 F' T: Z" a
- - p+ P3 m. l% n
- htim2.Instance = TIM2;9 e! b. U2 ?; l3 @8 T6 n; K; g
- htim2.Init.Prescaler = 2000;: L# r, P+ ?$ d# L, ?: @& E
- htim2.Init.CounterMode = TIM_COUNTERMODE_UP;0 E6 T- i' P3 b$ E5 d' i% E
- htim2.Init.Period = time - 1;8 w* q) R, ]# I
- htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
/ X3 p1 z, F" O9 y2 Y ?0 i - htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;. ?- E; G( m7 R( b
- if (HAL_TIM_Base_Init(&htim2) != HAL_OK)4 m9 j5 \% L# v5 K1 p3 d- R U9 q1 ~5 D
- {
7 o+ n4 |2 [8 ^% G( o7 r - Error_Handler();; A6 t+ \8 B! C( N; P1 j( Q
- }
9 s3 E t y* K( R, m- g - sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; C& B1 i2 I3 S
- if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)" z7 z( y) E% f3 w- f2 p+ V% U
- {
9 ?- c0 i- t- F' O7 W! J - Error_Handler();
7 d. ^7 r7 G6 j! _' x2 [ - }* I: I' q! I+ d" ^$ V$ y
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
( p/ R7 u" s3 O, p# A$ y5 \: D - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
5 Y7 \- @% W" Y- d' {+ i - if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK), w1 y9 ^+ ~2 b. |! S7 @% d0 A
- {
8 W, \; D: E. H- P - Error_Handler();
3 @5 W4 e. n5 A. L8 f- g - }0 p e2 z7 F% v/ }& j
- }
复制代码 - c1 X3 X1 Z0 i3 M# i C5 I/ h
2.TIM2 中断处理函数5 b/ Y' y- v( ^% S. j# Z3 T( t3 C
- /**
$ Q s$ A! R! o# M$ ]& \ - * @brief This function handles TIM2 global interrupt.. m0 Z5 F$ {* d6 E: ^8 i
- */2 s$ ~* u* U" G2 m& {
- void TIM2_IRQHandler(void)4 w0 b7 p3 l5 d" w+ ]! Y( u" H
- {8 a. W: q5 W9 T/ V" q
- HAL_TIM_IRQHandler(&htim2);
) B. I6 q7 `( E' b0 i' _ - }
复制代码 8 v" ]8 i% P" ]
3.主函数main
( o* M) m+ Q" S; k: f- int main(void)1 H1 f% k+ d, v
- {
& R4 H" G/ S' z7 K3 A; @$ r8 w - HAL_Init();4 j" F* g: b: ~- }
- /* Configure the system clock */
& s- C9 \$ [, Q" J5 I - SystemClock_Config();2 D N1 N2 w! R1 y+ Z
- LED_Init();//LED初始化. p3 ]6 U n- \. k
- MX_TIM2_Init(1001); //1s一次
7 l# h* Q& q q' G" ? - HAL_TIM_Base_Stop_IT(&htim2);
) N! m5 k- g: @$ G/ Z- `7 o) W$ c - HAL_TIM_Base_Start_IT(&htim2);
6 X* V, U4 k5 Y - # {% d* b7 ^7 K6 C3 T5 T1 i3 l
- while(1)) I* D. e9 Q: D7 B. @
- {}
7 x6 H/ g! e. { - }
复制代码 ; B' P9 y' O7 s/ ]
4.TIM中断回调处理函数
& S9 c4 Q' {1 H) W4 o# w) J- [" n- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)% {* r5 Y; D; ^% V3 ^1 g
- {0 r0 j( o5 \2 q6 `" Q# e
- if(htim == &htim2)
' L0 O. [" x6 h9 { - {
* ^+ R% b" U3 [0 ` - HAL_GPIO_TogglePin(GPIOB,GPIO_PIN8);5 a# X# h H3 @
- }9 b) c2 W9 ]. @: H6 {
- }% F2 t4 r) @- D0 N/ |
复制代码
; g$ m" G9 l% ^6 F# a |