你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

基于STM32F1标准库 编码器经验分享

[复制链接]
攻城狮Melo 发布时间:2023-5-14 17:32
初始化代码- b7 u- o4 N' k- q
  1. void Encoder_Init_TIM3(void)$ i7 m2 a6 Y* }* B4 X
  2. {! ~8 P, B! K6 c& G% A
  3.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
    7 g4 y8 ^* J2 R: U
  4.         TIM_ICInitTypeDef TIM_ICInitStructure;  ; b/ L3 J1 ~& \9 @( S$ @
  5.         GPIO_InitTypeDef GPIO_InitStructure;4 s/ P0 I9 f" ?' @  J
  6.        
    6 C. z3 c6 Q4 {5 @+ ?7 t
  7.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//?????2???
    ( x9 ?# A" k( @: C
  8.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//??PA PB????1 i8 F/ [! ]4 h+ V
  9.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    ( ^. S) N$ \: f8 ~# x; \1 ?
  10.         # ~7 z( U4 t" G( p- s9 G. Q& _( u
  11.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;        //???? PA15# y1 D4 g; e; e6 {
  12.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????
    5 `8 w) S1 C) ]  N' [+ {
  13.         GPIO_Init(GPIOA, &GPIO_InitStructure);                                              //?????????GPIOA& k4 K; O7 F" j
  14.   
    0 m4 P" R( \" b
  15.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;        //???? PB3: C5 d. i& l8 I& b: g1 i) g
  16.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????8 Z: r# m2 G* r) }# t/ ?. ]1 I
  17.         GPIO_Init(GPIOA, &GPIO_InitStructure);                                              //?????????GPIOB
    2 Y$ G7 G% m7 I" d
  18.         5 a; F+ y1 O$ {8 n% x7 _
  19.         TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    8 {/ ~7 _4 D  c* q
  20.         TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // ????
    4 Z4 I2 J. @. \4 R* q. f0 S1 O% |  W. k, M
  21.         TIM_TimeBaseStructure.TIM_Period = 65535; //??????????
    - T4 l& t- P* g/ B' {5 R0 R
  22.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//??????:???* D6 P( C: I8 {' x1 {! K( H
  23.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//??????
    7 f4 [5 Y( y6 p6 {" _# S
  24.         TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  //??????20 g) \8 v) }. p( s# Y$ ^
  25.        
    7 w5 _" Z( R* `0 u, Z7 w# l/ T5 Q+ C
  26.         TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//???????3" _) D: U" u: `' W, x3 H
  27.         " y* s. w; t9 S$ i
  28.         TIM_ICStructInit(&TIM_ICInitStructure); //?TIM_ICInitStruct ?????????????, q8 x$ S5 N+ r
  29.         TIM_ICInitStructure.TIM_ICFilter = 10;  //???????
    . E5 L! k) y( B0 k
  30.         TIM_ICInit(TIM3, &TIM_ICInitStructure);//?? TIM_ICInitStruct ????????        TIMx
    1 T" P8 Q; Y# E8 z+ E( a/ z3 W# G. L4 N
  31. " z( R0 q6 I+ s
  32.         TIM_ClearFlag(TIM3, TIM_FLAG_Update);//??TIM??????
    ( ]' C& e% @0 G2 o
  33.         TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);//???????
    / B! x4 v% B, I
  34.         " S2 V8 t+ i/ T6 [6 ?
  35.         TIM_SetCounter(TIM3,0);
    7 o/ P7 b% n% D
  36.         TIM_Cmd(TIM3, ENABLE); //?????2
    , e$ P+ z3 k' r7 r/ \5 w$ P
  37. }- A- N% _2 b  _4 W8 G: B

  38. , I, ~* ?- }  z( N- @  R/ w
  39. void Encoder_Init_TIM4(void)
    9 n5 L% e& r0 a5 {% E& a! k
  40. {
    7 R$ A! Y; |, W) n" v
  41.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
    , O3 ^- ~4 @- K* ~
  42.         TIM_ICInitTypeDef TIM_ICInitStructure;  . r+ j: C4 C7 [* e. m& G
  43.         GPIO_InitTypeDef GPIO_InitStructure;3 v& G5 R0 K' a9 [, ?
  44.        
    + O9 i* E+ Y9 f9 E
  45.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//?????2???
      b( A! T3 [1 z, X' Z( l
  46.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//??PA PB????
    8 [! G% t% d! _: {, I, g9 X) a2 R
  47.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    ( d( }7 h) j$ {$ b+ T9 A, Z6 `/ W0 t
  48.        
    1 T6 X; j5 f) n3 \: e% U
  49.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;        //???? PA15) {6 W. k2 q7 m& [; n
  50.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????0 u: O8 |/ n1 k3 W4 P* C# @2 B2 h4 o3 N
  51.         GPIO_Init(GPIOB, &GPIO_InitStructure);                                              //?????????GPIOA6 c1 I- I( }+ u- v( b. Q
  52.   & Y8 ]! J1 m8 E
  53.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;        //???? PB33 c* s, v7 Y+ T$ z9 ]' o$ j: \) t7 A: i0 q
  54.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????
    % u. h/ l* X& N+ _; A; p9 U
  55.         GPIO_Init(GPIOB, &GPIO_InitStructure);                                              //?????????GPIOB
    7 T; O0 P) E! d% `
  56.         5 b. D: r6 \3 _1 U& i# E0 s
  57.         TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);! M2 u/ x' ^8 f, e/ H8 H0 r
  58.         TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // ???? 2 d5 M  G, Z0 g& O1 a4 u# P6 g
  59.         TIM_TimeBaseStructure.TIM_Period = 65535; //??????????% z! t- r5 H# k
  60.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//??????:???0 D) u/ e, @9 y8 A" N6 B( t
  61.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//??????
    ; R1 c* b  D1 o( B# D  p
  62.         TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);  //??????2
    * y* u6 H8 r) Q! R: y. ~% [
  63.        
    ( `% z! O! ^4 l/ ]
  64.         TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//???????3. P" @- U8 Q$ P7 A( a, F
  65.         : ]. x& u, f% E. D
  66.         TIM_ICStructInit(&TIM_ICInitStructure); //?TIM_ICInitStruct ?????????????
    " `. }0 ?9 H: Y. x6 U
  67.         TIM_ICInitStructure.TIM_ICFilter = 2;  //???????& h2 j% p7 ~' s, r1 {' g
  68.         TIM_ICInit(TIM4, &TIM_ICInitStructure);//?? TIM_ICInitStruct ????????        TIMx8 c7 J, v8 n1 H( q3 A6 B: t* T
  69. . B: A8 |1 g. E
  70.         TIM_ClearFlag(TIM4, TIM_FLAG_Update);//??TIM??????3 g. A& \# w0 z  g& p2 f1 V
  71.         TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//???????- B" h3 s; Z5 M3 w) a. U2 r0 Y- Z
  72.        
    - a) c% p3 {1 X, H$ ~' I
  73.         TIM_SetCounter(TIM4,0);
    6 [" u$ ]( r9 b9 l8 ]' j! \
  74.         TIM_Cmd(TIM4, ENABLE); //?????2) t* x3 Z& T- B0 l
  75. }
复制代码

0 @( m6 K- v* D! A9 \4 O读取函数
4 r1 x$ s( {6 i, A9 O
  1. int Read_EncoderB(void)
    $ }" G+ x/ E) T  {# Z
  2. {0 k0 z* d5 j, `& Z6 r2 L& _
  3.         int encoder_num;
    $ Z5 p, d; w! X  T
  4.         encoder_num = (int)((int16_t)(TIM3->CNT)); 0 [, \9 I) J* {2 ~
  5.         //TIM_SetCounter(TIM3, 0);. h; f$ L6 ^+ o7 p& k% L! T
  6.         return encoder_num;: x; y6 J' h! i; Q
  7. }
    " l) u* n* [& F- f. C
  8. int Read_EncoderA(void)
    1 v. r$ w, L. ^
  9. {
    . q5 j6 `* F5 @* m
  10.         int encoder_num;
    # G4 z/ w. G6 ^
  11.         encoder_num = (int)((int16_t)(TIM4->CNT));
    . I% j9 c% W" G, }$ x) T
  12.         //TIM_SetCounter(TIM3, 0);! j& A( K* e/ J$ t" E8 u
  13.         return encoder_num;4 j: i6 M( ?  R$ x$ H8 S
  14. }
复制代码

: U8 G$ u5 j" w1 [" L' \置零函数
$ M4 |2 ^9 }" h* j0 t0 f

  1. 2 m4 T0 `7 Y! ~0 F# b# g
  2.         TIM_SetCounter(TIM3, 0);
    " j- `% M: x. O* k3 _
  3.         TIM_SetCounter(TIM4, 0);
复制代码

# U3 U/ e& y# \4 f. W————————————————, _3 d# r/ E* S( k
版权声明:阻容降压
# U+ r" I8 i/ ]4 o. ^. f如有侵权请联系删除
' a( w3 B# H( E; {* N! w% u4 l2 f9 Y; C

( W) n1 c& q3 Q# r0 A8 `3 G
收藏 评论0 发布时间:2023-5-14 17:32

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版