stm32关于利用定时器计数脉冲个数的问题,请大家帮助!
时间:10-02
整理:3721RD
点击:
代码如下:
void DJ_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ClocksTypeDef RCC_ClockFreq;
SystemInit();
RCC_GetClocksFreq(&RCC_ClockFreq);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,
ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|
RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void DJ_BH_MODE_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode =
TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_div1
;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_Direct
TI;
TIM_ICInitStructure.TIM_ICFilter=0x0;
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF,
TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE);
}
小弟想利用编码器进行电机测速,定时器设置成外部时钟源,然后用计数器对编码器输出的脉冲进行计数,计数到334,电机就转动一圈。不过貌似程序有点问题,电机怎么也停不下来,求帮助啊。万分感谢!
void DJ_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ClocksTypeDef RCC_ClockFreq;
SystemInit();
RCC_GetClocksFreq(&RCC_ClockFreq);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,
ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|
RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void DJ_BH_MODE_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode =
TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_div1
;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_Direct
TI;
TIM_ICInitStructure.TIM_ICFilter=0x0;
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF,
TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE);
}
小弟想利用编码器进行电机测速,定时器设置成外部时钟源,然后用计数器对编码器输出的脉冲进行计数,计数到334,电机就转动一圈。不过貌似程序有点问题,电机怎么也停不下来,求帮助啊。万分感谢!
你这个没有主程序啊,只让我们看看,这几个模块的配置吗,
表以为我们都可以 以管窥天
不太了解,呵呵。
建议吧主程序搞出来看看 ,这样是不可解决问题的。