| 
 
金牌会员  
 
	积分1050金钱1050 注册时间2016-5-13在线时间111 小时 | 
 
 发表于 2016-5-29 16:35:30
|
显示全部楼层 
| 供你参考。 [mw_shl_code=c,true]// --------------------------------------------------------- //
 // 函数名称:MotorEncoderInit
 // 函数说明: 电机编码器初始化
 // 输入参数: 无
 // 输出参数: 无
 // 返回值:   无
 // 历史记录:
 //     <作者>    <时间>      <修改记录>
 //      lp    2015-06-29    创建该函数
 // --------------------------------------------------------- //
 static void MotorBEncoderInit(void)
 {
 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 TIM_ICInitTypeDef TIM_ICInitStructure;
 GPIO_InitTypeDef GPIO_InitStructure;
 
 /*开定时器时钟*/
 RCC_APB1PeriphClockCmd(MOTORB_ENCODER_TIM_CLK, ENABLE);
 /*开对应GPIO时钟*/
 RCC_APB2PeriphClockCmd(MOTORB_ENCODER_CLK, ENABLE);
 
 /*配置对应的GPIO*/
 GPIO_StructInit(&GPIO_InitStructure);
 GPIO_InitStructure.GPIO_Pin = MOTORB_ENCODER_PIN;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_Init(MOTORB_ENCODER_PORT, &GPIO_InitStructure);
 
 
 /*配置定时器*/
 TIM_DeInit(MOTORB_ENCODER_TIM);
 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
 TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
 TIM_TimeBaseStructure.TIM_Period = 65535;
 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseInit(MOTORB_ENCODER_TIM, &TIM_TimeBaseStructure);
 
 /*配置定时器正交计数功能*/
 TIM_EncoderInterfaceConfig(MOTORB_ENCODER_TIM, TIM_EncoderMode_TI12,
 TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
 TIM_ICStructInit(&TIM_ICInitStructure);
 TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
 TIM_ICInit(MOTORB_ENCODER_TIM, &TIM_ICInitStructure);
 
 /*清除定时器溢出中断*/
 TIM_ClearFlag(MOTORB_ENCODER_TIM, TIM_FLAG_Update);
 /*预先清除所有中断位*/
 TIM_ClearITPendingBit(MOTORB_ENCODER_TIM, TIM_IT_Update);
 /*由于16位的计数器会溢出,所以开定时器溢出中断,在中断里面加溢出值或着减溢出值*/
 TIM_ITConfig(MOTORB_ENCODER_TIM, TIM_IT_Update, ENABLE);
 TIM_Cmd(MOTORB_ENCODER_TIM, ENABLE);
 
 /*清零编码器值*/
 ClearEncodeBValue();
 }
 
 
 // --------------------------------------------------------- //
 // 函数名称:TIM8_UP_IRQHandler
 // 函数说明: TM8溢出中断函数,通过中断来累计脉冲数超出16定时器最大计数值的次数
 // 输入参数: 无
 // 输出参数: 无
 // 返回值:   无
 // 历史记录:
 //     <作者>    <时间>      <修改记录>
 //      lp    2015-06-30      创建该函数
 // --------------------------------------------------------- //
 void TIM4_IRQHandler(void)
 {
 int8 lCurDir = INVAL;
 if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
 {
 TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
 lCurDir = GetMotorBDirection();
 s_lTIM4UpOverCnt -= lCurDir;        //lCurDir来确定是加一,还是减一
 }
 }
 
 // --------------------------------------------------------- //
 // 函数名称:GetMotor1EncoderValue
 // 函数说明: 获取电机编码器计数值
 // 输入参数: 无
 // 输出参数: 无
 // 返回值:   返回电机编码器脉冲个数,有符号,可以表示电机正反转记录的脉冲值
 // 历史记录:
 //     <作者>    <时间>      <修改记录>
 //      lp    2015-06-30    创建该函数
 // --------------------------------------------------------- //
 int32 GetMotorBEncoderValue(void)
 {
 int32 lEncoderValue = 0;
 lEncoderValue = MOTORB_ENCODER_TIM->CNT + s_lTIM4UpOverCnt*65536 - ENCODER_VALUE_OFFSET;
 return lEncoderValue;
 }
 
 // --------------------------------------------------------- //
 // 函数名称:ClearEncodeBValue
 // 函数说明: 电机编码器计数值清零
 // 输入参数: 无
 // 输出参数: 无
 // 返回值:   无
 // 历史记录:
 //     <作者>    <时间>      <修改记录>
 //      lp    2015-06-30    创建该函数
 // --------------------------------------------------------- //
 void ClearEncodeBValue(void)
 {
 MOTORB_ENCODER_TIM->CNT = ENCODER_VALUE_OFFSET;
 s_lTIM4UpOverCnt = 0;
 }[/mw_shl_code]
 | 
 |