#ifndef _TIME1_H_
#define _TIME1_H_
#include stm32f0xx.h
#define DEF_PWMFRE DEF_PWMFRE_16K
#define DEF_PWMFRE_8K
8000
#define DEF_PWMFRE_16K
16000
#define DEF_PWMFRE_20K
20000
#define DEF_PWMFRE_25K
25000
#define DEF_PWMFRE_30K
30000
#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)))
#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)))
#define TIM1_CMP4IEN_ENB TIM1->DIER |= TIM_IT_CC4
#define TIM1_CMP4IEN_DIS TIM1->DIER &= ~TIM_IT_CC4
#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
#include global.h
#include time1.h
TIM_TimeBaseInitTypeDef TIM_Time1BaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t TimerPeriod =
0
;
/*******************************************************************************
* 函数名称: TIM1_GPIO_Init
* 功能描述: TIM1--GPIO引脚配置---在此可以设置6路PWM输出
* 输入参数: void
* 返回参数: 无
********************************************************************************/
void
TIM1_GPIO_Init(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
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_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
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);
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
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_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_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
)
{
TimerPeriod = (SystemCoreClock / DEF_PWMFRE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
TIM_Time1BaseStructure.TIM_Prescaler =
0
;
TIM_Time1BaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_Time1BaseStructure.TIM_Period = TimerPeriod;
TIM_Time1BaseStructure.TIM_ClockDivision =
0
;
TIM_Time1BaseStructure.TIM_RepetitionCounter =
0
;
TIM_TimeBaseInit(TIM1, &TIM_Time1BaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable ;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse =
0
;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =
0
;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =
0
;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable ;
TIM_OCInitStructure.TIM_Pulse = V_CCP4PW;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_ITConfig(TIM1,TIM_IT_CC4, ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/*******************************************************************************
* 函数名称: ToPwmValue
* 功能描述: 赋各通道占空比值
* 输入参数: 占空比值
* 返回参数: 无
********************************************************************************/
void
ToPwmValue(uint16 PwmDuty)
{
TIM1->CCR1 = PwmDuty ;
TIM1->CCR2 = PwmDuty ;
TIM1->CCR3 = PwmDuty ;
}
/*******************************************************************************
* 函数名称: BLDC_Bak
* 功能描述: 刹车
* 输入参数: 刹车占空比
* 返回参数: 无
********************************************************************************/
void
BLDC_Bak(uint16_t PwmDuty)
{
GPIO_DOWNPIN_AFConfig() ;
PWMABC_Dis ;
PWMABCN_Enb ;
TIM1->CCR1 = PwmDuty ;
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;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPriority =
0
;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* 函数名称: ToCCP4PwmValue
* 功能描述: 赋比较4中断时间值
* 输入参数: 占空比值
* 返回参数: 无
********************************************************************************/
void
ToCCP4PwmValue(uint16 PwmDuty)
{
TIM1->CCR4 = PwmDuty ;