void CoderTimeInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure; //定义结构体变量
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //接收编码器脉冲A波B波的引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO设置
TIM_DeInit(TIM3); //将TIM3设置为默认缺省值
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; //设置预分频器分频系数
TIM_TimeBaseStructure.TIM_Period = 65535-1; //设置自动重载计数周期值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分频因子
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //设置计数方式
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //初始化定时器TIM3参数
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising,TIM_ICPolarity_Rising); //配置定时器TIM3的编码器接口
TIM_ICStructInit(&TIM_ICInitStructure); // 把TIM_ICInitStruct中的每一个参数按缺省值(默认值)填入
TIM_ICInitStructure.TIM_ICFilter = 6; //比较滤波器,实质是设置了TIMx->CCMRx寄存器的IC1F[3:0]位
TIM_ICInit(TIM3,&TIM_ICInitStructure); //根据TIM_ICInitStruct中指定的参数初始化外设TIM4
TIM_Cmd(TIM3,ENABLE);
程序里没有说PA6一定与A波对应,我把接线反过来接就程序运行就不对了,这是为什么啊?新手,对这一块不太了解。
|