之前只用过51单片机,编程的时候全是设定寄存器,现在接触STM32发现寄存器太多了,头大了三天。。。
之前一直对着103的资料设定407的定时器,但是这哥俩区别真是有的,一开始就进错了门,还想找对人?407输入捕获要把GPIO设定成复用模式,还要做管脚复用的映射,
`GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM5); `
进入`GPIO_PinAFConfig` 看看注释后才豁然开朗,原来要这么设置AF。
@brief Changes the mapping of the specified pin.
@param GPIOx: where x can be (A..I) to select the GPIO peripheral.
@param GPIO_PinSource: specifies the pin for the Alternate function.
This parameter can be GPIO_PinSourcex where x can be (0..15).
@param GPIO_AFSelection: selects the pin to used as Alternate function.
This parameter can be one of the following values:
@arg GPIO_AF_RTC_50Hz: Connect RTC_50Hz pin to AF0 (default after reset)
@arg GPIO_AF_MCO: Connect MCO pin (MCO1 and MCO2) to AF0 (default after reset)
@arg GPIO_AF_TAMPER: Connect TAMPER pins (TAMPER_1 and TAMPER_2) to AF0 (default after reset)
@arg GPIO_AF_SWJ: Connect SWJ pins (SWD and JTAG)to AF0 (default after reset)
@arg GPIO_AF_TRACE: Connect TRACE pins to AF0 (default after reset)
@arg GPIO_AF_TIM1: Connect TIM1 pins to AF1
@arg GPIO_AF_TIM2: Connect TIM2 pins to AF1
@arg GPIO_AF_TIM3: Connect TIM3 pins to AF2
@arg GPIO_AF_TIM4: Connect TIM4 pins to AF2
@arg GPIO_AF_TIM5: Connect TIM5 pins to AF2
顺便记下捕获输入的配置项:
===============================================================================
Input Capture management functions
===============================================================================
===================================================================
TIM Driver: how to use it in Input Capture Mode
===================================================================
To use the Timer in Input Capture mode, the following steps are mandatory:
1. Enable TIM clock using RCC_APBxPeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE) function
2. Configure the TIM pins by configuring the corresponding GPIO pins
就是这一步除了错,IO口没配置正确,应该=加复用映射。
2. Configure the Time base unit as described in the first part of this driver,
if needed, else the Timer will run with the default configuration:
- Autoreload value = 0xFFFF
- Prescaler value = 0x0000
- Counter mode = Up counting
- Clock Division = TIM_CKD_DIV1
3. Fill the TIM_ICInitStruct with the desired parameters including:
- TIM Channel: TIM_Channel
- TIM Input Capture polarity: TIM_ICPolarity
- TIM Input Capture selection: TIM_ICSelection
- TIM Input Capture Prescaler: TIM_ICPrescaler
- TIM Input CApture filter value: TIM_ICFilter
4. Call TIM_ICInit(TIMx, &TIM_ICInitStruct) to configure the desired channel with the
corresponding configuration and to measure only frequency or duty cycle of the input signal,
or,
Call TIM_PWMIConfig(TIMx, &TIM_ICInitStruct) to configure the desired channels with the
corresponding configuration and to measure the frequency and the duty cycle of the input signal
5. Enable the NVIC or the DMA to read the measured frequency.
6. Enable the corresponding interrupt (or DMA request) to read the Captured value,
using the function TIM_ITConfig(TIMx, TIM_IT_CCx) (or TIM_DMA_Cmd(TIMx, TIM_DMA_CCx))
7. Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
8. Use TIM_GetCapturex(TIMx); to read the captured value.
Note1: All other functions can be used separately to modify, if needed,
a specific feature of the Timer.