本帖最后由 qq4056 于 2019-6-27 19:18 编辑
- void TIM2_PWM_Init() //产生pwm
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- float TimerPeriod = 0;
- uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
-
- /* GPIOA Clocks enable */
- RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
-
- /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- 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(GPIOA, &GPIO_InitStructure);
-
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_2);
-
- /* TIM1 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
-
- /* Time Base configuration */
- TIM_TimeBaseStructure.TIM_Prescaler = 48-1;//预分频值
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = 500000; //自动重装载值
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
-
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
-
- /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = 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_OCInitStructure.TIM_Pulse = 100000; //占空比时间
- TIM_OC3Init(TIM2, &TIM_OCInitStructure);
-
- /* TIM1 counter enable */
- TIM_Cmd(TIM2, ENABLE);
-
- /* TIM1 Main Output Enable */
- TIM_CtrlPWMOutputs(TIM2, ENABLE);
- }
复制代码我用PA2,定时器TIM2,通道TIM2_CH3,可是也是产生不了pwm.是在是不知道哪出问题
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>