huo_hu 发表于 2015-4-10 22:11
这些结构体只是在初始化时传一下参数,然后就没用了,gpio如果一样的设置参数可以直接用"或"在一起,一次完成,timebase是对定时器时钟部分的设置只有一个,OC和BTR的设置是对通道的,两个通道一定要两次.
有点乱,重新发一下,请帮我看看这样是否正确呢?谢谢
//PWM1 for inverter (PA8---TIM1_CH1(pin29),PB13---TIM1_CH1N(pin26))
//PWM2 for inverter (PA9---TIM1_CH2(pin30),PB14---TIM1_CH2N(pin27))
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure,TIM_OCInitStructure1;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure,TIM_BDTRInitStructure1;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; //PA.8 and PA.9 for TM1_CH1 and TM1_CH2 pin config
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14; //PB.13 and PB.14 for TIM1_CH1N and TIM1_CH2N pin config
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//TIM base config
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period=1200-1; //72M/1200 = 60k
TIM_TimeBaseStructure.TIM_Prescaler = 0; // 72M/(0+1) = 72M
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
//PWM1输出配置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// TIM_OCInitStructure.TIM_Pulse = 3000-1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_BDTRInitStructure.TIM_DeadTime = 0x24;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
//PWM2输出配置
TIM_OCInitStructure1.TIM_OCMode = TIM_OCMode_PWM1;
// TIM_OCInitStructure1.TIM_Pulse = 3000-1;
TIM_OCInitStructure1.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure1.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure1.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure1.TIM_OutputNState=TIM_OutputNState_Enable;
TIM_OCInitStructure1.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure1.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure1);
TIM_BDTRInitStructure1.TIM_DeadTime = 0x24;
TIM_BDTRInitStructure1.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure1);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);