大佬,用的的框架里面定时器库函数想实现定时器通过编码器接口获取编码值,却找不到相关命令,只能通过标准库实现,能否升级一下
大佬,用的的框架里面定时器库函数想实现定时器通过编码器接口获取编码值,却找不到相关命令,只能通过标准库实现,能否升级一下 标准库相关实现命令: void Encoder_init(void) { //1、开启时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 65536-1; TIM_TimeBaseInitStructure.TIM_Prescaler = 1-1; TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICFilter = 0xF; TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_Cmd(TIM3,ENABLE);
}
int16_t Encoder_Get(void) { int16_t Temp; Temp = TIM_GetCounter(TIM3); TIM_SetCounter(TIM3,0); return Temp;
}
喔,我以为是那种用GPIO中断检测的普通的旋转编码器。这种要用配置定时器的特殊需求,还是用标准库实现,这样最灵活。 因为需要保证不同平台的可移植性和可维护性,所以只封装满足最常用的需求API。