这2周时实训,要求做一个控制旋转角度,查阅资料发现需要用到编码器,
便去买了个编码器,做了一个控制角度的程序
脉冲计数程序:
void TIM5_Cap1_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitTypeDefstrue;
TIM_ICInitTypeDef TIM_ICInitTypeDefstrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
GPIO_InitTypeDefstrue.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitTypeDefstrue.GPIO_Pin=GPIO_Pin_0;
GPIO_InitTypeDefstrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitTypeDefstrue);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
TIM_ICInitTypeDefstrue.TIM_Channel=TIM_Channel_1;
TIM_ICInitTypeDefstrue.TIM_ICFilter=2;
TIM_ICInitTypeDefstrue.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitTypeDefstrue.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitTypeDefstrue.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM5,&TIM_ICInitTypeDefstrue);
NVIC_InitStructure.NVIC_IRQChannel=TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStructure);
<