[img]file:///C:UsersSunMinghuiDocumentsTencent Files1007235227ImageC2CPAV(1WT4JAQ[_ISHBGCNZQQ.jpg[/img][img=0,1]file:///C:UsersSunMinghuiDocumentsTencent Files1007235227ImageC2CPAV(1WT4JAQ[_ISHBGCNZQQ.jpg[/img][img=0,1]file:///C:UsersSunMinghuiDocumentsTencent Files1007235227ImageC2CPAV(1WT4JAQ[_ISHBGCNZQQ.jpg[/img] GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//开启TIM3时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);//开启GPIOB时钟
GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4);//PB0引脚复用
GPIO_PinAFConfig(GPIOD,GPIO_PinSource13,GPIO_AF_TIM4);//PB1引脚服用
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13; //GPIOB0,GPIOB1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOD,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 4000-1; //设置下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//设置为编码器模式
TIM_ICStructInit(&TIM_ICInitStruct);//没有时,在电磁阀工作时会对编码器造成影响
TIM_ICInitStruct.TIM_ICFilter=60;//滤波。
TIM_ICInit(TIM4,&TIM_ICInitStruct);
TIM_ClearFlag(TIM4,TIM_FLAG_Update); //清除标志位
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);//更新中断
TIM4->CNT = 0; //计数器清零
TIM_Cmd(TIM4, ENABLE); //使能TIMx
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>