关于STM32F10x系列 编码器模式的使用与配置

2019-08-14 18:36发布

void  TIM2_INIT(void)
{
       
        GPIO_InitTypeDef     GPIO_InitStruct;
        NVIC_InitTypeDef     NVIC_InitStructure;       
        TIM_ICInitTypeDef    TIM2_ICInitStructure;
        TIM_TimeBaseInitTypeDef   TIM_TimeBaseInitStruct;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA  ,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
       
       
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPD;
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
         GPIO_Init(GPIOA,&GPIO_InitStruct);
  GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1);       

        TIM_TimeBaseInitStruct.TIM_Period = 0xFFFF; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öé趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Ö
        TIM_TimeBaseInitStruct.TIM_Prescaler =72-1;         //Ô¤·ÖÆμÆ÷   
        TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòéϼÆêyÄ£ê½
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ

       
        TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);
       
        TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01         Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ       
  TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        //éÏéyÑØ2¶»ñ
  TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
  TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;         //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
  TIM2_ICInitStructure.TIM_ICFilter = 6;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
        TIM_ICInit(TIM2, &TIM2_ICInitStructure);
  TIM2->CCMR1 |= 1<<8;
        TIM2->CCER &= ~(1<<1);  //CC1P='0'  IC1FP1???,IC1FP1=TI1
  TIM2->CCER &= ~(1<<5);  //CC2P='0'  IC2FP2???,IC2FP2=TI2

        NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  //TIM2&#214;D&#182;&#207;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //&#207;è&#213;&#188;ó&#197;&#207;è&#188;&#182;2&#188;&#182;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //′óó&#197;&#207;è&#188;&#182;0&#188;&#182;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±&#187;ê1&#196;ü
        NVIC_Init(&NVIC_InitStructure);  //&#184;ù&#190;YNVIC_InitStruct&#214;D&#214;&#184;&#182;¨μ&#196;2&#206;êy3&#245;ê&#188;&#187;ˉíaéèNVIC&#188;&#196;′&#230;&#198;÷

        TIM_ITConfig (TIM2, TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2, ENABLE);//&#212;êDí&#184;üD&#194;&#214;D&#182;&#207; ,&#212;êDíCC1IE2&#182;&#187;&#241;&#214;D&#182;&#207;       
       
     TIM_Cmd(TIM2,ENABLE);         //ê1&#196;ü&#182;¨ê±&#198;÷2
        TIM2->CNT=0;
                        
       
}
u8 BMQ_A;
u8 BMQ_B;

void TIM2_IRQHandler(void)
{
       

        if(TIM_GetITStatus(TIM2,TIM_IT_CC1)!=RESET )
        {
                        BMQ_A++;
                LED1=0;       
        }
        if(TIM_GetITStatus(TIM2,TIM_IT_CC2)!=RESET )
        {
                        BMQ_B++;
        }
       
        TIM_ClearITPendingBit(TIM2,TIM_IT_Update|TIM_IT_CC1);
}

这个是我本人配置的编码器程序   但是在调试过程中发现  BMQ_A   根本就没有自增  也就是  没有进入捕获中断里面   请问各位大神   这是怎么回事  是不是我配置的问题

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。