2019-12-16 22:35发布
brumby 发表于 2015-11-18 14:11 计数值溢出了吧? 应该还有个处理的,在毫秒中断中比较计数器的值(上次和本次的值),推断编码器的方向, ...
最多设置5个标签!
//部分设置贴出来,大家讨论讨论
下面的两条语句谁明白?
1. TIM_ICInitStructure.TIM_ICFilter = 6;
2. TIM_EncoderInterfaceConfig(TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
static void Encoder_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// #ifdef TIM2_ON
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_TIM2);
// #endif
// #ifdef TIM3_ON
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3);
// #endif
// #ifdef TIM4_ON
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
// #endif
}
char EncoderConfig(TIM_TypeDef* TIMx, uint16_t TIM_Period, uint16_t TIM_Prescaler)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler =TIM_Prescaler-1; // No prescaling
TIM_TimeBaseStructure.TIM_Period = TIM_Period-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(TIMx, &TIM_ICInitStructure);
// Clear all pending interrupts
// TIM_ClearFlag(TIMx, TIM_FLAG_Update);
// TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE);
//TIM_UpdateRequestConfig( TIM7, TIM_UpdateSource_Regular); ²éÒ»²é
TIM_Cmd(TIMx, ENABLE);
return 0;
}
这个考虑过了,我计数到60000是重新装载的,EncoderConfig(TIM2, 60000, 1);
下面是得到一个编码器的值的过程
NextCnt = TIM_GetCounter(TIM2);
Cnt = NextCnt - LastCH1Cnt;
if(Cnt>30000)
{
Cnt -= 60000;
}
else if(Cnt<-30000)
{
Cnt += 60000;
}
LastCH1Cnt = NextCnt;
CH1EncoderCnt += Cnt;
TIM_ICInitStructure.TIM_ICFilter = 0x0f;
TIM_ICInit(TIMx, &TIM_ICInitStructure);
滤波设置很重要,现在测试4小时来回正弦运动2HZ两度约2*1000*4(线)*5.5(传动比,或8.6)/360,肉眼分辨不丢了。明天测试下高速运转状态下结果。
话说回来,谁知道这个滤波是如何算的?看懂下面连接的,回复下,这个滤波还是很重要的。
http://wenku.baidu.com/link?url= ... nJDXgngm7b7fhMJUiY3
一周热门 更多>