需要使用TIM1定时器输出4路独立PWM,初始设置OC1占空比27%,程序如下所示。上电后示波器测的占空比为73%,需硬件复位后才可变为27%。
求大神解答!!!!
void PWM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;//GPIO_Mode_Out_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void PWM_Timer_Config(void)
{
/* ¶¨òå¸÷3õê¼»ˉ½á11ìå TIM_ICInitStructure */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
/*TIM1_CH4êä3ö
WMDÅoÅμÄÏà1ØÅäÖÃ*/
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = 3000; //200HZ,ÖüÆú5mS
TIM_TimeBaseStructure.TIM_Prescaler = 0; //Ô¤·ÖÆμ0
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ê±Öó·Ö¸îÎa0
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM1 , &TIM_TimeBaseStructure); //3õê¼»ˉê±»ùμ¥Ôa
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //1¤×÷ÔúPWM1Ä£ê½
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //êä3öê1Äü
TIM_OCInitStructure.TIM_Pulse = 800; //±è½ÏÖμÎa15000
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔÎa¸ß
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //3õê¼»ˉTIM3_CH1(PA6)
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //1¤×÷ÔúPWM1Ä£ê½
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //êä3öê1Äü
TIM_OCInitStructure.TIM_Pulse = 800; //±è½ÏÖμÎa15000
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔÎa¸ß
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //3õê¼»ˉTIM3_CH1(PA6)
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //1¤×÷ÔúPWM1Ä£ê½
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //êä3öê1Äü
TIM_OCInitStructure.TIM_Pulse = 3100; //±è½ÏÖμÎa15000
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔÎa¸ß
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //3õê¼»ˉTIM3_CH1(PA6)
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //1¤×÷ÔúPWM1Ä£ê½
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //êä3öê1Äü
TIM_OCInitStructure.TIM_Pulse = 0; //±è½ÏÖμÎa15000
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔÎa¸ß
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //3õê¼»ˉTIM3_CH1(PA6)
TIM_OC1PreloadConfig(TIM1 , TIM_OCPreload_Enable); //ê1ÄüԤװÔØ
TIM_OC2PreloadConfig(TIM1 , TIM_OCPreload_Enable); //ê1ÄüԤװÔØ
TIM_OC3PreloadConfig(TIM1 , TIM_OCPreload_Enable); //ê1ÄüԤװÔØ
TIM_OC4PreloadConfig(TIM1 , TIM_OCPreload_Enable); //ê1ÄüԤװÔØ
TIM_ARRPreloadConfig(TIM1, ENABLE); //ÆôóÃARR
TIM1->CR2&=0X0000; //½a¾öż¶ûéÏμçoóOC1£¬0C2ÎTêä3ö×′쬣¡£¡£¡
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIM1
TIM_CtrlPWMOutputs(TIM1,ENABLE);
// GPIO_WriteBit(GPIOB,GPIO_Pin_8,0); //Din=0
}
一周热门 更多>