void PWM_IN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM4_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ê1ÄüTIM4ê±Öó
RCC_APB2PeriphClockCmd(RCC_GPIO_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = TIM4_CH1 | TIM4_CH2 | TIM4_CH3 | TIM4_CH4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIO_TIM4,TIM4_CH1 | TIM4_CH2 | TIM4_CH3 | TIM4_CH4);
//3õê¼»ˉ¶¨ê±Æ÷4 TIM4
TIM_TimeBaseStructure.TIM_Period = 2499; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =71; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM4êäèë2¶»ñ2Îêy
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPolarity =TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM4_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM4_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM4_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM4_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM_Cmd(TIM4,ENABLE );
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE); //ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE);
}
/*====================================================================================================*/
/*====================================================================================================*
**oˉêy : TIM4_IRQHandler
**1|Äü : TIM4ÖD¶Ï·tÎñ
**êäèë : None
**Y”3ö : None
**±¸×¢ : None
**====================================================================================================*/
/*====================================================================================================*/
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) //2¶»ñ1·¢éú2¶»ñê¼t
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); //Çå3yÖD¶Ï±ê־λ
if(GPIO_ReadInputDataBit(GPIO_TIM4,TIM4_CH1) == 1)
{
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
d=TIM_GetCapture1(TIM4);
}
else
{
LED_Init();
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
d=TIM_GetCapture1(TIM4);
if(Rise[0]>Drop[0]) RC_Pwm_In[0] = 65535-Rise[0] + Drop[0];
else RC_Pwm_In[0] = Drop[0] - Rise[0];
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET) //2¶»ñ1·¢éú2¶»ñê¼t
{ //LED_Init();
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); //Çå3yÖD¶Ï±ê־λ
if(GPIO_ReadInputDataBit(GPIO_TIM4,TIM4_CH2) == 1)
{
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
//d=TIM_GetCapture2(TIM4);
}
/* else
{
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
d=TIM_GetCapture2(TIM4);
if(Rise[1]>Drop[1]) RC_Pwm_In[1] = 65535-Rise[1] + Drop[1];
else RC_Pwm_In[1] = Drop[1] - Rise[1];
} */
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET) //2¶»ñ1·¢éú2¶»ñê¼t
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); //Çå3yÖD¶Ï±ê־λ
if(GPIO_ReadInputDataBit(GPIO_TIM4,TIM4_CH3) == 1)
{
TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
Rise[2]=TIM_GetCapture3(TIM4);
}
else
{
TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
Drop[2]=TIM_GetCapture3(TIM4);
if(Rise[2]>Drop[2]) RC_Pwm_In[2] = 65535-Rise[2] + Drop[2];
else RC_Pwm_In[2] = Drop[2] - Rise[2];
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET) //2¶»ñ1·¢éú2¶»ñê¼t
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); //Çå3yÖD¶Ï±ê־λ
if(GPIO_ReadInputDataBit(GPIO_TIM4,TIM4_CH4) == 1)
{
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
Rise[3]=TIM_GetCapture4(TIM4);
}
else
{
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
Drop[3]=TIM_GetCapture4(TIM4);
if(Rise[3]>Drop[3]) RC_Pwm_In[3] = 65535-Rise[3] + Drop[3];
else RC_Pwm_In[3] = Drop[3] - Rise[3];
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
请问我用的MDK在线调试会不会有误差,我用单片机捕获它自己产生的pwm时能正常工作
一周热门 更多>