本帖最后由 wzrde21 于 2016-5-27 16:08 编辑
使用TIM4编码器模式驱动EC11,初始化函数如下void EC11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能TIM4时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //EC11A MOD B A
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //普通输入模式
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //定时器4中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //中断使能
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 1333; //设定计数器重装值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM4时钟预分频值
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising ,TIM_ICPolarity_Rising); //使用编码器模式3,
TIM_ICStructInit(&TIM_ICInitStructure); //将结构体中的内容缺省输入
TIM_ICInitStructure.TIM_ICFilter = 1; //选择输入比较滤波器
TIM_ICInit(TIM4, &TIM_ICInitStructure); //将TIM_ICInitStructure中的指定参数初始化TIM3
TIM_ClearFlag(TIM4, TIM_FLAG_Update); //清除TIM3的更新标志位
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //开启TIM3更新标志使能
TIM_SetCounter(TIM4,0); //
TIM_Cmd(TIM4, ENABLE); //启动TIM3定时器
}
初始化结束后再main中循环读取计数器的值,但是没有变化
int main(void)
{
// u8 i;
u32 n_Counter;
delay_init(168);
KEY_Init();
EC11_Init();
while (1)
{
// i=KEY_Scan(1);
n_Counter = TIM_GetCounter(TIM4);
n_Counter =n_Counter;
delay_ms(500);
}
}
循环读取计数器的值,但是没有变化
示波器能看到输入引脚的波形 波形没问题
原理图如下
求各位大神解答
|