具体设置如下:
/* TIM3_CH2 为脉冲输入口
1. 配置GPIO_GPIOA_PIN7 输入
2. 配置TIM3 计数器在TI2 端的上升沿计数:
1). TIMx_CCMR1: CC2S =01; 配置通道2检测TI2输入的上升沿
2). TIMx_CCMR1:IC2F =000; 选择输入滤波器带宽
3). TIMx_CCER: CC2P =0; 配置上升沿极性 √
4). TIMx_SMCR: SMS =111; 选择定时器外部时钟模式1
5). TIMx_SMCR: TS =110; 选择TI2作为触发输入源 √
6). TIMx_CR1: CEN =1; 启动计数器
*/
void TIM3_External_Clock_CountingMode(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; /*定时器时钟(CK_INT)频率与数字滤波器(ETR,TIx)
使用的采样频率之间的分频比为1*/
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure); // Time base configuration
/*
tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12); // CCMR1_IC2F
tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8); // CCMR1_CC2S
由TIM_TIxExternalCLK1Source_TI2决定了
TIM_ICSelection=TIM_ICSelection_DirectTI: CCMR1_CC2S = 01;
TIM_ICPolarity_Rising = CCER_CC2P
TIM_TIxExternalCLK1Source_TI2 = TIM_SMCR_TS
该函数定义了TIM_SlaveMode_External1;外部时钟模式1
*/
TIM_TIxExternalClockConfig(TIM3,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0);
// TIM_SetCounter(TIM3, 0); // 清零计数器CNT
// TIM_Cmd(TIM3,ENABLE);
}
// 下面是使用方法:
TIM3_External_Clock_CountingMode();
TIM_SetCounter(TIM3, 0); // 清零计数器CNT
TIM_Cmd(TIM3,ENABLE);
SecCnt = 0;
TFgs.Secok = 0;
i=0;
while(1)
{
Delay_Nms(1000);
CountPulse = TIM_GetCounter(TIM3);
DisplayDat(10,10+24*i,CountPulse,5);
TFgs.Secok = 0;
if(++CountTims>=120)
{
TIM_Cmd(TIM3,DISABLE);
CountPulse = TIM_GetCounter(TIM3);
DisplayDat(10,10+24*i,CountPulse,5);
if(++i>11)i=0;
TIM_SetCounter(TIM3, 0); // 清零计数器CNT
TIM_Cmd(TIM3,ENABLE);
SecCnt = 0;
TFgs.Secok = 0;
CountTims =0;
}
};
上述程序经过硬件测试。