大学生
最后登录1970-1-1
在线时间 小时
注册时间2016-5-16
|
楼主 |
发表于 2016-11-1 21:01:20
|
显示全部楼层
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//TIM4时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);//使能PORTA时钟
/* 定时器复用引脚 */
GPIO_PinAFConfig( GPIOB,GPIO_Pin_6,GPIO_AF_TIM4);
GPIO_PinAFConfig( GPIOB,GPIO_Pin_7,GPIO_AF_TIM4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7;//PB6,PB7
// GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_DeInit(TIM4);//重新将Timer设置为缺省值
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 2-1; // 不分频
TIM_TimeBaseStructure.TIM_Period =0xFFFF;
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1;//选择时钟分频:不分频
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up; //向上计数模式
//(当配置为中央对齐模式或者编码器模式时,此位只读)
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//TI1和TI2的输入均在上升沿和下降沿有效
//选择模式 :两相计数(速度和方向)
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;//选择通道1
// TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x03;//0011 对应8个事件输出一个跳变
TIM_ICInit(TIM4,&TIM_ICInitStructure);
// TIM_PWMIConfig(ADVANCE_TIM, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//选择通道2
// TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//映射到 TI2 上
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x03;
TIM_ICInit(TIM4,&TIM_ICInitStructure);
// TIM_ICPreloadConfig(ADVANCE_TIM, TIM_OCPreload_Enable);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);//清除TIM的更新标志位
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_SetCounter(TIM4,0);
TIM_Cmd(TIM4, ENABLE); |
|