void Motor_Init(u16 TIM2per, u16 TIM4per, u16 TIM4Compare1)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
//TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//ê1Äü¶¨ê±Æ÷2μÄê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//ê1Äü¶¨ê±Æ÷3μÄê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//ê1Äü¸′óÃIOê±Öó
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//¸′óÃíÆíìêä3ö
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_6;//PA0
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//¸′óÃíÆíìêä3ö
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;//PA0
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
//TIM21¤×÷Ôúμ¥Âö3åÏÂ
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//ûóDê±¼ä·Ö¸ô
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInitStruct.TIM_Prescaler = 7200-1;//ԤװÆμêy£¬Ã¿100US¼Æêyò»′Î
TIM_TimeBaseInitStruct.TIM_Period = TIM2per;//ÖØ×°Öμ
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single);//éèÖÃTIM2Ôúμ¥Âö3åģ꽣¬ÇòêÇμ¥ò»μÄÂö3壬ÔúÏÂò»¸ö¸üDÂê¼tí£Ö1
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//ê1Äü¶¨ê±Æ÷2μÄí¨μà1ԤװÔؼÄ′æÆ÷
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;//ÔùÏòéϼÆêyê±£¬ò»μ©TIMx_CNT<TIMx_CCR1ê±í¨μà1ÎaÎTD§μçƽ£¬·ñÔòÎaóDD§μçƽ
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//OC1êä3öê1Äü
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//óDD§μçƽÎa¸ß
TIM_OCInitStruct.TIM_Pulse = 1;//±è½Ï2¶»ñ1μÄԤװÔØÖμ
TIM_OC1Init(TIM2,&TIM_OCInitStruct);
TIM_Cmd(TIM2,DISABLE);//Ïè2»ê1ÄüTIM2
//TIM1¤×÷Ôú′óÄ£ê½μÄÃÅ¿ØÖÆÏÂμÄ
WMêä3öÄ£ê½
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//ûóDê¼t·Ö¸î
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInitStruct.TIM_Prescaler = 720-1;//Ô¤·ÖÆμÖ죬10us¼Æêyò»′Î
TIM_TimeBaseInitStruct.TIM_Period = TIM4per;//ÖØ×°Öμ
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Gated);//TIM3ÎaÃÅ¿ØÄ£ê½
TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);//ê1ÄüTIM3μÄÖ÷′óÄ£ê½
TIM_SelectInputTrigger(TIM4,TIM_TS_ITR1);//Äú2¿′¥·¢£¬′óTIM2′¥·¢
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;//ÔúÏòéϼÆêyê±£¬ò»μ©TIMxCNT<TIMx_CCR1ê±í¨μà1ÎaÎTD§μçƽ£¬·ñÔòÎaóDD§μçƽ
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//OC1êä3öê1Äü
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//óDD§μçƽÎa¸ß
TIM_OCInitStruct.TIM_Pulse = TIM4Compare1;//±è½Ï2¶»ñ1μÄԤװÔØÖμ
TIM_OC1Init(TIM4,&TIM_OCInitStruct);
TIM_Cmd(TIM4,ENABLE);//ê1ÄüTIM3
}
//éèÖÃ
WMêä3ö
//Cycle:ÎaÖüÆú£¬μ¥λ(us£©
//Pulse_Num:ÎaÂö3å¸öêy£¨D¡óú3200£©
void TIM2_TIM4_PWM(u16 Cycle, u16 Pulse_Num)
{
u16 TIM4per = 0;
u32 Time = 0;
//¸Ä±äTIM3μÄÖØ×°Öμ¸Ä±äÂö3åÆμÂêÕaàïéèÖÃÂö3åÕ¼¿Õ±èÎa50%
//¸Ä±äTIM2μÄÔ¤·ÖÆμÆ÷oíÖØ×°Öμ¸Ä±äÂö3å¸öêy
Time = Cycle * Pulse_Num;
Time /= 100; //Ô¤·ÖÆμ7200£¬100us¼Æêyò»′Î
TIM4per = Cycle/10; //Ô¤·ÖÆμ720£¬10us¼Æêyò»′Î
TIM_SetAutoreload(TIM2, Time+1);//éèÖÃTIM2μÄÖØ×°Öμ
TIM_SetAutoreload(TIM4, TIM4per-1);//éèÖÃTIM3μÄÖØ×°Öμ
TIM_SetCompare1(TIM4,TIM4per/2);//Õ¼¿Õ±è50%
TIM_Cmd(TIM2,ENABLE);//ê1ÄüTIM2
}
把TIM4改成TIM3可以出现脉冲 为什么改成TIM4从定时器没反应 主定时器TIM2有 这是为什么
一周热门 更多>