我用的是 STM32F407VET6核心板
想用定时器的编码器模式测 增量AB式编码器的速度 编码器为5V 供电 在配置完之后 发现没有反馈 希望大神能帮我看看代码 谢啦。。
//前左的编码器初始化 //****** PA0 TIM5_CH1 PA1 TIM5_CH2****** //
void ENC_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Encoder unit connected to TIM5, 4X mode */
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM4 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
/* Enable GPIOD, clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure); //将GPIO_InitStruct中的参数按缺省值输入
/* Configure PD.12,13 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL; // 输入浮空
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable the TIM3 Update Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Timer configuration in Encoder mode */
TIM_DeInit(ENCODER_Q1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_Q1, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(ENCODER_Q1, TIM_EncoderMode_TI12,
TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge); // ???
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; // 什么作用
TIM_ICInit(ENCODER_Q1, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(ENCODER_Q1, TIM_FLAG_Update);
TIM_ITConfig(ENCODER_Q1, TIM_IT_Update, ENABLE);
//Reset counter
TIM5->CNT = COUNTER_RESET;
TIM_Cmd(ENCODER_Q1, ENABLE);
}
void TIM5_IRQHandler(void) // 轮胎完成一圈旋转
{
/* Clear the interrupt pending flag */
TIM_ClearFlag(ENCODER_Q1, TIM_FLAG_Update);
}
int speedMeasure2()
{
int count;
TIM5->CNT = COUNTER_RESET;
TIM_Cmd(TIM5, ENABLE);
delay_ms(100); // 100ms
count = TIM5->CNT;
TIM_Cmd(TIM5, DISABLE);
count = count - COUNTER_RESET;
return count;//返回负数,直接表示反转了
}
main()
{
其他的函数略。。
void ENC_Init(void);
while(1)
{
delay_ms(100);
FeedBack_Count=speedMeasure2();
OLED_ShowNum(10,40,FeedBack_Count,5,12);
OLED_Refresh_Gram();//更新显示到OLED
}
}
你的意思是我的程序写的没有问题 是硬件出问题啦?。那怎么输入捕获下没事? 还有我的编码器的输出波形我看了一下,波形很不错啊
改为复用之后还是输入浮空?
一周热门 更多>