资源描述:
《PWM脉冲程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、一个定时器四个通道,分别产生不同频率(这个例子网上也有)vu16CCR1_Val=32768;vu16CCR2_Val=16384;vu16CCR3_Val=8192;vu16CCR4_Val=4096;voidTIM_Configuration(void){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_OCInitTypeDefTIM_OCInitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE
2、);TIM_TimeBaseStructure.TIM_Period=65535; TIM_TimeBaseStructure.TIM_Prescaler=2; TIM_TimeBaseStructure.TIM_ClockDivision=0; TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);TIM_OCInitStructure.TIM_
3、OCMode=TIM_OCMode_Toggle; //PWM模式2TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//正向通道有效TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;//反向通道无效TIM_OCInitStructure.TIM_Pulse=CCR1_Val; //占空时间TIM_OCInitStructure.TIM_OCPolari
4、ty=TIM_OCPolarity_Low; //输出极性TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High; //互补端的极性TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset;TIM_OC1Init(TIM2,&TIM_OCInitStructure);
5、 //通道1TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Disable);TIM_OCInitStructure.TIM_Pulse=CCR2_Val; //占空时间TIM_OC2Init(TIM2,&TIM_OCInitStructure); //通道2TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Disable);TIM_OCInitStructure.TIM_Pulse=CCR3_Val; //占空
6、时间TIM_OC3Init(TIM2,&TIM_OCInitStructure); //通道3TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Disable);TIM_OCInitStructure.TIM_Pulse=CCR4_Val; //占空时间TIM_OC4Init(TIM2,&TIM_OCInitStructure); //通道4TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Disable);TIM_
7、Cmd(TIM2,ENABLE);//TIM_CtrlPWMOutputs(TIM2,ENABLE); TIM_ITConfig(TIM2,TIM_IT_CC1
8、TIM_IT_CC2
9、TIM_IT_CC3
10、TIM_IT_CC4,ENABLE);}voidGPIO_Configuration(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);RCC_APB2PeriphClockCmd
11、(RCC_APB2Periph_GPIOB,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2
12、