高中生
最后登录1970-1-1
在线时间 小时
注册时间2016-9-2
|
楼主 |
发表于 2016-9-13 15:39:57
|
显示全部楼层
这是编码器的配置
- void Encoder_Init_TIM4(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//ê1Äü¶¨ê±Æ÷4μÄê±Öó
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//ê1ÄüPB¶Ë¿úê±Öó
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //¶Ë¿úÅäÖÃ
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿Õêäèë
- GPIO_Init(GPIOB, &GPIO_InitStructure); //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOB
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // Ô¤·ÖÆμÆ÷
- TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//Ñ¡Ôñê±Öó·ÖÆμ£o2»·ÖÆμ
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIMÏòéϼÆêy
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//ê1óñàÂëÆ÷Ä£ê½3
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10;
- TIM_ICInit(TIM4, &TIM_ICInitStructure);
- TIM_ClearFlag(TIM4, TIM_FLAG_Update);//Çå3yTIMμĸüD±ê־λ
- TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
- TIM_SetCounter(TIM4,0);
- TIM_Cmd(TIM4, ENABLE);
- }
复制代码
这是主函数
- while(1)
- {
- Encoder=TIM_GetCounter(TIM2);
-
- printf("%d\r\n",Encoder);
- delay_ms(500);
- }
复制代码 |
|