最近下的一个关于stm32控制无刷直流电机的程序,通过以下2个定时器分别产生PWM和接受霍尔信号,但是本人刚接触stm32,下面的程序看不懂,希望高手能详细解释下,万分感谢。
void BLDC_TIM1Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //基本结构体变量定义
TIM_OCInitTypeDef TIM_OCInitStructure; //输出结构体变量定义
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //死区刹车结构体变量定义
TIM_DeInit(TIM1);
/*PWM f = 72MHZ/(3*3000) = 8kHZ */
TIM_TimeBaseStructure.TIM_Prescaler = 3 - 1; //TIM基本初始化
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;//中央对齐计数模式 向上计数时被设置
TIM_TimeBaseStructure.TIM_Period = 750-1; //PWM 16K
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道初始化
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = (unsigned int)AD_value;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = (unsigned int)AD_value;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = (unsigned int)AD_value;
TIM_OC3Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道4初始化,用来触发AD注入采样
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse =745;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM1,&TIM_OCInitStructure);
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //死区刹车初始化
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 100;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //如打开,开机无输出且状态紊乱
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道1)
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道2)
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道3)
TIM_SelectInputTrigger(TIM1, TIM_TS_ITR1); //输入触发源选择TIM2
TIM_CtrlPWMOutputs(TIM1,ENABLE);
void BLDC_TIM2Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //基本结构体变量定义
TIM_ICInitTypeDef TIM_ICInitStructure; //定义结构体变量
TIM_OCInitTypeDef TIM_OCInitStructure; //输出结构体变量定义
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //TIM基本初始化
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period =65535;
TIM_TimeBaseStructure.TIMClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&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(TIM2, &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(TIM2,&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(TIM2,&TIM_OCInitStructure);
TIM_SelectHallSensor(TIM2,ENABLE); //使能TIMx的霍尔传感器接口
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1F_ED); //输入触发源选择
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //从模式选择
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//主从模式选择 |