如题,谢谢大家指导~~
[mw_shl_code=c,true]void TIM1_PWM_Init(u32 arr,u32 psc)
{
//????·??è????????IO???è??
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM1_BDTRInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM1?±??????
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //????PORTB?±??
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //????PORTA?±??
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13|GPIO_PinSource14|GPIO_PinSource15,GPIO_AF_TIM1); //GPIOB15~13???????¨?±?÷1:CH1N,CH2N,CH3N
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8|GPIO_PinSource9|GPIO_PinSource10|GPIO_PinSource11,GPIO_AF_TIM1);//GPIOA8~11???????¨?±?÷1:CH1,CH2,CH3,CH4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15|GPIO_Pin_13|GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //????100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???ì????????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //????
GPIO_Init(GPIOB,&GPIO_InitStructure); //??????PB13~15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //????100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???ì????????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //????
GPIO_Init(GPIOA,&GPIO_InitStructure); //??????PA8~11
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler=psc; //?¨?±?÷·???
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //?ò??????????
TIM_TimeBaseStructure.TIM_Period=arr; //×?????×°????
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);//???????¨?±?÷1
TIM_ARRPreloadConfig(TIM1,ENABLE);//ARPE????
//??????TIM1 Channel~3,Channel1N~3N PWM????
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????¨?±?÷????:TIM?????í???÷??????2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±???????????
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//±???????????
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //????????:TIM????±?????????
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //????????:TIM????±?????????
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_Pulse = 200;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //?ù??T???¨???????????????èTIM1OC1
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //?ù??T???¨???????????????èTIM1OC2
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //?ù??T???¨???????????????èTIM1OC3
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //?ù??T???¨???????????????èTIM1OC4
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //????TIM1??CCR1?????¤×°???????÷
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //????TIM1??CCR1?????¤×°???????÷
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //????TIM1??CCR1?????¤×°???????÷
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //????TIM1??CCR1?????¤×°???????÷
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM1_BDTRInitStructure.TIM_DeadTime = 7;
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure);
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_CCPreloadControl(TIM1, ENABLE);
TIM_SelectCOM(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE); //????TIM1
TIM_CtrlPWMOutputs(TIM1, ENABLE);
} [/mw_shl_code]
|