本帖最后由 ht_chb 于 2016-7-14 08:33 编辑
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
uint16_t Channel1Pulse = 0, Channel2Pulse = 0;
Channel1Pulse = TIM20OC1_MIN_DC;
Channel2Pulse = TIM20OC2_MIN_DC;
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = TIM20PRESC;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TIM20ARR;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM20, &TIM_TimeBaseStructure);
//Æô¶¯ARRÓ°×ӼĴæÆ÷,Ö±µ½¸üÐÂʼþ²ÅÉèÖÃ
TIM_ARRPreloadConfig(TIM20, ENABLE);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState =TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
//ͨµÀ1
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
TIM_OC1Init(TIM20, &TIM_OCInitStructure);
//Æô¶¯OC1Ó°×ӼĴæÆ÷,Ö±µ½¸üÐÂʼþ²ÅÉèÖÃ
TIM_OC1PreloadConfig(TIM20, TIM_OCPreload_Enable);
//ͨµÀ2
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
TIM_OC2Init(TIM20, &TIM_OCInitStructure);
//Æô¶¯OC2Ó°×ӼĴæÆ÷,Ö±µ½¸üÐÂʼþ²ÅÉèÖÃ
TIM_OC2PreloadConfig(TIM20, TIM_OCPreload_Enable);
//ÉèÖÃËÀÇøʱ¼ä
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_3;
TIM_BDTRInitStructure.TIM_DeadTime = ALL_DeadTime;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM20, &TIM_BDTRInitStructure);
/* TIM20 counter enable */
TIM_Cmd(TIM20, ENABLE);
/* TIM20 Main Output Enable */
TIM_CtrlPWMOutputs(TIM20, ENABLE); |