void TIM1_PWM_Init(u32 arr,u32 psc)
{
u32 TimerPeriod,ccr1,ccr2;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BdtrStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource10,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1);
TimerPeriod = (SystemCoreClock / 10000 ) - 1;
ccr1 = TimerPeriod / 2; //1/2 = 50%
ccr2 = TimerPeriod / 2; //1/2 = 50%
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Prescaler = 199; //Timer clock = sysclock /(TIM_Prescaler+1) = 168M
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInitStructure.TIM_Period = TimerPeriod - 1;//Period = (TIM counter clock / TIM output clock) - 1 = 20K
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = ccr1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = ccr2;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = ccr3;
// TIM_OC3Init(TIM1,&TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = ccr4;
// TIM_OC4Init(TIM1,&TIM_OCInitStructure);
TIM_BdtrStructure.TIM_OSSRState = TIM_OSSRState_Enable; //ê1?üTIM1 OSSR×′ì?
TIM_BdtrStructure.TIM_OSSIState = TIM_OSSIState_Enable; //ê1?üTIM1 OSSI×′ì?
TIM_BdtrStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //2???è?o???
TIM_BdtrStructure.TIM_DeadTime = 20; //ê?3?′ò?aoí1?±?×′ì?????μ??óê±£??à??ê±??
TIM_BdtrStructure.TIM_Break = TIM_Break_Disable; //ê§?üTIM1é23μê?è?
TIM_BdtrStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //TIM1é23μê?è?1ü????D???
TIM_BdtrStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //×??ˉê?3?1|?üê1?ü
TIM_BDTRConfig(TIM1, &TIM_BdtrStructure);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
不是,我也是问问题的。我的定时器我没法控制。
不写那句TIM_Cmd(TIM1,ENABLE);
它自己跑的飞快。停都没法停
一周热门 更多>