使用STM32基本定时器进行输入捕获测频率和周期(标准库)

 频率测量:

 频率>中界频率:适用测周法                                                          频率<中界频率:适用测频法

第一步:配置GPIO,TIM的时基单元
             在配置TIM的时基单元参数有些许不同。

	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;		//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;		//PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;


	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);



    TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;		//ARR
    
    以最大量程计数,防止计数溢出




    TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;		//PSC 

    这个决定计数频率:72M/PSC,这里取72-1,则计数频率就1MHZ

    最低频率:1MHZ/65535 = 15HZ  所以最低频率由PSC决定

初始化输入捕获单元:void TIM_ICInit();
 

/**
  * @brief  Initializes the TIM peripheral according to the specified
  *         parameters in the TIM_ICInitStruct.
  * @param  TIMx: where x can be  1 to 17 except 6 and 7 to select the TIM peripheral.
  * @param  TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure
  *         that contains the configuration information for the specified TIM peripheral.
  * @retval None
  */
void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
{
  ......
}


   参数讲解

	TIM_ICInitStructure.TIM_Channel =          选择通道

	TIM_ICInitStructure.TIM_ICFilter =         选择滤波器

	TIM_ICInitStructure.TIM_ICPolarity =       选择触发极性

	TIM_ICInitStructure.TIM_ICPrescaler =      选择分频系数

	TIM_ICInitStructure.TIM_ICSelection =      选择触发信号的输入端      

配置触发源:void TIM_SelectInputTrigger()
 

/**
  * @brief  Selects the Input Trigger source
  * @param  TIMx: where x can be  1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the TIM peripheral.
  * @param  TIM_InputTriggerSource: The Input Trigger source.
  *   This parameter can be one of the following values:
  *     @arg TIM_TS_ITR0: Internal Trigger 0
  *     @arg TIM_TS_ITR1: Internal Trigger 1
  *     @arg TIM_TS_ITR2: Internal Trigger 2
  *     @arg TIM_TS_ITR3: Internal Trigger 3
  *     @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  *     @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  *     @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  *     @arg TIM_TS_ETRF: External Trigger input
  * @retval None
  */
void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)
{
  ...........
}

选择从模式:void TIM_SelectSlaveMode()
 

/**
  * @brief  Selects the TIMx Slave Mode.
  * @param  TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the TIM peripheral.
  * @param  TIM_SlaveMode: specifies the Timer Slave Mode.
  *   This parameter can be one of the following values:
  *     @arg TIM_SlaveMode_Reset: Rising edge of the selected trigger signal (TRGI) re-initializes
  *                               the counter and triggers an update of the registers.
  *     @arg TIM_SlaveMode_Gated:     The counter clock is enabled when the trigger signal (TRGI) is high.
  *     @arg TIM_SlaveMode_Trigger:   The counter starts at a rising edge of the trigger TRGI.
  *     @arg TIM_SlaveMode_External1: Rising edges of the selected trigger (TRGI) clock the counter.
  * @retval None
  */
void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode)
{
  .............
}

使能定时器:void TIM_Cmd();
 

/**
  * @brief  Enables or disables the specified TIM peripheral.
  * @param  TIMx: where x can be 1 to 17 to select the TIMx peripheral.
  * @param  NewState: new state of the TIMx peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
{
  ............
}

那么,怎么获取周期呢:根据测周法:fx = fc / N  
 

根据测周法:fx = fc / N

fc  为标准频率  根据PSC的配置 fc = 1MHZ


 则 N 等于输入捕获的值: TIM_GetCapture1()、 TIM_GetCapture2()、 TIM_GetCapture3()、 
                        TIM_GetCapture4()、

/**
  * @brief  Gets the TIMx Input Capture 2 value.
  * @param  TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the TIM peripheral.
  * @retval Capture Compare 1 Register value.
  */
uint16_t TIM_GetCapture1(TIM_TypeDef* TIMx)
{
  ..............
}


如何测占空比?只需要将CH2通道配置成相反的的数

然后 Duty = CCR(CH2)/CCR(CH1)  or  CCR(CH1)/CCR(CH2) 

其他函数:void TIM_PWMIConfig();

/**
  * @brief  Configures the TIM peripheral according to the specified
  *         parameters in the TIM_ICInitStruct to measure an external PWM signal.
  * @param  TIMx: where x can be  1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the TIM peripheral.
  * @param  TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure
  *         that contains the configuration information for the specified TIM peripheral.
  * @retval None
  */
void TIM_PWMIConfig(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
{
.........................
}



将通道CH1和CH2配置成相反的参数,不支持CH3,和CH4

快速配置成PWMI的标准模型

物联沃分享整理
物联沃-IOTWORD物联网 » 使用STM32基本定时器进行输入捕获测频率和周期(标准库)

发表评论