本人用定时器10 脉冲计数时遇到一些问题,当外部无脉冲时,计数器计数一直增加,当我清零之后,还会增加!跪求大神指导!已解决!
查文档发现定时器10,没有外部时钟输入管脚,于是用了定时器2.
static void fn_TIM2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/*输入管脚*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource1, GPIO_AF_TIM2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*计数器*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; /*定时器时钟(CK_INT)频率与数字滤波器(ETR,TIx)
使用的采样频率之间的分频比为1*/
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure); // Time base configuration
TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);/*TIM2 的chanl2 做外部时钟的输入管脚。
/*TIM2 的ETR管脚做外部时钟的输入管脚用这种模式*/
TIM_ETRClockMode1Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0);
TIM_SetCounter(TIM2, 0); // 清零计数器CNT
}
static uint32_t ui_Count = 0;
void main()
{
fn_TIM2_Init();
TIM_Cmd(TIM2, ENABLE);
while(1)
{
TIM_Cmd(TIM2, DISABLE);
ui_Count = TIM_GetCounter(TIM2);
TIM_SetCounter(TIM2, 0);
TIM_Cmd(TIM2, ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>