?頻率測量:
?頻率>中界頻率:適用測周法? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 頻率<中界頻率:適用測頻法
第一步:配置GPIO,TIM的時(shí)基單元
? ? ? ? ? ? ?在配置TIM的時(shí)基單元參數(shù)有些許不同。
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
以最大量程計(jì)數(shù),防止計(jì)數(shù)溢出
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC
這個(gè)決定計(jì)數(shù)頻率:72M/PSC,這里取72-1,則計(jì)數(shù)頻率就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)
{
......
}
參數(shù)講解
TIM_ICInitStructure.TIM_Channel = 選擇通道
TIM_ICInitStructure.TIM_ICFilter = 選擇濾波器
TIM_ICInitStructure.TIM_ICPolarity = 選擇觸發(fā)極性
TIM_ICInitStructure.TIM_ICPrescaler = 選擇分頻系數(shù)
TIM_ICInitStructure.TIM_ICSelection = 選擇觸發(fā)信號的輸入端
配置觸發(fā)源: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)
{
.............
}
使能定時(shí)器: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)
{
............
}
那么,怎么獲取周期呢:根據(jù)測周法:fx = fc / N??
?文章來源:http://www.zghlxwxcb.cn/news/detail-645275.html
根據(jù)測周法:fx = fc / N
fc 為標(biāo)準(zhǔn)頻率 根據(jù)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通道配置成相反的的數(shù)
然后 Duty = CCR(CH2)/CCR(CH1) or CCR(CH1)/CCR(CH2)
其他函數(shù):void TIM_PWMIConfig();文章來源地址http://www.zghlxwxcb.cn/news/detail-645275.html
/**
* @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配置成相反的參數(shù),不支持CH3,和CH4
快速配置成PWMI的標(biāo)準(zhǔn)模型
到了這里,關(guān)于STM32—TIM:基本定時(shí)器(輸入捕獲:測頻率、周期)(標(biāo)準(zhǔn)庫)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!