看你的代码好费劲,发一个我的吧,默认50KHZ,可以随意设置频率和占空比,楼主可以参考一下:这个代码已经验证: extern unsigned char Pwm_freg ; unsigned char Pwm_duty_num =20 ; // 50kHZ extern volatile unsigned short int Pwm_period_num ; unsigned short int CCR1_Val; unsigned short int CCR2_Val; //unsigned short int CCR3_Val = 50; //unsigned short int CCR4_Val = 20; /******************************************************************************* * 函数名称: Timer3_PWM_Init(); * 功能描述: 定时器3初始化--用于产生四路PWM * 输入参数: void * 返回参数: 无 ********************************************************************************/ void Timer3_PWM_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; CCR1_Val = (unsigned short int)(50*(Pwm_period_num+1)/100); CCR2_Val = (unsigned short int)(50*(Pwm_period_num+1)/100); //时钟配置 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE , ENABLE); // 使能GPIOB端口 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // 打开定时器3的时钟 //TIM3-CH1-CH4GPIO通道配置--PE2-PE5 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_PinAFConfig(F303C_PWM3_CH1_P0RT , F303C_PWM3_CH1_SOURCE, F303C_PWM3_CH1_AF); GPIO_PinAFConfig(F303C_PWM3_CH2_P0RT , F303C_PWM3_CH2_SOURCE, F303C_PWM3_CH2_AF); GPIO_PinAFConfig(F303C_PWM3_CH3_P0RT , F303C_PWM3_CH3_SOURCE, F303C_PWM3_CH3_AF); GPIO_PinAFConfig(F303C_PWM3_CH4_P0RT , F303C_PWM3_CH4_SOURCE , F303C_PWM3_CH4_AF); //设置定时器相关参数 TIM_TimeBaseInitStructure.TIM_Prescaler = 71; // 定时器时钟分频后: 72000000 / (72 + 1) = 1MHZ. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式 TIM_TimeBaseInitStructure.TIM_Period = Pwm_period_num ; // 自动重装载计数器周期的值--默认50KHZ TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分频系数--不分频 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); // TIM2, 每1ms中断一次. //PWM模式设置-CH3 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; TIM_OC3Init(TIM3, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //PWM模式设置-CH4 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; TIM_OC4Init(TIM3, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); TIM_Cmd(TIM3, ENABLE); }
所在话题
参与活动
-
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16