|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟用STM32定时器编码器模式计算减速电机AB相脉冲个数,目的是想通过 AB相脉冲的上升沿和下降沿触发,然后计数器加一,那么如果检测到一个周期,有AB两相,两个上升沿两个下降沿的话,计数器就会四个 加一 。现在的问题是检测出来总是出现两次加一,只是检测到了上升沿,感觉没有检测到下降沿呀。希望论坛里的大哥们帮忙解答一下。4 r1 {9 \* ^! {. o. G- n; t7 } E
+ C' M' b; Y. Y+ E! M' Yvoid TIM3_Int_Init(u16 arr,u16 psc)
/ i; P T8 h# N. l; C2 a0 N{
: |4 G% U% c. A7 z6 X) D TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;, n @( [: b" b7 s8 k2 _8 Q% ~2 e
NVIC_InitTypeDef NVIC_InitStructure;
8 p- l. K5 U# I/ A# O
1 G& X$ B* e6 Q! Z3 ` RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
s# L7 J+ b- e* R% k
4 b1 H* k8 x6 ?+ ?* h //定时器TIM3初始化/ E4 c, G0 r1 `0 ]
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
, u8 a- M/ j; j( r) }8 N) j1 J TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值( D9 N& Z0 d8 K9 l- y
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
, g3 c2 |; V$ t4 o4 H TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
9 p0 s ^6 \2 `+ n TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位 ) p# X* S1 h+ \7 P7 N4 D5 m9 T0 N
// TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//选择TIMx从模式
# y, w( C" M) ?% ], x TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断! z3 u% M9 {% l# e
' J5 P! X7 Z7 D" s4 a. \
//中断优先级NVIC设置
1 z5 ^3 `. \, z NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断- Q- x! g0 p/ `2 m
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级3级
+ D: `4 ^' H( d6 O# r1 F NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级; w# P! O: K# G* a
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
* w& n& k$ m H. j% Z# m2 i; v NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
8 a+ p/ B, M8 a4 C
( M+ E/ l$ S( a( o+ A1 x6 q% k //TIM3_Encoder_Write(65416) ;1 L( V Y! m0 O5 P3 Q' [- n
% w# m' k2 C# s/ L8 l TIM_Cmd(TIM3, ENABLE); //使能TIMx 6 }, Z4 Z2 z, `4 O3 a% x
}
7 n- t' b+ H9 E! J1 t- ^; @& _3 o8 e: B& K- g) n
* f3 l ^4 g; f. u! _2 |# d3 ~1 R/ A% T7 h4 y
/**********TIM2_Encoder******/
5 ]9 r! G" U. r @* \3 j4 M. |0 F
' J( l/ ^/ p9 T6 o1 A: g) `void TIM2_Encoder_Init(u16 arr,u16 psc)
& Y/ T$ S; z0 |, a! N{
& y6 x( ?4 ?& j. B TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;& h* D; O& ]: p8 `; V4 B5 t
NVIC_InitTypeDef NVIC_InitStructure;
. w4 l2 g7 m+ Y8 f TIM_ICInitTypeDef TIM_ICInitStructure;+ P$ N3 K5 j K6 m' E
0 ^+ a+ o/ z& j4 w- d RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
; U L' T3 x: s2 T7 g
( g% _4 n0 Q' I' L2 N% } //定时器TIM2初始化
; n( C2 k, O% F: }# c l' ?" A% \ TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
. C& T; o( f7 ]7 J/ v ~5 j$ d4 V1 o5 S TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
: _+ T: _8 [$ H7 T/ k: A TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
. D! x2 h X* C% r% i TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式, d5 a- [1 n) i$ g" \% m3 Q7 R( W/ x1 q
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
& q1 B$ T9 v' |# A0 m- O+ a //TIM_SetCounter(TIM3, 0xFF88);//设置TIMx自动重装载寄存器值
5 [4 |6 u. v, x( ]& M5 I
5 t9 `. v5 f5 x Q' ]$ Z TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12,
( D4 `! k9 Z# L0 D8 H TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
5 z" B* `) W+ W b6 F TIM_ICStructInit(&TIM_ICInitStructure);9 u7 T* Y$ O0 [
TIM_ICInitStructure.TIM_ICFilter = 6;. l. u! n7 A7 `! [" X; G# D. m2 T
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
( c. R. z* i% |6 K* { TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频 J; E, ?7 G( d& j$ ^$ ^
TIM_ICInit(TIM2, &TIM_ICInitStructure);
! A! P( K, ]0 X. f+ p //TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//选择TIMx从模式% E2 l/ s ~! }: A9 X8 x" I. i6 {
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
/ V3 X: W* N+ s8 ]; q9 w TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
- n& W8 \$ \( `/ ] //TIM2->CNT = 120;
/ V: r( v+ o+ d0 S //中断优先级NVIC设置' \0 T8 \' I9 E4 {: V3 F% w
NVIC_InitStructure.NVIC_IRQChannel = TIM_Channel_1|TIM_Channel_2; //TIM3中断
2 n+ e U( B3 J NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级3级! q* ~& q# _( Y) N
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级; l h9 z7 I* x3 N% q
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能4 |! ~! {8 F1 N( g) h
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
4 J( H8 h7 K+ |1 W, U6 G ^! R
) d7 [) @" p6 ?) g, T# F //TIM3_Encoder_Write(65416) ;- f' F2 L' H$ g) E( N
9 X. P4 N/ f- _( \! n2 Y TIM_Cmd(TIM2, ENABLE); //使能TIMx
+ G# ]; S( X1 P* o" L9 \* I$ r( E& R}+ v; w0 J3 c* \* m, O; u8 j# X
$ ~6 x' s! y2 T$ p! Z# a% k7 f$ u/ f" U# j" a2 m
//定时器3中断服务程序
" \+ I( [# C! d; x( N9 H3 U6 D- Rvoid TIM3_IRQHandler(void) //TIM3中断" S' }4 \9 W4 P6 C. F0 A) R h H) \6 r
{! o3 l2 P5 @& z5 p; C
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否4 S H9 r- u) l9 j d* v; g" W
{
7 K9 B4 o% E {* @# B TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志
0 U9 d c- K4 @6 M/ Z$ J TIM2->SMCR ^= 0x8000 ;$ E: M8 f, \1 Z+ D& }
}- B1 v' P0 F1 S: J
EncVal = TIM_GetCounter(TIM2);& _1 y% Q3 v* i5 F& @
; P m3 ?6 ^0 i u' w& ` m$ ] if(EncVal>=240)
0 j0 G3 i6 e* B( U {
8 e8 m) ^# g% [* N EncVal = 240;
( d: ~, Q2 ]( C% ?/ Y- ~4 L }" N8 K0 p, B7 {) k9 p( S+ U1 C
LCD_DrawPoint(i, 5+EncVal);
' g, V9 [/ ]( f/ ^/ Y if(i==320)7 w U& g2 p8 _5 E
{ ! H! m- A' M8 _' Z
i =0;' g0 ]) Z+ `3 S: \/ T. u" P. X
LCD_Clear(WHITE); H8 v9 R$ O3 K. |8 y4 b7 u! {1 b
coordinate() ; //横坐标 1 a8 U# P9 |' P2 O# {0 P
2 T( |, d. r6 P4 b9 T5 s4 T
//delay_ms(50);
% M4 U+ P# I& M }
e! ~& d& w8 `* c a( L i++ ;
* s+ O8 |4 a& V. n* A& Q Y TIM2->CNT = 240;
T+ ~ H# \$ d: N Frequent() ;
/ F2 ?4 C. @, d1 W8 c1 ^! R //fre = 1000/(90 /(720*100/72)) ;
{" M# Z) ?. ?; J1 Z4 b* Y0 ] //sprintf((char*)speed,"speed : %d ",EncVal );//将LCD ID打印到lcd_id数组。" X: U. Z) j/ |; r9 e# Z' h
//sprintf((char*)lcd_id,"LCD ID:%04X",lcddev.id);
8 X# X9 x; s) r. _6 j5 a) X //LCD_ShowString(200,200,200,16,16,"EncVal");0 U& L2 Z$ I/ v2 L, H, T' K
LCD_ShowxNum(250,200,EncVal,5,16,0);
' \+ u3 ]3 x3 L6 B printf("%d\r\n",EncVal) ;
& H; F8 o ~# w/ Z9 G TIM_SetAutoreload(TIM1, 0xFF88);//设置TIMx自动重装载寄存器值
' x3 y6 E( ^6 C* v) Q }
; V, L8 |1 Y/ Z- C4 x& G* O}
( N' a: P$ F) X' ~) k0 ~9 ~% H) e, n, c
|
|