RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//选择GPIOA组的第123根引脚
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//选择引脚为复用推挽功能
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//选择引脚速度
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOA
TIM_TimeBaseStructure.TIM_Period=999; //计数值
TIM_TimeBaseStructure.TIM_Prescaler=71; //分频值
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); //初始化定时器
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//打开或者关闭脉冲输出
TIM_OCInitStructure.TIM_Pulse = 0; //比较值
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //有效电平设置为高状态
TIM_OC2Init(TIM2,&TIM_OCInitStructure);//初始化定时器通道2
TIM_OC3Init(TIM2,&TIM_OCInitStructure);//3
TIM_OC4Init(TIM2,&TIM_OCInitStructure);//4
TIM_Cmd(TIM2,ENABLE);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2,ENABLE);
|