- void Encoder1_Init(void)
- {
- GPIO_InitTypeDef GPIO_Initure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_Initure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_Initure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Initure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_Initure);
-
- TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI2, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
-
- TIM3->CNT = 1500;
- //TIM3->CNT = 0;
- TIM_Cmd(TIM3, ENABLE);
- }
- //读取编码器的值
- int Encoder1_read(void)
- {
- int count = 0;
-
- count = TIM3->CNT - 1500;
- //count = TIM3->CNT;
- // TIM3->CNT = 0x7fff; //清零重新读取
- return count;
- }
复制代码
|