中级会员
 
- 积分
- 227
- 金钱
- 227
- 注册时间
- 2016-4-12
- 在线时间
- 82 小时
|

楼主 |
发表于 2016-6-22 15:43:39
|
显示全部楼层
void TIM5_Configuration(void)//编码器接口设置(TIM5)/PA0-A相
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //TIM5时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //PA0复用位定时器5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//需要弱上拉
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=0; //定时器分频(输入下应为0否则不稳定(手册:由于捕获预分频器不用于触发操作,因此无需对其进行配置。))
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=65535; //自动重装载值,【编码器模式中为计数最大值】
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM5,TIM_EncoderMode_TI1,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//ENCODER模式设定
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;//ENCODER模式必须为直接
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;//ENCODER模式必须为不分频
TIM_ICInitStructure.TIM_ICFilter = 6; //选择输入比较滤波器,加入滤波后在频繁正反转时可能卡死
TIM_ICInit(TIM5, &TIM_ICInitStructure);//将TIM_ICInitStructure中的指定参数初始化ENCODER_TIM
TIM_ClearFlag(TIM5, TIM_FLAG_Update);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM5->CNT = 0;
TIM_Cmd(TIM5, ENABLE);
} |
|