在实验1的电机驱动基础上,这里补充编码器测速的程序部分
#include "encoder.h"
#include "led.h"
///TIM4 编码器输入,TIM4_CH1作A相,TIM4_CH2作B相GPIO_Pin_6|GPIO_Pin_7;
TIM_ICInitTypeDef TIM_ICInitStructure;
void TIM4_Encoder_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);// 使能定时器时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//io口使能使用到的捕获编码器AB相借,
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);
//推挽输出 ,IO口速度为50MHz
//初始化TIM4的时机单元
TIM_TimeBaseStructure