STM32F030F4 BLDC

Posted beiyhs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32F030F4 BLDC相关的知识,希望对你有一定的参考价值。

//==文件time1.h============================================================
#ifndef _TIME1_H_
#define _TIME1_H_
 
#include stm32f0xx.h
 
//========定义PWM的频率================================================
#define         DEF_PWMFRE          DEF_PWMFRE_16K
#define         DEF_PWMFRE_8K           8000 //计数周期值为48000000/8000=6000
#define         DEF_PWMFRE_16K      16000//计数周期值为48000000/16000=3000 0.02U/N
#define         DEF_PWMFRE_20K      20000//计数周期值为48000000/20000=2400
#define         DEF_PWMFRE_25K      25000//计数周期值为48000000/16000=1920
#define         DEF_PWMFRE_30K      30000//计数周期值为48000000/16000=1600
 
 
//========PWM1 2 3通道输出使能位操作=====================================
#define     PWMA_Enb    TIM1->CCER |= (uint16_t)(((uint16_t)TIM_CCER_CC1E))
#define     PWMA_Dis    TIM1->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC1E))
#define     PWMB_Enb    TIM1->CCER |= (uint16_t)(((uint16_t)TIM_CCER_CC2E))
#define     PWMB_Dis    TIM1->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC2E))
#define     PWMC_Enb    TIM1->CCER |= (uint16_t)(((uint16_t)TIM_CCER_CC3E))
#define     PWMC_Dis    TIM1->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC3E))
#define     PWMD_Enb    TIM1->CCER |= (uint16_t)(((uint16_t)TIM_CCER_CC4E))
#define     PWMD_Dis    TIM1->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC4E))
 
#define     PWMABC_Dis TIM1->CCER &= (uint16_t)(~((uint16_t)(TIM_CCER_CC1E|TIM_CCER_CC2E|TIM_CCER_CC3E)))
 
//========PWM1 2 3通道互补输出使能位操作=====================================
#define     PWMAN_Enb   TIM1->CCER |= (uint16_t)(((uint16_t)TIM_CCER_CC1NE))
#define     PWMAN_Dis   TIM1->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC1NE))
#define     PWMBN_Enb   TIM1->CCER |= (uint16_t)(((uint16_t)TIM_CCER_CC2NE))
#define     PWMBN_Dis   TIM1->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC2NE))
#define     PWMCN_Enb   TIM1->CCER |= (uint16_t)(((uint16_t)TIM_CCER_CC3NE))
#define     PWMCN_Dis   TIM1->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC3NE))
#define     PWMDN_Enb   TIM1->CCER |= (uint16_t)(((uint16_t)TIM_CCER_CC4NE))
#define     PWMDN_Dis   TIM1->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC4NE))
 
 
#define     PWMABCN_Enb TIM1->CCER |= (uint16_t)(((uint16_t)(TIM_CCER_CC1NE|TIM_CCER_CC2NE|TIM_CCER_CC3NE)))
#define     PWMABCN_Dis TIM1->CCER &= (uint16_t)(~((uint16_t)(TIM_CCER_CC1NE|TIM_CCER_CC2NE|TIM_CCER_CC3NE)))
 
 
//========使能CC4 比较中断=====================================
#define     TIM1_CMP4IEN_ENB            TIM1->DIER |= TIM_IT_CC4  //((uint16_t)0x0010)
#define     TIM1_CMP4IEN_DIS            TIM1->DIER &= ~TIM_IT_CC4
 
////========使能CC4 更新中断=====================================
//#define       TIM1_UpdateIEN_ENB          TIM1->DIER |= TIM_IT_Update  //((uint16_t)0x0010)
//#define       TIM1_UpdateIEN_DIS          TIM1->DIER &= ~TIM_IT_Update
 
//=========比较4中断进入的时间点=================================
#define     V_CCP4PW                150 //
 
 
//===================================================
 
extern void TIM1_GPIO_Init(void) ;
extern void TIM1_PWM_Config(void);
extern void ToPwmValue(uint16 Tcon) ;
extern void BLDC_Bak(uint16_t PwmDuty) ;
extern void BLDC_Bak_End(void) ;
 
extern void TIM1_CCP4NVIC_Config(void) ;
extern void ToCCP4PwmValue(uint16 PwmDuty) ;
 
#endif  /* _TIME1_H_ */
 
 
 
//==文件time1.c============================================================
#include global.h
#include time1.h
 
 
TIM_TimeBaseInitTypeDef  TIM_Time1BaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
 
uint16_t TimerPeriod = 0;
 
 
//#define       DEF_PWM_NCP_ENB             1       //PWM互补使能定义
 
/*******************************************************************************
* 函数名称: TIM1_GPIO_Init
* 功能描述: TIM1--GPIO引脚配置---在此可以设置6路PWM输出
* 输入参数: void
* 返回参数: 无
********************************************************************************/
void TIM1_GPIO_Init(void)
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* 使能GPIO时钟 */
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
   
  /* 配置GPIO管脚复用  PWM1 PWM2 PWM3 */
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;//|GPIO_Pin_11
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;//GPIO_PuPd_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   
 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2); //开启PA8复用功能
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2); //开启PA9复用功能
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2); //开启PA10复用功能
//  GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
 
//----------下管 PA7 PB0 PB1 配置---------------------------------------------------
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;//
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   
   
//----------------------------------------------------------------------------------- 
  /* 配置GPIO管脚  PWM2 PWM3 互补GPIO   PB0 PB1 */
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;//||GPIO_Pin_11
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);     
 
/*******************************************************************************
* 函数名称: GPIO_DOWNPIN_ConfigRst
* 功能描述: BLDC下管IO 配置为正常模式
* 输入参数: void
* 返回参数: 无
********************************************************************************/
void GPIO_DOWNPIN_AFConfigRst(void)
    GPIO_InitTypeDef GPIO_InitStructure;
     
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;//
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   
   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;//||GPIO_Pin_11
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);   
         
/*******************************************************************************
* 函数名称: GPIO_DOWNPIN_AFConfig
* 功能描述: BLDC下管IO 配置为AF 模式
* 输入参数: void
* 返回参数: 无
********************************************************************************/
void GPIO_DOWNPIN_AFConfig(void)
    GPIO_InitTypeDef GPIO_InitStructure;
     
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;//
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   
   
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2); //
     
    //----------------------------------------------------------
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;//||GPIO_Pin_11
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);    
   
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_2); //
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_2) ;
 
/*******************************************************************************
* 函数名称: TIM1_PWM_Config
* 功能描述: TIM1-PWM配置
* 输入参数: void
* 返回参数: 无
********************************************************************************/
void TIM1_PWM_Config(void)
    /* TIM1 的配置 ---------------------------------------------------
   TIM1 输入时钟(TIM1CLK) 设置为 APB2 时钟 (PCLK2)   
    => TIM1CLK = PCLK2 = SystemCoreClock
   TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
   SystemCoreClock 为48 MHz
    
   我们的目标产生 4 路PWM 信号在17.57 KHz:
     - TIM1_Period = (SystemCoreClock / 17570) - 1
   信道1设置的占空比为 50%
   信道2设置的占空比为 37.5%
   信道3设置的占空比为 25%
   信道4设置的占空比为 12.5%
   定时器脉冲的计算方式如下:
     - ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
    */ 
    /*计算预定表的值,也就是多少个时钟计数为一个周期*/
  //TimerPeriod = (SystemCoreClock / 17570 ) - 1;
  //TimerPeriod = (SystemCoreClock / DEF_PWMFRE ) - 1;
  TimerPeriod = (SystemCoreClock / DEF_PWMFRE);
  /* TIM1 时钟使能 */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
   
  /* Time 定时基础设置*/
  TIM_Time1BaseStructure.TIM_Prescaler = 0;
  TIM_Time1BaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  /* Time 定时设置为上升沿计算模式*/
  TIM_Time1BaseStructure.TIM_Period = TimerPeriod;
  TIM_Time1BaseStructure.TIM_ClockDivision = 0;
  TIM_Time1BaseStructure.TIM_RepetitionCounter = 0;
 
  TIM_TimeBaseInit(TIM1, &TIM_Time1BaseStructure);
 
  /* 频道1,2,3,4的PWM 模式设置 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable ;//TIM_OutputState_Enable; //PWM输出使能位
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable ;//TIM_OutputNState_Enable; //互补PWM输出使能位
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //PWM 1为有效电平
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //PWM互补 0为有效电平
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
 
  TIM_OCInitStructure.TIM_Pulse = 0; //赋占空比值
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);//使能频道1配置
 
  TIM_OCInitStructure.TIM_Pulse = 0; //赋占空比值
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);//使能频道2配置
 
  TIM_OCInitStructure.TIM_Pulse = 0; //赋占空比值
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);//使能频道3配置
//------使能比较四通道------------------------------------
   
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable ;//TIM_OutputState_Enable; //
  TIM_OCInitStructure.TIM_Pulse = V_CCP4PW;
  TIM_OC4Init(TIM1, &TIM_OCInitStructure);
     
    TIM_ITConfig(TIM1,TIM_IT_CC4, ENABLE); //TIM1 比较四中断
     
//-------------------------------------------------------- 
  /* TIM1 计算器使能*/
  TIM_Cmd(TIM1, ENABLE);
     
  /* TIM1 主输出使能 */
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
 
/*******************************************************************************
* 函数名称: ToPwmValue
* 功能描述: 赋各通道占空比值
* 输入参数: 占空比值
* 返回参数: 无
********************************************************************************/
void ToPwmValue(uint16 PwmDuty)
    TIM1->CCR1 = PwmDuty ;
    TIM1->CCR2 = PwmDuty ;
    TIM1->CCR3 = PwmDuty ;
//  TIM1->CCR4 = PwmDuty ;
/*******************************************************************************
* 函数名称: BLDC_Bak
* 功能描述: 刹车
* 输入参数: 刹车占空比
* 返回参数: 无
********************************************************************************/
void BLDC_Bak(uint16_t PwmDuty)
        GPIO_DOWNPIN_AFConfig() ;
         
        PWMABC_Dis ; //关PWM输出
        PWMABCN_Enb ; //使能互补PWM输出
       
        TIM1->CCR1 = PwmDuty ; //赋刹车PWM值
        TIM1->CCR2 = PwmDuty ;
        TIM1->CCR3 = PwmDuty ;
/*******************************************************************************
* 函数名称: BLDC_Bak_End
* 功能描述: 刹车结束时 初始化化下管配置为正常IO模式
* 输入参数: 刹车占空比
* 返回参数: 无
********************************************************************************/
void BLDC_Bak_End(void)
    PWMABCN_Dis ; //禁止互补输出
    GPIO_DOWNPIN_AFConfigRst() ; //
 
/*******************************************************************************
* 函数名称: TIM1_CCP4NVIC_Config
* 功能描述: TIM1 CCR4 中断配置
* 输入参数: 无
* 返回参数: 无
********************************************************************************/
void TIM1_CCP4NVIC_Config(void)
   NVIC_InitTypeDef NVIC_InitStructure;
 
  /* TIM1 时钟使能 */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
 
  /*  TIM1 中断嵌套设计*/
  NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn ;//TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
   
/*******************************************************************************
* 函数名称: ToCCP4PwmValue
* 功能描述: 赋比较4中断时间值
* 输入参数: 占空比值
* 返回参数: 无
********************************************************************************/
void ToCCP4PwmValue(uint16 PwmDuty)
    TIM1->CCR4 = PwmDuty ;

以上是关于STM32F030F4 BLDC的主要内容,如果未能解决你的问题,请参考以下文章

stm32f030f4p6 i2c地址是多少?

基于STM32F030F4P9和STM32 CUBEMX 输出PWM波形

STM32F0使用LL库实现MS5536C通讯

STM32F030F4P6这个单片机怎么样?

STM32F030 IO口外部中断应用

STM32F030F4P6 仅在从闪存启动时运行中断处理程序。从引导加载程序启动时重置