芯片有TIM5_CH1_ETR
调试了很久不能成功计数,是我的初始化有问题吗?还是获取计数值得方式不对?下面为初始化代码和读取函数:
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/*输入管脚*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
GPIO_PinAFConfig(GPIOH, GPIO_PinSource10, GPIO_AF_TIM5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOH, &GPIO_InitStructure);
/*计数器*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
TIM_DeInit(TIM5);
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;
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( TIM5, &TIM_TimeBaseStructure); // Time base configuration
TIM_TIxExternalClockConfig(TIM5, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);//TIM5 的chanl2 做外部时钟的输入管脚。
//TIM5 的ETR管脚做外部时钟的输入管脚用这种模式
TIM_ETRClockMode2Config(TIM5, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_Cmd(TIM5, ENABLE);
TIM_SetCounter(TIM5, 0); // 清零计数器CNT
// TIM_Cmd(TIM5, DISABLE);
获取脉冲个数
SPEED4 = TIM5->CNT;
一周热门 更多>