采用TIM的霍尔模式,只能捕获霍尔的变化,不能读取计数器的值。配置程序如下
static void HALL_TIMx_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure;
/* 使能定时器时钟 */
HALL_TIM_APBxClock_FUN(HALL_TIM_CLK,ENABLE);
/* 定时器基本参数始终 */
/* 定时周期: HALL_TIM_Period+1 */
TIM_TimeBaseStructure.TIM_Period = HALL_TIM_PERIOD;
/* 设置预分频:HALL_TIM_Prescaler,输出脉冲频率:72MHz/(HALL_TIM_Prescaler+1)/(HALL_TIM_Period+1) */
TIM_TimeBaseStructure.TIM_Prescaler = HALL_TIM_PRESCALER;
/* 设置时钟分频系数:不分频(这里用不到) */
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
/* 向上计数模式 */
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up ;
TIM_TimeBaseInit(HALL_TIMx, &TIM_TimeBaseStructure);
/* 初始化TIM5输入捕获参数 */
/* CC1S=01 选择输入端 IC1映射到TI1上 */
TIM_ICInitStructure.TIM_Channel = HALL_TIM_Channel_x;
/* 上升沿捕获 */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
/* 映射到TI1上 */
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC;
/* 配置输入分频,不分频 */
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
/* IC1F=0000 配置输入滤波器 不滤波 */
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(HALL_TIMx, &TIM_ICInitStructure);
// TIM4->CCMR1
TIM4->CCER&=0xFFEF;
TIM4->CCMR1|=0x700;
TIM4->CCMR1&=0xFCFF;
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道初始化 OC2M
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //CCR2
// TIM_OCInitStructure.TIM_Pulse =0;
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
// TIM_OC2Init(HALL_TIMx,&TIM_OCInitStructure);//此处是OCx代表配置通道x
/* 配置NVIC */
HALL_TIMx_NVIC_Configuration();
TIM_SelectHallSensor(HALL_TIMx,ENABLE); //使能TIMx的霍尔传感器接口
TIM_SelectInputTrigger(HALL_TIMx, TIM_TS_TI1F_ED); //输入触发源选择
TIM_SelectSlaveMode(HALL_TIMx, TIM_SlaveMode_Reset); //从模式选择
TIM_SelectMasterSlaveMode(HALL_TIMx, TIM_MasterSlaveMode_Enable); //主从模式选择
/* 允许更新中断 ,允许CC1IE捕获中断 */
TIM_ITConfig(HALL_TIMx, TIM_IT_Trigger, ENABLE);
/* 使能定时器 */
TIM_Cmd(HALL_TIMx, ENABLE);
TIM_ClearITPendingBit (HALL_TIMx,TIM_IT_Trigger);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>