标题: 基于stm32 编码器应用 [打印本页]

作者: 前行者    时间: 2018-5-27 12:15
标题: 基于stm32 编码器应用
  1. void Encoder1_Init(void)
  2. {
  3.         GPIO_InitTypeDef GPIO_Initure;
  4.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  5.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
  6.        
  7.         GPIO_Initure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  8.         GPIO_Initure.GPIO_Mode = GPIO_Mode_IPU;
  9.         GPIO_Initure.GPIO_Speed = GPIO_Speed_50MHz;
  10.         GPIO_Init(GPIOA, &GPIO_Initure);
  11.        
  12.         TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI2, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
  13.        
  14.         TIM3->CNT = 1500;
  15.         //TIM3->CNT = 0;
  16.         TIM_Cmd(TIM3, ENABLE);
  17. }

  18. //读取编码器的值
  19. int Encoder1_read(void)
  20. {
  21.         int count = 0;
  22.        
  23.         count = TIM3->CNT - 1500;
  24.   //count = TIM3->CNT;
  25.         //        TIM3->CNT = 0x7fff; //清零重新读取

  26.         return count;
  27. }
复制代码







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1