关于stm32控制无刷直流电机的问题!
时间:10-02
整理:3721RD
点击:
定时1中断由定时3的3路霍尔传感器异或触发,
求大神给我分析下这样配置出什么问题了?
/*******************************************************************************
* 函数名 :TIM_Configuration * 输出结果 :None
* 函数描述 :设置TIM各通道 * 返回值 :None
* 输出参数 :None
*******************************************************************************/
void BLDC_TIM1Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //基本结构体变量定义
TIM_OCInitTypeDef TIM_OCInitStructure; //输出结构体变量定义
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //死区刹车结构体变量定义
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler = 2; //TIM基本初始化
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;//中央对齐计数模式
TIM_TimeBaseStructure.TIM_Period = 1499; //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_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse =1400;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =1400;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =1400;
TIM_OC3Init(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_Enable;
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_ITR2); //输入触发源选择TIM3
//TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Trigger);//从模式选择 触发
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
/**********************************************************************
* Description : None
* Input : None
* Output : None
* Return : None
* Attention : None
**********************************************************************/
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_Configuration * 输出结果 :None
* 函数描述 :设置TIM各通道 * 返回值 :None
* 输出参数 :None
*******************************************************************************/
void BLDC_TIM1Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //基本结构体变量定义
TIM_OCInitTypeDef TIM_OCInitStructure; //输出结构体变量定义
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //死区刹车结构体变量定义
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler = 2; //TIM基本初始化
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;//中央对齐计数模式
TIM_TimeBaseStructure.TIM_Period = 1499; //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_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse =1400;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =1400;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =1400;
TIM_OC3Init(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_Enable;
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_ITR2); //输入触发源选择TIM3
//TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Trigger);//从模式选择 触发
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
/**********************************************************************
* Description : None
* Input : None
* Output : None
* Return : None
* Attention : None
**********************************************************************/
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); //开定时器中断
}
这个需要根据手册看一下TIME是否试着正确 参考一下代码
- 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);//主从模式选择
自己做的STM32 BLDC直流无刷电机控制器:http://www.amobbs.com/thread-5202426-1-1.html
你现在的问题是什么呢?给出一段长代码让别人分析吗?要先定位问题才能解决问题
时钟不用使能吗?