stm32f103c8t6的定时器4设为输入捕获模式,但无法正确都读电机编码器的值,只有0和1,怎么办?
tim2的设置和tim4完全一样,tim2可以正常读取
Ecoder.c
void Encoder_TIM2_Init(void) //定时器3编码器初始化
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
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 = 65535; //ARR(0~65535) 1s
TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //PSC(0~65535) 定时频率=72M/(PSC+1)/(ARR+1)
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器(高级计数器特有)
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//配置编码器模式
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICStructInit(&TIM_ICInitStructure);//初始化输入捕获
TIM_ICInitStructure.TIM_ICFilter=10;
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//配置溢出更新中断标志位
TIM_SetCounter(TIM2,0);//清零定时器计数值
TIM_Cmd(TIM2,ENABLE);
}
void Encoder_TIM4_Init(void) //定时器4编码器初始化
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStructure.TIM_Period = 65535; //ARR(0~65535) 1s
TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //PSC(0~65535) 定时频率=72M/(PSC+1)/(ARR+1)
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器(高级计数器特有)
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//配置编码器模式
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICStructInit(&TIM_ICInitStructure);//初始化输入捕获
TIM_ICInitStructure.TIM_ICFilter=10;
TIM_ICInit(TIM4,&TIM_ICInitStructure);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);//配置溢出更新中断标志位
TIM_SetCounter(TIM4,0);//清零定时器计数值
TIM_Cmd(TIM4,ENABLE);
}
/**********************
编码器
速度读取函数
入口参数:定时器
**********************/
int Read_Speed(int TIMx)
{
int value_1;
switch(TIMx)
{
case 2: value_1= (short)TIM_GetCounter(TIM2);TIM_SetCounter(TIM2,0);break;
case 4: value_1= (short)TIM_GetCounter(TIM4);TIM_SetCounter(TIM4,0);break;
}
return value_1;
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
}
}