static volatile int hEncoder_Timer_Overflow1;
void ENC_Init1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);//½«GPIO_InitStructÖDμÄ2Îêy°′è±ê¡Öμêäèë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //*******2»í¬******
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM3);
NVIC_Config(2);
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = (4*4096)-1;
TIM_TimeBaseStructure.TIM_Prescaler = 84-1; //1-1=0£¬òÔ84MHz/1=84MHzμÄÆμÂê¼Æêy
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_BothEdge,TIM_ICPolarity_BothEdge);
TIM_ICStructInit(&TIM_ICInitStructure); /*½«½á11ìåÖDμÄÄúèYè±ê¡êäèë*/
TIM_ICInitStructure.TIM_ICFilter = 6; //éèÖÃêäèë2éÑùÆμÂ꣬á¬Dø2éÑù6′Îμçƽ£¬èô¶¼êǸßμçƽÕa′¥·¢2¶»ñÖD¶Ï
TIM_ICInit(TIM3,&TIM_ICInitStructure); //½«TIM_ICInitStructureÖDμÄÖ¸¶¨2Îêy3õê¼»ˉTIM3
TIM_ClearFlag(TIM3,TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM3->CNT=0;//Reset counter
TIM_Cmd(TIM3, ENABLE);
}
//±àÂëÆ÷1μÄòç3öÖD¶Ïoˉêy
void TIM3_IRQHandler (void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) //òç3öÖD¶Ï ±àÂëÆ÷2é¼ˉμÄÂö3åêy£¨êμ¼êéÏ′ÎêDZàÂëÆ÷×aμÄè|êy£©
{
if(hEncoder_Timer_Overflow1!=65535)
{
hEncoder_Timer_Overflow1++;
}
printf("溢出次数:%d
",hEncoder_Timer_Overflow1);
LED0=!LED0;
}
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
}
连接编码器后,编码器不发生转动,怎么也能产生脉冲?请求民间大神,帮忙看下编码器模式是否正确!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
方便的话把您的联系方式发给我,我把工程发给你,非常感谢!
一周热门 更多>