使用的是103系列,将PA0和PA1作为编码器输入应用,目前出现的问题是配置后定时器2的寄存器一直为0没响应,但是将同样的配置给定时器1寄存器就能赋值,配置如下:
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM2_TimeBaseStructure;
TIM_ICInitTypeDef TIM2_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能TIM2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
GPIO_StructInit(&GPIO_InitStructure);//将GPIO_InitStruct中的参数按缺省值输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit(&TIM2_TimeBaseStructure);
TIM2_TimeBaseStructure.TIM_Period = 360 * 4 -1; //设定计数器重装值
TIM2_TimeBaseStructure.TIM_Prescaler = 0; //TIM2时钟预分频值
TIM2_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;//设置时钟分割 T_dts = T_ck_int
TIM2_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(TIM2, &TIM2_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12,
TIM_ICPolarity_BothEdge ,
TIM_ICPolarity_BothEdge);//使用编码器模式3,上升下降都计数
TIM_ICStructInit(&TIM2_ICInitStructure); //将结构体中的内容缺省输入
TIM2_ICInitStructure.TIM_ICFilter = 6; //选择输入比较滤波器
TIM_ICInit(TIM2, &TIM2_ICInitStructure); //将TIM_ICInitStructure中的指定参数初始化TIM2
TIM_ClearFlag(TIM2, TIM_FLAG_Update); //清除TIM2的更新标志位
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //运行更新中断
//Reset counter
TIM2->CNT = 0;
TIM_Cmd(TIM2, ENABLE); //启动TIM2定时器
实在找不出问题出在哪里,求助了 |