微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 关于stm32控制无刷直流电机的问题!

关于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);      //开定时器中断

}


这个需要根据手册看一下TIME是否试着正确 参考一下代码

  1. void BLDC_TIM1Config(void)
  2. {
  3.      TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;                       //基本结构体变量定义
  4.    TIM_OCInitTypeDef                  TIM_OCInitStructure;               //输出结构体变量定义
  5.    TIM_BDTRInitTypeDef          TIM_BDTRInitStructure;          //死区刹车结构体变量定义

  6.    TIM_DeInit(TIM1);
  7.    /*PWM f = 72MHZ/(3*3000) = 8kHZ */
  8.    TIM_TimeBaseStructure.TIM_Prescaler = 3 - 1;                   //TIM基本初始化
  9.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;//中央对齐计数模式 向上计数时被设置
  10.   TIM_TimeBaseStructure.TIM_Period = 750-1;                         //PWM 16K
  11.    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  12.    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  13.    TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
  14.    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;                    //TIM输出通道初始化
  15.   TIM_OCInitStructure.TIM_OutputState  = TIM_OutputState_Disable;
  16.    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;                  
  17.    TIM_OCInitStructure.TIM_Pulse = (unsigned int)AD_value;
  18.    TIM_OCInitStructure.TIM_OCPolarity   = TIM_OCPolarity_High;
  19.    TIM_OCInitStructure.TIM_OCNPolarity  = TIM_OCNPolarity_High;         
  20.    TIM_OCInitStructure.TIM_OCIdleState  = TIM_OCIdleState_Set;
  21.    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;           
  22.    TIM_OC1Init(TIM1,&TIM_OCInitStructure);

  23.    TIM_OCInitStructure.TIM_Pulse = (unsigned int)AD_value;
  24.    TIM_OC2Init(TIM1,&TIM_OCInitStructure);

  25.    TIM_OCInitStructure.TIM_Pulse = (unsigned int)AD_value;
  26.    TIM_OC3Init(TIM1,&TIM_OCInitStructure);

  27.    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;             //TIM输出通道4初始化,用来触发AD注入采样
  28.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;                  
  29.    TIM_OCInitStructure.TIM_Pulse =745;
  30.    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;           
  31.    TIM_OC4Init(TIM1,&TIM_OCInitStructure);

  32.    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;        //死区刹车初始化
  33.   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  34.    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  35.    TIM_BDTRInitStructure.TIM_DeadTime = 100;
  36.    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;         //如打开,开机无输出且状态紊乱
  37.   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
  38.    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
  39.    TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);

  40.    TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);       //使能捕获比较寄存器预装载(通道1)
  41.   TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);         //使能捕获比较寄存器预装载(通道2)
  42.   TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);         //使能捕获比较寄存器预装载(通道3)
  43.    
  44.   TIM_SelectInputTrigger(TIM1, TIM_TS_ITR1);        //输入触发源选择TIM2   
  45.     TIM_CtrlPWMOutputs(TIM1,ENABLE);
  46. void BLDC_TIM2Config(void)
  47. {
  48.    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;                   //基本结构体变量定义
  49.    TIM_ICInitTypeDef                 TIM_ICInitStructure;               //定义结构体变量
  50.    TIM_OCInitTypeDef                  TIM_OCInitStructure;               //输出结构体变量定义
  51.       
  52.    TIM_DeInit(TIM2);

  53.    TIM_TimeBaseStructure.TIM_Prescaler = 72-1;                   //TIM基本初始化
  54.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  55.    TIM_TimeBaseStructure.TIM_Period =65535;
  56.    TIM_TimeBaseStructure.TIMClockDivision = 0;
  57.    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  58.    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);     
  59.       
  60.    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;            //选择通道1
  61.    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //输入上升沿捕获  
  62.    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC;  //配置通道为输入,并映射到哪里
  63.    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_div1;       //输入捕获预分频值
  64.    TIM_ICInitStructure.TIM_ICFilter = 10;                      //输入滤波器带宽设置
  65.    TIM_ICInit(TIM2, &TIM_ICInitStructure);                     //输入通道配置

  66.    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;             //TIM输出通道初化
  67.    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;            
  68.    TIM_OCInitStructure.TIM_Pulse =1023;
  69.    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;      
  70.    TIM_OC2Init(TIM2,&TIM_OCInitStructure);

  71.    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;                     //TIM输出通道初始化
  72.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;            
  73.    TIM_OCInitStructure.TIM_Pulse =65535;
  74.    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;      
  75.    TIM_OC4Init(TIM2,&TIM_OCInitStructure);

  76.    TIM_SelectHallSensor(TIM2,ENABLE);                          //使能TIMx的霍尔传感器接口
  77.   TIM_SelectInputTrigger(TIM2, TIM_TS_TI1F_ED);               //输入触发源选择   
  78. TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);             //从模式选择
  79. TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//主从模式选择     

复制代码

自己做的STM32 BLDC直流无刷电机控制器:http://www.amobbs.com/thread-5202426-1-1.html

你现在的问题是什么呢?给出一段长代码让别人分析吗?要先定位问题才能解决问题

时钟不用使能吗?

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top