void BLDC_TIM3Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //基本结构体变量定义
TIM_ICInitTypeDef TIM_ICInitStructure; //定义结构体变量
TIM_OCInitTypeDef TIM_OCInitStructure; //输出结构体变量定义
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Prescaler = 71; //TIM基本初始化
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period =65535;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //选择通道1
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //输入上升沿捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC; //配置通道为输入,并映射到哪里
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //输入捕获预分频值
TIM_ICInitStructure.TIM_ICFilter = 10; //输入滤波器带宽设置
TIM_ICInit(TIM3, &TIM_ICInitStructure); //输入通道配置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道初始化
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse =1023;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //TIM输出通道初始化
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse =65535;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
TIM_SelectHallSensor(TIM3,ENABLE); //使能TIMx的霍尔传感器接口
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1F_ED); //输入触发源选择
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); //从模式选择
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);//主从模式选择
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_OC2Ref); //选择输出触发模式(TRGO端)
TIM_ITConfig(TIM3, TIM_IT_Trigger|TIM_IT_CC4, ENABLE); //开定时器中断
//TIM_Cmd(TIM3,ENABLE);
}
这是前人写的一个无刷直流电机霍尔检测程序,TIM3触发产生COM事件,使TIM1换相。抛开程序不谈,按我的理解,霍尔检测定时器TIM3的三个通道CH1、CH2、CH3都应该设置成输入检测。但这段程序却是把CH1设置成输入捕获,CH2设置成输出PWM波,CH3没有设置,CH4设置成冻结模式,很不理解。请大虾们指点!TIM3触发产生COM事件,使TIM1换相。 |