STM32F2定时器1通道1输入捕获( `% U* H5 |% A" T4 F6 M! }- }3 u
一、中文手册上的配置步骤: i/ a# \; y3 E) B, a7 I8 T& v* D
" w$ J0 d# ]% B' j. ~8 Z6 h* t, v7 ~! d; s
1.1初始化通道对应的IO口
5 X+ m u, w sGPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//选择通道对应的IO口
6 x8 |& Z+ }. u
3 y% ?% ~, a* p. S7 k9 q; Y8 x. {; j; N7 u
/ p# C% e. \) n& f n( q& u- w1.2选择输入捕获的通道( {( z; C) s1 Y, u# [
InitStructure.TIM_Channel = TIM_Channel_1;
/ K J5 _! p7 v! Q3 C1.3选择有效输入端
) ~) M# |8 c" q g3 S# {8 t1 o( g选择有效输入端:TIMx_CCR1必须连接到TI1输入,所以写入TIMx_CCR1寄存器中的" H1 W4 q# v$ P) N
CC1S=01,只要CC1S不为’00’,通道被配置为输入,并且TIMx_CCR1寄存器变为只读6 j* h8 r" t+ ]: V
注意 CC1S仅在通道关闭时(TIMx_CCER寄存器的CC1E=0)才是可写的,配置之前调用
# H7 Q5 O! V1 M. [5 p" Lvoid TIM_DeInit(TIM_TypeDef* TIMx)即可
8 n0 i8 x# H; o; c! r
) w- z2 M) [4 {; s6 _ I; T8 Q5 g, b+ J( ?2 ]
# d! r5 p7 g5 d7 f2 b! [+ U1.3配置滤波器为所需带宽–TIM_ICInitStructure.TIM_ICFilter = 0x00;
) X" _) l O5 ]2 S% A根据输入信号的特点,配置输入滤波器为所需的带宽(即输入为TIx时,输入滤波器控制位是0 G0 }8 S' r# b) v: \
TIMx_CCMRx寄存器中的ICxF位)。假设输入信号在最多5个内部时钟周期的时间内抖动,# M5 A- p7 b$ B, T2 o
我们须配置滤波器的带宽长于5个时钟周期;因此我们可以(以f DTS 频率)连续采样8次,以确3 X+ z# \$ z' q+ k1 ]
认在TI1上一次真实的边沿变换,即在TIMx_CCMR1寄存器中写入IC1F=0011。4 {$ [$ O. ^$ Z" ~! t5 s- G
1.4选择有效转换边沿–TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
. i% O- ]7 h8 v$ i1 i选择TI1通道的有效转换边沿,在TIMx_CCER寄存器中写入CC1P=0(上升沿)。
+ T' L, V# A9 X% T2 Y6 ^, z; s7 Y' F+ D/ u: b$ H' {
7 S4 d2 g$ h; a: b3 K% H* t
( i4 J* Z# `0 K1 |0 Y: p1.5配置输入预分频器–TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
4 D4 y, K! w2 Y在本例中,我们希望捕获发生在每一个有效的电平转换时刻,因此预
( i' y$ L# n; e2 I$ v分频器被禁止(写TIMx_CCMR1寄存器的IC1PS=00)。) n! l& Q0 y$ o1 q3 c5 O" `7 \
1.6 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
. |$ U; ^9 X$ t& k2 }TIM_ICSelection_DirectTI -->TI1,TI2,TI3,TI4 对应IC1,IC2,IC3,IC4;
! Q. ^: s- H8 ^' ?TIM_ICSelection_IndirectTI–>TI1,TI2,TI3,TI4 对应IC2,IC1,IC4,IC3;
1 ~: o. b0 i+ y) O" c% y5 `9 k- Z& K$ |5 e1 E2 n
$ o& a3 f3 o, y( X4 S2 x5 [; b" t+ B4 T2 q1 d
P; y! s s) x; u# M2.1完整代码
' O" p1 _3 ^" \6 Q- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);# ?, }6 U( t( O& t/ m3 M, h& \
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
9 k1 Y9 n* E I9 J+ y: g" ] - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//根据通道所对应的IO口修改
- n' o$ H# g3 {. ^6 @9 e - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
1 [- U7 {" R1 s; ] \: O4 [ - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;! y% M7 J$ S/ y9 c- F( C
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
7 Y2 P8 Y: x, b# p' i3 ]9 h' @% a; t - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
" C) ^% @. K) A8 d- K" F! J - GPIO_Init(GPIOA, &GPIO_InitStructure); ; [+ n) ~! \) q' [0 }' G
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);& [" I0 {7 ?6 V! _5 |1 A
- * X& l6 U/ d2 F0 g' y/ z
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;- H* Z2 N# I: e# E! G
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//TIM_Input_Capture_Polarity
: c9 A: C- B N - TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//TIM_Input_Capture_Selection* v* A, X8 W2 g9 Q/ T' R7 }6 J
- TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;2 s. L; F8 z; B9 x1 G6 Y
- TIM_ICInitStructure.TIM_ICFilter = 0x00;
* @8 w, d! \' [$ x8 @! [. | - TIM_ICInit(TIM1, &TIM_ICInitStructure);
8 I) U+ G5 z. |, R s2 g - 6 H8 g: f3 X+ m+ k' F
- NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
. _0 t6 h; G5 T- a' [ - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;) U; W: S- i; m, M0 z, [8 J
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;% w, v* u R: D: d* {
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
+ N/ j1 O3 b1 W# j# Z$ ^/ k - NVIC_Init(&NVIC_InitStructure);
复制代码
4 L0 G9 H* Q) f% [3.1输入捕获启动DMA传输配置- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);
& E; C% V) S1 W4 J4 u - DMA_DeInit(DMA2_Stream6);
1 ?9 x0 G; p; s4 x( ]6 I% s - DMA_InitStructure.DMA_Channel = DMA_Channel_6; ! g7 _9 x2 W' \9 D4 P
- DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) GPIOD->IDR;
w" Z. J& t! C6 j; k' q - DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DEF_Buffer;
( f3 \$ C/ m, I! k r - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
, c" N8 |# `7 j2 ? - DMA_InitStructure.DMA_BufferSize = 40000;. S( G8 g7 z! x7 j) _8 T! `# t
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;: W2 }1 W1 y+ Y9 D% k% n& x9 W
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;- w9 z S3 [6 f& w$ H
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
: X2 J' \, l6 M0 U - DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;/ V p* v3 g1 n$ a0 ?7 x5 L: H; [/ i
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;3 s, o3 u6 e/ i; o+ D( ]0 E, p
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;* I% B& a3 I: A5 R) V& B: i u1 Z
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;* s( v! t+ m( |: B! M ?
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
% h) X: C7 T: M# i8 A. Z - DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
" @/ H& G; Z9 P; i; o - DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
! m0 J3 V2 s) ?4 v; z7 }9 W - DMA_Init(DMA2_Stream6, &DMA_InitStructure);
复制代码 6 q8 E% R9 ~; D- V9 W$ S
3.2启动传输# W$ O, }. i- d
- TIM_DMAConfig(TIM1, TIM_DMABase_CCR1, TIM_DMABurstLength_1Transfer);
* c0 Y: ]; d7 m4 I - TIM_DMACmd(TIM1, TIM_DMA_CC1, ENABLE);* O8 S4 b0 w) M3 H& \" F: U) x
-
, b! Q" F6 v8 a. _, o4 i - /* Enable the CC2 Interrupt Request */) b6 ?3 D; e% ~: Y- |2 R- o* W
- TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);: r6 O1 i2 T) n$ r5 x0 X
- /* TIM enable counter */, k j, v+ i$ x" J% Y8 ?5 g& U, e8 }
- TIM_Cmd(TIM1, ENABLE);+ s% \5 @% N; M' y3 U
- DMA_Cmd(DMA2_Stream6, ENABLE);3 }8 f& t8 y0 B% H* u! S3 c
复制代码 ; Z6 j. w4 Z: C( K3 V
4 N8 ^* i- ^ }
" c. G, X/ H( \( `) A1 T3 @: ~ |