F035 输出6路互补pwm。
通常我们在调试电机的时候,有时候会控制不好参数,导致硬件过流。这时候我们就要先确定IPM模块有没有被损坏,最简单的办法就是MCU给IPM输入6路pwm,看IPM是否可以正常输出经过放大后的pwm波形。
TMR_TimeBase_TTIM_TimeBaseInitStructure;
TMR_OCConfig_TTIM_OCInitStructure;
TMR_BDTInit_TTIM_BDTRInitStructure;
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG | RCM_APB2_PERIPH_TMR1 );
/* Time Base configuration ,init time1 freq*/
TIM_TimeBaseInitStructure.period = u16_Period; //(SYSTEM_FREQUENCY/2/8000) - 1;
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;
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;
TMR_OC2Config(TMR1, &TIM_OCInitStructure);
//set OC3/OC3N
TIM_OCInitStructure.Pulse = u16_Period;
TMR_OC3Config(TMR1, &TIM_OCInitStructure);
TIM_OCInitStructure.OC_Mode = TMR_OC_MODE_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.OC_OutputState = TMR_OUTPUT_STATE_ENABLE; //CH1比较输出使能
TIM_OCInitStructure.OC_OutputNState = TMR_OUTPUT_NSTATE_ENABLE; //CH1N比较输出使能
TIM_OCInitStructure.OC_Polarity = TMR_OC_POLARITY_HIGH; //CH1输出极性:TIM输出比较极性高
TIM_OCInitStructure.OC_NPolarity = TMR_OC_NPOLARITY_HIGH; //CH1N输出极性:TIM输出比较极性高
TIM_OCInitStructure.OC_Idlestate = TMR_OCIDLESTATE_RESET; //CH1输出空闲状态:0
TIM_OCInitStructure.OC_NIdlestate = TMR_OCNIDLESTATE_SET; //CH1N输出空闲状态:0
//set OC4
TIM_OCInitStructure.Pulse = 500;
TMR_OC4Config(TMR1, &TIM_OCInitStructure);//OC4
TIM_OCInitStructure.OC_Mode = TMR_OC_MODE_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.OC_OutputState = TMR_OUTPUT_STATE_ENABLE; //CH1比较输出使能
TIM_OCInitStructure.OC_OutputNState = TMR_OUTPUT_NSTATE_ENABLE; //CH1N比较输出使能
TIM_OCInitStructure.OC_Polarity = TMR_OC_POLARITY_HIGH; //CH1输出极性:TIM输出比较极性高
TIM_OCInitStructure.OC_NPolarity = TMR_OC_NPOLARITY_HIGH; //CH1N输出极性:TIM输出比较极性高
TIM_OCInitStructure.OC_Idlestate = TMR_OCIDLESTATE_RESET; //CH1输出空闲状态:0
TIM_OCInitStructure.OC_NIdlestate = TMR_OCNIDLESTATE_SET;
TIM_OCInitStructure.Pulse = 1500;
TMR_OC5Config(TMR1, &TIM_OCInitStructure);//OC5
//enable interrupt
TMR_EnableInterrupt(TMR1, TMR_INT_UPDATE | 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);
TMR1->CTRL2_B.MMSEL = 1; //使能主模式定时器的计数器使能型号用于TRGO
TMR1->CCM3_B.OC5PEN = 1; //使能输出比较通道5预装载
TMR1->CTRL3_B.OC1AEN =1; //使能通道1非对称PWM输出模式
TMR1->CTRL3_B.OC2AEN =1; //使能通道2非对称PWM输出模式
TMR1->CTRL3_B.OC3AEN =1; //使能通道3非对称PWM输出模式
//TRGO 0
TMR1->CTRL3_B.MMSZE =1; //计数器归零时不产生TRGO
TMR1->CTRL3_B.MMSPE =0; //计数器与自动重装载寄存器匹配时产生TRGO
TMR1->CTRL3_B.MMSEL2 =8; //根据上面两个寄存器的控制方式产生TRGO
//TRGO2
TMR1->CTRL4_B.MM2SZE = 0; //计数器归零时不产生TRGO
TMR1->CTRL4_B.MM2SPE = 0; //计数器与自动重装载寄存器匹配时不产生TRGO
TMR1->CTRL4_B.MM2SEL = 7; //比较模式4,OC4REF用于触发TRGO2
//TRGO3
TMR1->CTRL4_B.MM3SZE = 0; //计数器归零时不产生TRGO
TMR1->CTRL4_B.MM3SPE = 0; //计数器与自动重装载寄存器匹配时不产生TRGO
TMR1->CTRL4_B.MM3SEL= 9; //比较模式5,OC5REF用于触发TRGO3
TMR_EnableMasterSlaveMode(TMR1); //使能主从模式
TMR_EnableAUTOReload(TMR1); //使能自动重装载寄存器,
TMR_Enable(TMR1);
TMR1->REPCNT = 1;
/* Main Output Enable */
TMR_EnablePWMOutputs(TMR1);
/* GPIOconfig */
GPIO_Config_TGPIO_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_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);
|