dirtwillfly 发表于 2014-3-2 09:42 
把io配置成外部中断,你的这个应用采用上升沿或下降沿都可以
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;
}
};
这是网上的一个例子,他得到的计数值是不是就是在定时器时间内的计数值
|