初级会员

- 积分
- 60
- 金钱
- 60
- 注册时间
- 2013-1-6
- 在线时间
- 10 小时
|
1金钱
void TIM3_Mode_Config(void)
{
//u16 CCR1_Val = 2500;
//u16 CCR2_Val = 1000;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
/*----------------------------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*----------------------------------------------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3
TIM_DeInit(TIM3);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period =0xffff; //
TIM_TimeBaseStructure.TIM_Prescaler =0; //设置预分频:
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ; //设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
/*初始化TIM2定时器 */
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/*-----------------------------------------------------------------*/
//编码配置 编码模式
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising ); //TIM_ICPolarity_Rising上升沿捕获
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6; //比较滤波器
TIM_ICInit(TIM3, &TIM_ICInitStructure);
//TIM_ARRPreloadConfig(TIM3, ENABLE);
// Clear all pending interrupts
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能中断
//Reset counter
TIM3->CNT =2000;
TIM_Cmd(TIM3, ENABLE); //使能定时器3
}
void TIM_Init(void)
{
TIM3_Mode_Config();
}
TIM_GetCounter(TIM3)可以正确读取TIM3的计数值,若想单纯把左转右转当成两个按键用 有没有寄存器直接读取正反转方向?或者有啥好点的方法??
|
|