标题:
stm32定时器1设置编码器接口模式后,计数寄存器的值不正常
[打印本页]
作者:
电子小萌
时间:
2020-4-3 15:10
标题:
stm32定时器1设置编码器接口模式后,计数寄存器的值不正常
不知道是程序问题还是啥问题,求帮帮忙看看指点一下
void Coder_Configure(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_TIM1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_DeInit(TIM1); //Çå3yòÔǰÅäÖÃ
TIM_TimeBaseInitStructure.TIM_Period = arr; //Tout= ((arr+1)*(psc+1))/Tclk
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
//éèÖñàÂëÆ÷Ä£ê½
TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising,
TIM_ICPolarity_Rising);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_SetCounter(TIM1, 0);
TIM_Cmd(TIM1,ENABLE);
}
void Coder_Configure1(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_DeInit(TIM4); //Çå3yòÔǰÅäÖÃ
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
//éèÖñàÂëÆ÷Ä£ê½
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising,
TIM_ICPolarity_Rising);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_SetCounter(TIM4, 0);
TIM_Cmd(TIM4,ENABLE);
}
uint32_t Coder_GetValue(TIM_TypeDef *timx)
{
return TIM_GetCounter(timx);
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1