[英]Confused when using timer as counter on STM32F1 series
我正在使用 stm32f103 微控制器實施一個項目。 基本上,我使用 Timer2 來計算外部脈沖。
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h" // timer library
#include "misc.h"
/* Built-in LED */
#define LEDPORT (GPIOC)
#define LEDPIN (GPIO_Pin_13)
int main(void){
/* gpio init struct */
GPIO_InitTypeDef gpioInit;
/* enable clock for GPIOA thru ABP2 peripheral communication bus */
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC->APB2ENR |= RCC_APB2Periph_GPIOC;
/* use LED pin */
gpioInit.GPIO_Pin = LEDPIN;
/* mode: output */
gpioInit.GPIO_Mode = GPIO_Mode_Out_PP;
gpioInit.GPIO_Speed = GPIO_Speed_2MHz;
/* apply configuration */
GPIO_Init(LEDPORT, &gpioInit);
/* clear built-in led */
GPIO_SetBits(LEDPORT, LEDPIN);
/* Enable timer clock */
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC->APB1ENR |= RCC_APB1Periph_TIM2;
/* Configure channel 2 as input, mapped on the Timer Input 1 (TI1) */
TIM2->CCMR1 |= TIM_CCMR1_CC2S_1;
/* Configure channel 2 detecting falling edge polarity */
TIM2->CCER |= TIM_CCER_CC2P;
/* Configure TIM2 in External Clock Mode 1 & select TI2 as the input source */
TIM2->SMCR |= TIM_SMCR_SMS | TIM_SMCR_TS_2 | TIM_SMCR_TS_1;
/* Enable the counter by writing CEN=1 in the TIMx_CR1 register */
// TIM_Cmd(TIM2, ENABLE);
TIM2->CR1 |= TIM_CR1_CEN;
/* Enable interrupt trigger. */
// TIM_ITConfig(TIM2, TIM_IT_Trigger, ENABLE);
TIM2->DIER |= TIM_DIER_TIE;
for (;;){
if (TIM_GetITStatus(TIM2, TIM_IT_Trigger) != RESET){
TIM_ClearITPendingBit(TIM2, TIM_IT_Trigger);
LEDPORT->ODR ^= LEDPIN;
}
}
/* never reach */
return 0;
}
該代碼運行良好。 我不明白的是
/* Configure CC2S bits = 10: CC2 channel is configured as input, IC2 is mapped on TI1 */
TIM2->CCMR1 |= TIM_CCMR1_CC2S_1;
假設使用定時器輸入 1,但我必須將觸發輸入配置為過濾定時器輸入 2
/* Configure TIM2 in External Clock Mode 1 & select TI2 as the input source (110: Filtered Timer Input 2 (TI2FP2))*/
TIM2->SMCR |= TIM_SMCR_SMS | TIM_SMCR_TS_2 | TIM_SMCR_TS_1;
有沒有人可以向我澄清?
該代碼運行良好。 我不明白的是
您的評論中有幾個失敗之處,需要澄清的是:
假設使用定時器輸入 1,但我必須將觸發輸入配置為過濾定時器輸入 2
TIM2->CCMR1 |= TIM_CCMR1_CC2S_1;
這將通道 2 配置為通過在 TIMx_CCMR1 寄存器中寫入CC2S= '01來檢測TI2輸入(不是 TI1)上的上升沿。
/* 在外部時鍾模式 1 中配置 TIM2 & 選擇 TI2 作為輸入源 */
TIM2->SMCR |= TIM_SMCR_SMS | TIM_SMCR_TS_2 | TIM_SMCR_TS_1;
a) 通過在 TIMx_SMCR 寄存器中寫入 SMS=111,將定時器配置為外部時鍾模式 1。
b) 通過將 TS=110 寫入 TIMx_SMCR 寄存器,選擇 TI2 作為開頭指定的輸入源。
有關更多信息,您可以在 uC 的最新數據表中找到“TI2 外部時鍾連接”示例。
BTW 你寫評論了嗎? 您的評論中有幾個失敗之處。
gpioInit.GPIO_Mode = GPIO_Mode_Out_PP; // Open-drain output mode for built-in LED
不是 open-drain,應該注釋為push-pull
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.