void Drv_Pwm_Init(uint16_t u16_Period,uint16_t u16_DeadTime)
{
TMR_TimeBase_T TIM_TimeBaseInitStructure;
TMR_OCConfig_T TIM_OCInitStructure;
TMR_BDTInit_T TIM_BDTRInitStructure;
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG | RCM_APB2_PERIPH_TMR1 );
/* Time Base configuration ,init time1 freq*/
TIM_TimeBaseInitStructure.period = u16_Period;
TIM_TimeBaseInitStructure.div = 0;
TIM_TimeBaseInitStructure.counterMode = TMR_COUNTER_MODE_CENTERALIGNED2;
TIM_TimeBaseInitStructure.clockDivision = TMR_CKD_DIV1;
TIM_TimeBaseInitStructure.repetitionCounter = 1;
TMR_ConfigTimeBase(TMR1, &TIM_TimeBaseInitStructure);
/* Automatic Output enable, Break, dead time and lock configuration*/
TIM_BDTRInitStructure.RMOS_State = TMR_RMOS_STATE_ENABLE;//--------
TIM_BDTRInitStructure.IMOS_State = TMR_IMOS_STATE_ENABLE;//--------
TIM_BDTRInitStructure.lockLevel = TMR_LOCK_LEVEL_OFF;//00:锁定关闭,寄存器无写保护;01:锁定级别1,不能写入TIMx_BDTR寄存器的DTG、BKE、BKP、AOE位和TIMx_CR2寄存器的OISx/OISxN位;
TIM_BDTRInitStructure.deadTime = u16_DeadTime;//死区时间
/**
* Brake configuration: enable brake
* Brake input polarity: active in low level
* Auto output enable configuration: Disable MOE bit hardware control
*/
TIM_BDTRInitStructure.breakState = TMR_BREAK_STATE_DISABLE;//TMR_BREAK_STATE_ENABLE TMR_BREAK_STATE_DISABLE;
TIM_BDTRInitStructure.breakPolarity = TMR_BREAK_POLARITY_LOW;
TIM_BDTRInitStructure.automaticOutput = TMR_AUTOMATIC_OUTPUT_DISABLE;
TMR_ConfigBDT(TMR1, &TIM_BDTRInitStructure);
/*pwm driver set,channel 1,2,3,4set pwm mode*/
TIM_OCInitStructure.OC_Mode = TMR_OC_MODE_PWM2;
TIM_OCInitStructure.OC_OutputState = TMR_OUTPUT_STATE_ENABLE; //TMR_OUTPUT_STATE_DISABLE;
TIM_OCInitStructure.OC_OutputNState = TMR_OUTPUT_NSTATE_ENABLE; //TMR_OUTPUT_NSTATE_DISABLE;//------------
TIM_OCInitStructure.Pulse = u16_Period/2;
TIM_OCInitStructure.OC_Polarity = TMR_OC_POLARITY_HIGH;
TIM_OCInitStructure.OC_NPolarity = TMR_OC_NPOLARITY_HIGH; //互补输出极性-------
TIM_OCInitStructure.OC_Idlestate = TMR_OCIDLESTATE_RESET; // TMR_OCIDLESTATE_SET; //
TIM_OCInitStructure.OC_NIdlestate = TMR_OCNIDLESTATE_RESET; // TMR_OCNIDLESTATE_SET;//
//set OC1/OC1N
TMR_OC1Config(TMR1, &TIM_OCInitStructure);
//set oc2/oc2N
TIM_OCInitStructure.Pulse = u16_Period/2;
TMR_OC2Config(TMR1, &TIM_OCInitStructure);
//set OC3/OC3N
TIM_OCInitStructure.Pulse = u16_Period/2;
TMR_OC3Config(TMR1, &TIM_OCInitStructure);
TIM_OCInitStructure.OC_Mode = TMR_OC_MODE_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.OC_OutputState = TMR_OUTPUT_STATE_ENABLE; //CH4比较输出使能
TIM_OCInitStructure.OC_Polarity = TMR_OC_POLARITY_HIGH; //CH4输出极性:TIM输出比较极性高
TIM_OCInitStructure.OC_Idlestate = TMR_OCIDLESTATE_RESET; //CH4输出空闲状态:0
TIM_OCInitStructure.Pulse =u16_Period/2+u16_DeadTime;
TMR_OC5Config(TMR1, &TIM_OCInitStructure);//OC5
//enable interrupt
TMR_EnableInterrupt(TMR1, TMR_INT_BRK);// TMR_INT_BRK
TMR_EnableAUTOReload(TMR1);//TIMx_ARR寄存器被装入缓冲器
TMR_OC1PreloadConfig(TMR1,TMR_OC_PRELOAD_ENABLE);
TMR_OC2PreloadConfig(TMR1,TMR_OC_PRELOAD_ENABLE);//比较值预装载使能
TMR_OC3PreloadConfig(TMR1,TMR_OC_PRELOAD_ENABLE);
TMR_OC4PreloadConfig(TMR1,TMR_OC_PRELOAD_ENABLE);
TMR_OC5PreloadConfig(TMR1,TMR_OC_PRELOAD_ENABLE);
TMR_EnableAUTOReload(TMR1);
TMR1->CTRL2_B.MMSEL = 2;
TMR1->CTRL3_B.OC1AEN = 1; //非对称模式
TMR1->CTRL3_B.OC2AEN = 1;
TMR1->CTRL3_B.OC3AEN = 1;
TMR1->CC1C = u16_Period/2 +u16_DeadTime;
TMR1->CC2C = u16_Period/2 +u16_DeadTime;
TMR1->CC3C =u16_Period/2 +u16_DeadTime;
TMR1->REPCNT = 1;
TMR_Enable(TMR1);
/* Main Output Enable */
TMR_EnablePWMOutputs(TMR1);
/* GPIO config */
GPIO_Config_T GPIO_InitStructure;
GPIO_InitStructure.pin = GPIO_PIN_PWM_UH_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_UH_Mobile, &GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_PWM_VH_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_VH_Mobile, &GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_PWM_WH_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_WH_Mobile, &GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_PWM_UL_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_UL_Mobile, &GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_PWM_VL_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_VL_Mobile, &GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_PWM_WL_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_WL_Mobile, &GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_PWM_Break_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_Break_Mobile, &GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_PWM_XL_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_XL_Mobile, &GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_PWM_XH_Mobile;
GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_InitStructure.pupd = GPIO_PUPD_PD;
GPIO_Config(GPIO_PWM_XH_Mobile, &GPIO_InitStructure);
//复用映射,需参考文档
GPIO_ConfigPinAF(GPIO_PWM_UH_Mobile,GPIO_PIN_SOURCE_PWM_UH_Mobile,GPIO_AF_PIN2);
GPIO_ConfigPinAF(GPIO_PWM_VH_Mobile,GPIO_PIN_SOURCE_PWM_VH_Mobile,GPIO_AF_PIN2);
GPIO_ConfigPinAF(GPIO_PWM_WH_Mobile,GPIO_PIN_SOURCE_PWM_WH_Mobile,GPIO_AF_PIN2);
GPIO_ConfigPinAF(GPIO_PWM_UL_Mobile,GPIO_PIN_SOURCE_PWM_UL_Mobile,GPIO_AF_PIN2);
GPIO_ConfigPinAF(GPIO_PWM_VL_Mobile,GPIO_PIN_SOURCE_PWM_VL_Mobile,GPIO_AF_PIN2);
GPIO_ConfigPinAF(GPIO_PWM_WL_Mobile,GPIO_PIN_SOURCE_PWM_WL_Mobile,GPIO_AF_PIN2);
GPIO_ConfigPinAF(GPIO_PWM_XL_Mobile,GPIO_PIN_SOURCE_PWM_XL_Mobile,GPIO_AF_PIN5);
GPIO_ConfigPinAF(GPIO_PWM_XH_Mobile,GPIO_PIN_SOURCE_PWM_XH_Mobile,GPIO_AF_PIN5);
GPIO_ConfigPinAF(GPIO_PWM_Break_Mobile,GPIO_PIN_SOURCE_PWM_Break_Mobile,GPIO_AF_PIN3);
}