如题,送给自己做警醒。
, l4 f2 e8 u( @
. h5 h0 E; W% l# D( u* `2 N注意:
! l; Y+ @9 g6 L" _! Y5 w. j8 Q( e3 N, e" j- O" T" [
如果stm32f2xx系列上面的网址的代码要做适当修改。
" y2 e( v! v; `8 J: v A以下说重点:stm32f2xx系列,在调试中对GPIO的初始化,切记选择GPIO_Mode_AF模式,即端口复用。3 @; F3 Q# ?1 A- i# i \6 |1 O! ]0 q
' \7 k/ U' F. S' C: f- `4 p随后,要做端口映射处理,即调用:GPIO_PinAFConfig()函数。
' `1 q B, B1 C4 N: ?, p' C( x, l这2个方式不选择,timer是不会有反应的
& _6 x \0 h" Z! v8 ]' \6 y& S7 C
6 _+ o' ^ [7 q8 G3 ]/ U- q' w+ @本人是stm32f207,花了4天惨痛代价。
' o7 P0 I4 ?$ [0 B" K V& p# T9 Z n- ~7 z0 I% g( S
以下贴代码:
8 d2 n3 ?1 Y; H% [- /* Private define ------------------------------------------------------------*/
7 f% H+ q# W. A - #define ENCODER_TIMER TIM3 // Encoder unit connected to TIM3
' J) L4 L1 Q" G* P - #define ENCODER_PPR (u16)(1000) // number of pulses per revolution9 x3 `7 g1 H- ^1 G
- #define SPEED_BUFFER_SIZE 8
* S, d% k: d K, Q0 v9 ]
* }# _3 O5 A! H1 r- #define COUNTER_RESET (u16)102 q5 f/ b* A! ]% a
- #define ICx_FILTER (u8) 6 // 6<-> 670nsec
# _# E5 c0 }# X2 y& V
. D+ @* i% q9 S! q- #define TIMx_PRE_EMPTION_PRIORITY 0x0F3 F0 ^# y; F% B" m0 M' R
- #define TIMx_SUB_PRIORITY 0x0F i0 i* s' J) h' j; P( @
- ; x" x) ~+ ^8 H1 q1 {, s A& u, i
# y5 P; ?& D& p, u, H
. C; ]1 k5 @' `$ K% S" Y( D- *******************************************************************************; E! d% ]: U0 m0 _+ k) K
- * Function Name : ENC_Init, _2 \4 }+ p0 B( o' M+ h% z, J7 H
- * Description : General Purpose Timer x set-up for encoder speed/position ( w6 J$ }- n. }
- * sensors& s( I* x" S: y( D
- * Input : None
2 _* M7 @; w/ f" {' a1 M5 w - * Output : None7 C2 \( C! ~" p5 R% F
- * Return : None
$ t8 X+ C: T$ O( M% Z l4 L- l# n - *******************************************************************************/
/ M1 k% i6 `! x1 _% Y - void Encoder_Init(void)1 `9 A, W: p. N$ X/ a# j
- {, f. r/ b( t/ P5 }$ ?; X
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
0 }2 c- B6 M9 T$ a - TIM_ICInitTypeDef TIM_ICInitStructure;# a5 ]1 G$ H6 K( y5 P8 C7 O6 b
' C8 n% U7 Z% B- /* Encoder unit connected to TIM3, 4X mode */ 4 n Z9 U2 B5 u* ?, Q3 B( c. m( Y' \
- GPIO_InitTypeDef GPIO_InitStructure;' `( H. h# n/ y* Y. u
- NVIC_InitTypeDef NVIC_InitStructure;
M. @+ Y+ r& B+ e/ d3 ~
. \- c; O& e- h3 Y# t- /* TIM3 clock source enable */
/ x, T3 r7 C0 k! {1 n0 n - RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
5 A1 O4 Q& @! C* f - /* Enable GPIOC, clock */
9 o0 S/ K; o/ n2 O' W* P - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);7 ?, c" O j$ @: x. g, q
- //GPIO_Init, F. i$ H7 y% B! t, p% p
- GPIO_StructInit(&GPIO_InitStructure);. n }6 ^& [6 X- o. F
- /* Configure PC.06,07 as encoder input */
, t8 z8 I/ [, T0 c! j - GPIO_InitStructure.GPIO_Pin = INTA_PIN | INTB_PIN;( k# j' a8 J4 K- ^# S; x) W7 y
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //这个地方注意
- j+ _% q0 _! n- O - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
) W2 k3 c( V4 Q - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;$ S0 L2 O1 ` h7 J+ v! Q
- GPIO_Init(GPIOC, &GPIO_InitStructure);9 Q0 Z" R+ I m
9 T% ~7 B+ M z8 Y% x- /* Connect TIM3 pins to AF2 */
; v9 z5 O( ^9 d) d- I3 ]/ n - GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3); //这个地方注意
# f0 ~* p' b3 N& {: O/ p' a - GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3); //这个地方注意" m" c0 `7 k$ `2 r# [9 K
- ' n e( T2 M5 [# A* T- S& e
" Q+ C/ z" s& I. W- z! q- /* Enable the TIM3 Update Interrupt */
7 I) P7 t& G0 v" b- k6 u" C* q - NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
( C; A8 c* ]4 m: X8 M - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
m- H# e$ M( o - NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;6 @% \0 S4 b# N! l/ }+ ?% \. u
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;" S6 y9 q4 V0 A
- NVIC_Init(&NVIC_InitStructure);
4 ]+ o8 u! |& H4 N; E - # k ]) T1 i) A) T( B7 a
- /* Timer configuration in Encoder mode */& ]$ @6 e' Z5 ]9 s+ j l7 l
- TIM_DeInit(ENCODER_TIMER);. X5 p. ?; A6 s
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
' i" D& y, s2 j+ i8 i0 g
" Y7 h0 Y# S& I& P3 K# K- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling ; U! m8 h& W. Q* C i& G! }
- TIM_TimeBaseStructure.TIM_Period = 0x0F;//(4*ENCODER_PPR)-1; : @! I* x+ [$ f, z
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
0 f' m3 f5 v5 R' A4 b. V - TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
6 C9 [. W( U5 s5 Y- t+ @1 o - TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);8 U/ f! T& p" P2 Q6 q
1 f0 Y" y* y! o2 D- TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI1, / x# R5 e$ O; G. d8 {+ T, C- D! N
- TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);9 |/ n( g* x5 a/ M
- TIM_ICStructInit(&TIM_ICInitStructure);
; ?- {/ W7 A& u+ v* C% F" l - TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; //--?--
3 e; [6 T" i# ^ - TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
3 L# u1 Y- A& P5 z# h6 A1 Z
' ?: n7 K+ U8 o6 R; T- // Clear all pending interrupts0 L9 S. G# \7 a" {% h
- TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
( b: |' r$ e! F* t - TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);4 U Y u0 I( K- f4 ^
- //Reset counter
, ~- v% k# a8 Y& w+ _1 T, q - //ENCODER_TIMER->CNT = COUNTER_RESET;
$ U; w2 a) ], N' B' |1 f4 k# E - 8 D$ [5 ^) W3 y. Z, H" p
- ENC_Clear_Speed_Buffer();/ P+ d8 L% p$ K( D+ V: O; l
: i/ k2 a( F6 ?9 f" N7 A1 q" g& z2 s. h- TIM_Cmd(ENCODER_TIMER, ENABLE); $ s" ]2 ~2 ?; ^% _9 V( D- O Y
- }$ L6 v- Y S |
- 6 f+ M/ X6 @5 u0 {* g, x
- /*******************************************************************************
- k. _6 y W; [* w+ P - * Function Name : TIM3_IRQHandler/ `. \/ Z. R* a
- * Description : This function handles TIMx Update interrupt request.
" t; @! ]4 k& ? Y - Encoder unit connected to TIM3
1 @' h$ E! x; K5 {' J8 q - * Input : None
- F9 b" a' U( ^* P, Y - * Output : None
/ E+ b& P4 a. w5 H" C - * Return : None
; b* U `$ [. a% y - *******************************************************************************/* p% g1 Q$ B' m& ~' A+ ^* W/ P
- void TIM3_IRQHandler(void)
% ~8 O( I' }# r/ h0 A - { ) B4 U+ R. `" p2 t6 _% j. R9 ^
- /* Clear the interrupt pending flag */
" _9 k( t- T p* a2 a; j - TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);3 }) Z" B. m# I5 w, E6 e$ E
[+ l4 R, E& [. E+ [5 C* Q- }
* G" J. g! J4 N6 Q9 t- R - 0 c5 ~7 u6 B# ^/ H
复制代码
/ @3 o+ f/ j' j4 W0 U H( x |