STM32F103V编码器接口A和B可以在程序中调换吗?

2020-01-01 17:40发布

如题,今天使用STM32的编码器接口,因电路板中A和B接反,正好在定时器中的设置可以把IC1 映射在TI2上;IC2 映射在TI1上,经多次测试还是未成功,依然是IC1 映射在TI1上;IC2 映射在TI2上.
有用过对调的朋友帮忙看看,是我设置问题还是st的这个功能就是没有?
  1. void TIM4_InputCapture_Init(void)
  2. {
  3.         GPIO_InitTypeDef GPIO_InitStructure;
  4.         TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  5.        
  6.        
  7.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  8.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
  9.        
  10.         {
  11.                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  12.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  13.                 GPIO_Init(GPIOB, &GPIO_InitStructure);
  14. //                GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);//PB6,7不需要映射
  15.         }
  16.         {
  17.                  TIM_DeInit(TIM4);
  18.                  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);  
  19.                  TIM_TimeBaseStructure.TIM_Prescaler = 1;
  20.                  TIM_TimeBaseStructure.TIM_Period = 0xFFFE;
  21.                  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  22.                  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  23.                  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  24.                  
  25.                  TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  26.              
  27.                  TIM_ClearFlag(TIM4, TIM_FLAG_Update);//清除中断标志
  28.                  TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//使能/失能相应中断
  29.                  
  30.                  TIM4->CNT = 0;
  31.                  TIM_Cmd(TIM4, ENABLE);
  32.         }
  33. }
复制代码
  1. void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,
  2.                                 uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity)
  3. {
  4.   uint16_t tmpsmcr = 0;
  5.   uint16_t tmpccmr1 = 0;
  6.   uint16_t tmpccer = 0;
  7.    
  8.   /* Check the parameters */
  9.   assert_param(IS_TIM_LIST5_PERIPH(TIMx));
  10.   assert_param(IS_TIM_ENCODER_MODE(TIM_EncoderMode));
  11.   assert_param(IS_TIM_IC_POLARITY(TIM_IC1Polarity));
  12.   assert_param(IS_TIM_IC_POLARITY(TIM_IC2Polarity));

  13.   /* Get the TIMx SMCR register value */
  14.   tmpsmcr = TIMx->SMCR;
  15.   
  16.   /* Get the TIMx CCMR1 register value */
  17.   tmpccmr1 = TIMx->CCMR1;
  18.   
  19.   /* Get the TIMx CCER register value */
  20.   tmpccer = TIMx->CCER;
  21.   
  22.   /* Set the encoder Mode */
  23.   tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_SMS));
  24.   tmpsmcr |= TIM_EncoderMode;
  25.   
  26.   /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  27.   tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC1S)) & (uint16_t)(~((uint16_t)TIM_CCMR1_CC2S)));
  28. //  tmpccmr1 |= TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
  29.   tmpccmr1 |= TIM_CCMR1_CC1S_1 | TIM_CCMR1_CC2S_1;//这段没有是我根据数据手册中添加的
  30.   
  31.   /* Set the TI1 and the TI2 Polarities */
  32.   tmpccer &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCER_CC1P)) & ((uint16_t)~((uint16_t)TIM_CCER_CC2P)));
  33.   tmpccer |= (uint16_t)(TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << (uint16_t)4));
  34.   
  35.   /* Write to TIMx SMCR */
  36.   TIMx->SMCR = tmpsmcr;
  37.   /* Write to TIMx CCMR1 */
  38.   TIMx->CCMR1 = tmpccmr1;
  39.   /* Write to TIMx CCER */
  40.   TIMx->CCER = tmpccer;
  41. }
复制代码

0条回答

一周热门 更多>