最近想用TIM8编码器模式来作为电机的速度反馈,但设置了很久一直都没有数值,用TIM2-TIM5都已经设置成功,唯独TIM8不能正常采集,以及设置了一整天都不行,很头疼,希望设置过的大佬能够指点一点。谢谢!以下附上代码。
//TIM 8编码器初始化
void Encoder_Init_TIM8(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);// TIM8 使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能GPIO C外设时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;//初始化 PA0 PA1 IO口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_DeInit(TIM8);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD-1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;//ICx_FILTER;
TIM_ICInit(TIM8, &TIM_ICInitStructure);
TIM_ClearFlag(TIM8, TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_SetCounter(TIM8, 0);
TIM_Cmd(TIM8, ENABLE);
}
//定义读取计时器数值函数
int Read_Encoder(u8 TIMX)
{
int Encoder_TIM;
switch(TIMX)
{
case 3: Encoder_TIM= (short)TIM3 -> CNT; TIM3 -> CNT=0;break;
case 4: Encoder_TIM= (short)TIM4 -> CNT; TIM4 -> CNT=0;break;
case 5: Encoder_TIM= (short)TIM5 -> CNT; TIM5 -> CNT=0;break;
case 8: Encoder_TIM= (short)TIM8 -> CNT; TIM8 -> CNT=0;break;
default: Encoder_TIM=222222;
}
return Encoder_TIM;
}
希望有人能够指点一下,谢谢
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>