求救, STM32 通用定时器产生多通道PWM输出,会相互影响,怎么办????
- #define _CH1 1
- #define _CH2 2
- #define _CH3 3
- #define _CH4 4
- #define _CH5 5
- #define _CH6 6
- #define _CH7 7
- #define _CH8 8
- #define CH1_CH4_PWM_GPIO_CLK RCC_APB2Periph_GPIOC
- #define CH1_CH4_PWM_CLK RCC_APB1Periph_tiM3
- #define CH1_CH4_PWM TIM3
- #define CH1_PWM_GPIO_PORT GPIOC
- #define CH1_PWM_PIN GPIO_Pin_9
- #define CH2_PWM_GPIO_PORT GPIOC
- #define CH2_PWM_PIN GPIO_Pin_8
- #define CH3_PWM_GPIO_PORT GPIOC
- #define CH3_PWM_PIN GPIO_Pin_7
- #define CH4_PWM_GPIO_PORT GPIOC
- #define CH4_PWM_PIN GPIO_Pin_6
- #define CH5_CH8_PWM_GPIO_CLK RCC_APB2Periph_GPIOB
- #define CH5_CH8_PWM_CLK RCC_APB1Periph_TIM4
- #define CH5_CH8_PWM TIM4
- #define CH5_PWM_GPIO_PORT GPIOB
- #define CH5_PWM_PIN GPIO_Pin_9
- #define CH6_PWM_GPIO_PORT GPIOB
- #define CH6_PWM_PIN GPIO_Pin_8
- #define CH7_PWM_GPIO_PORT GPIOB
- #define CH7_PWM_PIN GPIO_Pin_7
- #define CH8_PWM_GPIO_PORT GPIOB
- #define CH8_PWM_PIN GPIO_Pin_6
- #define PWM_Freq (500 - 1)
- /*******************************************************************************
- ** 名称: vLight_PWMModeInit
- ** 描述: PWM输出模式配置;
- ** 设置PWM工作频率,计数模式,占空比
- **
- ** 输入: 无
- ** 输出: 无
- ********************************************************************************/
- void vLight_PWMModeInit(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- //TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
-
- //定时器工作在单脉冲下
- TIM_DeInit(CH1_CH4_PWM);
-
- TIM_TimeBaseStructure.TIM_Period = PWM_Freq; //计数值,即定时周期
- TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频,不分频为72MHz
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频系数
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInit(CH1_CH4_PWM,&TIM_TimeBaseStructure);
-
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- /*配置通道1*/
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC4Init(CH1_CH4_PWM,&TIM_OCInitStructure);
- TIM_OC4PreloadConfig(CH1_CH4_PWM,TIM_OCPreload_Enable);
- /*配置通道2*/
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC3Init(CH1_CH4_PWM,&TIM_OCInitStructure);
- TIM_OC3PreloadConfig(CH1_CH4_PWM,TIM_OCPreload_Enable);
- /*配置通道3*/
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC2Init(CH1_CH4_PWM,&TIM_OCInitStructure);
- TIM_OC2PreloadConfig(CH1_CH4_PWM,TIM_OCPreload_Enable);
- /*配置通道4*/
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC1Init(CH1_CH4_PWM,&TIM_OCInitStructure);
- TIM_OC1PreloadConfig(CH1_CH4_PWM,TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(CH1_CH4_PWM,ENABLE);
- TIM_Cmd(CH1_CH4_PWM,ENABLE);//先不使能能TIM
-
- //定时器工作在单脉冲下
- TIM_DeInit(CH5_CH8_PWM);
-
- TIM_TimeBaseStructure.TIM_Period = PWM_Freq; //计数值,即定时周期
- TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频,不分频为72MHz
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频系数
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInit(CH5_CH8_PWM,&TIM_TimeBaseStructure);
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
-
- /*配置通道5*/
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC4Init(CH5_CH8_PWM,&TIM_OCInitStructure);
- TIM_OC4PreloadConfig(CH5_CH8_PWM,TIM_OCPreload_Enable);
- /*配置通道6*/
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC3Init(CH5_CH8_PWM,&TIM_OCInitStructure);
- TIM_OC3PreloadConfig(CH5_CH8_PWM,TIM_OCPreload_Enable);
- /*配置通道7*/
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC2Init(CH5_CH8_PWM,&TIM_OCInitStructure);
- TIM_OC2PreloadConfig(CH5_CH8_PWM,TIM_OCPreload_Enable);
- /*配置通道8*/
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC1Init(CH5_CH8_PWM,&TIM_OCInitStructure);
- TIM_OC2PreloadConfig(CH5_CH8_PWM,TIM_OCPreload_Enable);
-
- TIM_ARRPreloadConfig(CH5_CH8_PWM,ENABLE);
- TIM_Cmd(CH5_CH8_PWM,ENABLE);//先不使能能TIM
- }
- /*******************************************************************************
- ** 名称: vLight_PWMModeInit
- ** 描述: PWM输出模式配置;
- ** 设置PWM工作频率,计数模式,占空比
- **
- ** 输入: 无
- ** 输出: 无
- ********************************************************************************/
- void vLight_SetPWM(uint8_t uCH,uint16_t uCycle)
- {
- switch(uCH)
- {
- case _CH1:
- {
- TIM_SetCompare4(CH1_CH4_PWM,uCycle);
- break;
- }
- case _CH2:
- {
- TIM_SetCompare3(CH1_CH4_PWM,uCycle);
- break;
- }
- case _CH3:
- {
- TIM_SetCompare2(CH1_CH4_PWM,uCycle);
- break;
- }
- case _CH4:
- {
- TIM_SetCompare1(CH1_CH4_PWM,uCycle);
- break;
- }
- case _CH5:
- {
- TIM_SetCompare4(CH5_CH8_PWM,uCycle);
- break;
- }
- case _CH6:
- {
- TIM_SetCompare3(CH5_CH8_PWM,uCycle);
- break;
- }
- case _CH7:
- {
- TIM_SetCompare2(CH5_CH8_PWM,uCycle);
- break;
- }
- case _CH8:
- {
- TIM_SetCompare1(CH5_CH8_PWM,uCycle);
- break;
- }
- default: break;
- }
- }
复制代码
0
|
|
|
|