代码如下:
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,电机就转动一圈。不过貌似程序有点问题,电机怎么也停不下来,求帮助啊。万分感谢!
一周热门 更多>