微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > STM32编码器模式函数,求投票

STM32编码器模式函数,求投票

时间:10-02 整理:3721RD 点击:


#include "include.h"
/*******************************************************************************
±à???÷2?×?oˉêy3?ê??ˉ
//encoder.INPUT_B---PB4(TIM3_CH1)
//encoder.INPUT_A---PB5(TIM3_CH2)
*******************************************************************************/
void Encoder_Configuration(void)
{
                GPIO_InitTypeDef                 gpio;
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
                RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  
       
                GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);   
                gpio.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_4;
                gpio.GPIO_Speed = GPIO_Speed_2MHz;
                gpio.GPIO_Mode = GPIO_Mode_IPU;
                GPIO_Init(GPIOB,&gpio);
               
                TIM_EncoderInterfaceConfig(TIM3,
                                                                                                                        TIM_EncoderMode_TI12,
                                                                                                                        TIM_ICPolarity_Falling,
                                                                                                                        TIM_ICPolarity_Falling);
                TIM_Cmd(TIM3,ENABLE);
}

/*******************************************************************************
TIM3?D??′|àíoˉêy
*******************************************************************************/
void TIM3_IRQHandler(void)
{  
  /* Clear the interrupt pending flag */
          if (TIM_GetITStatus(TIM3,TIM_IT_Update)!= RESET)
        {
        TIM_ClearFlag(TIM3, TIM_FLAG_Update);
        }
}
void Encoder_Start(void)
{
    TIM3->CNT = 0x7fff;
}
int Encoder_Get_CNT(void)
{
    int cnt = 0;
    cnt = (TIM3->CNT)-0x7fff;
    TIM3->CNT = 0x7fff;
    return cnt;
}

上一篇:+开发板靓照
下一篇:开箱献美照

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top