|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟用STM32定时器编码器模式计算减速电机AB相脉冲个数,目的是想通过 AB相脉冲的上升沿和下降沿触发,然后计数器加一,那么如果检测到一个周期,有AB两相,两个上升沿两个下降沿的话,计数器就会四个 加一 。现在的问题是检测出来总是出现两次加一,只是检测到了上升沿,感觉没有检测到下降沿呀。希望论坛里的大哥们帮忙解答一下。% B$ ]# u9 R% j$ L+ T* |
4 S1 t8 Z9 x$ F
void TIM3_Int_Init(u16 arr,u16 psc)6 Q ~( M) k8 h3 \: Z
{
8 l) k* z3 I/ e0 y0 K# q. G4 C0 \ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
" I% X) Z0 v% N+ G& K NVIC_InitTypeDef NVIC_InitStructure; q. k8 g: m* [* A$ X
, Z e, m- S T, ?/ |& d. b( y RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
$ y3 D- O t& {4 d) @! l1 p' o
# Q: l. |1 _: F8 X, I4 m. j5 r //定时器TIM3初始化
8 x1 u: E3 ~1 { TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 , A0 Y8 _" a5 A, v) }
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值* N. y4 Y1 j2 n8 @
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
S; H# g# K8 ^2 h# Y TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式& W: s' h7 y, x0 a. \
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
! p- I" L+ q+ f6 f1 Y$ b// TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//选择TIMx从模式/ Y; Q- h5 n) w" u( t! C1 c
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断, ~6 @4 r$ _( T; S
+ I: Z3 ?2 l# b8 h, M% o k% A
//中断优先级NVIC设置
- Z( S9 z0 s9 Y. K' }) x NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断% n1 I! f8 b: T; ^
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级3级
4 r* f2 a! ~2 ~) e: D, i NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级* Q" F2 y. e% k3 s) q$ U% s
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能0 Q$ a1 R r$ u3 J6 L
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器/ z& p" q% o6 s1 Q- H
. U+ J% `5 x6 z9 u# I1 ^ //TIM3_Encoder_Write(65416) ;0 \0 d" e4 ^; R5 d
0 a$ d% J0 A( I3 r3 f TIM_Cmd(TIM3, ENABLE); //使能TIMx
6 K/ @- b2 m6 R; ?}
. V9 @ J$ A( @# {( ^
/ n; z4 }( w- N3 ?5 y4 \7 o4 ~6 @- i
5 _* |3 J( `, k+ ]: D/**********TIM2_Encoder******/2 y4 F |; B* |2 n5 g9 }
) q0 k! t" R7 f8 ?, a% u/ f, ~
void TIM2_Encoder_Init(u16 arr,u16 psc)
0 `7 o7 j5 @1 t5 c0 c4 e{
3 l, i* ^7 ]& @; F# Q TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
3 |/ H% k1 |- [% R4 x/ x6 I/ ?8 U NVIC_InitTypeDef NVIC_InitStructure;2 j, ^. Y% f; K" C$ h3 r! P$ N% b
TIM_ICInitTypeDef TIM_ICInitStructure;# s. {9 L: t& s* B+ e5 I
% l" o* a6 L n) d
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能6 t2 O% v: X2 I
+ V) e D6 W V; s; D
//定时器TIM2初始化! f. c, R- ~$ Z7 N& V+ Z% d* e/ S, Z
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
$ {2 k3 l1 `0 M/ f7 U% } TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值/ m# j- \( ~) C- I' Y3 P
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim' g/ }' l: Z( G
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
2 E& M/ G+ U" k% {1 p$ U" c TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位9 t0 Z+ E6 s6 w7 I9 D n* M
//TIM_SetCounter(TIM3, 0xFF88);//设置TIMx自动重装载寄存器值+ _3 h" B) B9 K8 Q2 ~6 ], `
; I; Y( p+ [6 ^* t' O TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12,8 g4 Y$ d/ R9 w
TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
+ z+ ~! `6 u5 W. f$ @0 ]+ a TIM_ICStructInit(&TIM_ICInitStructure);/ s+ @( E0 m8 f! `) R3 X
TIM_ICInitStructure.TIM_ICFilter = 6;
7 ]5 C9 x- D3 A6 `/ b. s TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
# M: J+ {0 ^" l$ u TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频 . T7 M& x" R" D: K# x. k) h, B
TIM_ICInit(TIM2, &TIM_ICInitStructure);0 U* |9 B/ x. N
//TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//选择TIMx从模式3 p5 }7 ?3 S% U, U2 \
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
* r$ Y% W) K9 @( {$ h TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断1 z# `! ]1 `0 |; S* [, V4 l9 l
//TIM2->CNT = 120;
; @$ J7 F; U- `; s2 y: Z( M- ] //中断优先级NVIC设置- t3 Q$ U) L8 } O5 d
NVIC_InitStructure.NVIC_IRQChannel = TIM_Channel_1|TIM_Channel_2; //TIM3中断
- m3 p+ F8 R | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级3级
2 u" z/ u4 m% y# t NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
) R+ o) O/ n% [0 j( H C4 h NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
1 z6 {2 p9 j' \9 t NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
# Q9 `& A) X& P3 i9 T$ D! Y& o) W9 V
//TIM3_Encoder_Write(65416) ;0 f) ~7 D- u; V! @& W% q( Z8 ]- N
2 z) n, o6 h6 d
TIM_Cmd(TIM2, ENABLE); //使能TIMx
" i2 T- }" D1 f! i' B}% _4 g( t9 \; x4 W T/ J9 S) y
2 S# I# I$ b* G0 K, h
# p; E" @" O+ A _
//定时器3中断服务程序& J2 l1 e- ^( d' F
void TIM3_IRQHandler(void) //TIM3中断6 X/ P+ A/ k/ f Q
{
- X( { B! Y9 U7 W! P if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否
; m8 q: e/ p; E6 l5 \/ ] {- j; y% w" [; X- K( T4 \
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志 8 C& ~6 u5 i- p! D! _% {
TIM2->SMCR ^= 0x8000 ;0 K4 s" d5 A5 D$ k4 A& y9 L0 y
% [3 y! A( j4 x2 ?
EncVal = TIM_GetCounter(TIM2);
% \2 b9 V2 d& J( X' c0 ^: R) {) X* j7 X
if(EncVal>=240) q, T: l; ~6 } F& j4 n
{6 H% i+ \+ w' c! l" s/ O
EncVal = 240;
3 I7 a% }' R5 \4 `. ^6 | }- v7 |0 ]. d P) ?
LCD_DrawPoint(i, 5+EncVal);
/ c9 M* `/ K1 |5 ^- U if(i==320)
: Q0 o6 q. A( R0 c9 r2 I {
; R4 {/ X) _- ~, y7 D5 K i =0;
+ q @" s( N9 j; O5 B LCD_Clear(WHITE); ! \& Y: F3 }. y
coordinate() ; //横坐标 , j, P" s& r/ K* M1 H- [0 C
; k ?/ L- C$ e5 L //delay_ms(50);
) E5 e) g! K d7 K }
$ f( I6 c3 P9 d+ K- X" A i++ ;
' t1 Z: B! [& g4 l! v& ] TIM2->CNT = 240;
5 J9 w; y* ]: V: m% [ Frequent() ;: ^' @. u* ?& n# @+ U/ v
//fre = 1000/(90 /(720*100/72)) ;
5 ]- T$ d: n, I8 O) H: {" F //sprintf((char*)speed,"speed : %d ",EncVal );//将LCD ID打印到lcd_id数组。
g+ P) N2 F" A* m8 g6 m //sprintf((char*)lcd_id,"LCD ID:%04X",lcddev.id);4 D- }! t, ]' E! I
//LCD_ShowString(200,200,200,16,16,"EncVal");+ Q" ~2 D7 T$ ?5 y
LCD_ShowxNum(250,200,EncVal,5,16,0);+ s* n; d' Y% d
printf("%d\r\n",EncVal) ;
' V6 l' u, G9 A4 Z- Y! c TIM_SetAutoreload(TIM1, 0xFF88);//设置TIMx自动重装载寄存器值
4 h9 \& X' {% m" A8 o }* N* y6 {- V8 d' k
}
! y' T. L$ q* R" Z% N
# L& Q8 ~# s4 t' |6 | |
|