C 在STM32F1系列上使用计时器作为计数器时感到困惑
我正在使用stm32f103微控制器实施一个项目。基本上,我用定时器2来计算外部脉冲C 在STM32F1系列上使用计时器作为计数器时感到困惑,c,timer,counter,stm32,C,Timer,Counter,Stm32,我正在使用stm32f103微控制器实施一个项目。基本上,我用定时器2来计算外部脉冲 #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_P
#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
/* 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;
这将信道2配置为通过在TIMx_CCMR1寄存器中写入CC2S='01来检测TI2输入(非TI1)上的上升沿
/*在外部时钟模式1中配置TIM2,并选择TI2作为输入源*/
a) 通过在TIMx_SMCR寄存器中写入SMS=111,在外部时钟模式1下配置定时器
b) 通过将TS=110写入TIMx_SMCR寄存器,选择TI2作为开始时指定的输入源
有关更多信息,您可以在uC的最新数据表中找到“TI2外部时钟连接”示例
顺便问一下,你写评论了吗?你的评论中有几处错误
gpioInit.GPIO_Mode = GPIO_Mode_Out_PP; // Open-drain output mode for built-in LED
不是明沟,应注释为推拉式
代码运行良好。我不明白的是
澄清一下,您的评论中有几个错误:
假设使用定时器输入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;
这将信道2配置为通过在TIMx_CCMR1寄存器中写入CC2S='01来检测TI2输入(非TI1)上的上升沿
/*在外部时钟模式1中配置TIM2,并选择TI2作为输入源*/
a) 通过在TIMx_SMCR寄存器中写入SMS=111,在外部时钟模式1下配置定时器
b) 通过将TS=110写入TIMx_SMCR寄存器,选择TI2作为开始时指定的输入源
有关更多信息,您可以在uC的最新数据表中找到“TI2外部时钟连接”示例
顺便问一下,你写评论了吗?你的评论中有几处错误
gpioInit.GPIO_Mode = GPIO_Mode_Out_PP; // Open-drain output mode for built-in LED
不是明沟,应注释为推拉式 你写评论了吗?你的评论中有几处错误。例如,
//内置LED的开漏输出模式
应该是推拉模式,而不是开漏模式。您写了评论吗?你的评论中有几处错误。例如,//内置LED的开漏输出模式
应为推拉模式
,而不是开漏模式。谢谢您的回答。当然,我也遵循了这个例子。根据STM32f1系列数据表,我在CCMR1寄存器中的配置为:10:CC2通道配置为输入,IC2映射到TI1。关于SMCR寄存器,它是110:滤波定时器输入2(TI2FP2)。我想我应该编辑我的帖子,让它更清楚。LED上的cmt是我的错误,抱歉!谢谢你的回答。当然,我也遵循了这个例子。根据STM32f1系列数据表,我在CCMR1寄存器中的配置为:10:CC2通道配置为输入,IC2映射到TI1。关于SMCR寄存器,它是110:滤波定时器输入2(TI2FP2)。我想我应该编辑我的帖子,让它更清楚。LED上的cmt是我的错误,抱歉!