本帖最后由 mochou 于 2017-6-19 09:12 编辑
硬件已经做好,输入捕获口接PC8,PC9。使用TIM8的CH3,和CH4,但是调试不能捕获到脉冲啊,大家帮看下,谢谢。
TIM_ICInitTypeDef TIM8_ICInitStructure;
void TIM8_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); //ê1ÄüTIM8ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //ê1ÄüGPIOAê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PA0 Çå3y֮ǰéèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 êäèë
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_8); //PA0 ÏÂà-
//GPIO_ResetBits(GPIOC,GPIO_Pin_9); //PA0 ÏÂà-
//3õê¼»ˉ¶¨ê±Æ÷5 TIM8
TIM_TimeBaseStructure.TIM_Period = arr; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM8êäèë2¶»ñ2Îêy
// TIM8_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP; //?????????
TIM8_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM8_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM8_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM8_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM8_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM8, &TIM8_ICInitStructure);
//ÖD¶Ï·Ö×é3õê¼»ˉ
NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn; //TIM3ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÏèÕ¼óÅÏ輶2¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //′óóÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_ITConfig(TIM8,TIM_IT_Update|TIM_IT_CC3,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_Cmd(TIM8,ENABLE ); //ê1Äü¶¨ê±Æ÷5
}
u8 TIM8CH3_CAPTURE_STA=0; //êäèë2¶»ñ×′ì¬
u16 TIM8CH3_CAPTURE_VAL; //êäèë2¶»ñÖμ
//¶¨ê±Æ÷5ÖD¶Ï·tÎñ3ìDò
//void TIM8_IRQHandler(void)
void TIM8_CC_IRQHandler(void)
{
if((TIM8CH3_CAPTURE_STA&0X80)==0)//»1Î′3é1|2¶»ñ
{
if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)
{
if(TIM8CH3_CAPTURE_STA&0X40)//òѾ-2¶»ñμ½¸ßμçƽáË
{
if((TIM8CH3_CAPTURE_STA&0X3F)==0X3F)//¸ßμçƽì«3¤áË
{
TIM8CH3_CAPTURE_STA|=0X80;//±ê¼Ç3é1|2¶»ñáËò»′Î
TIM8CH3_CAPTURE_VAL=0XFFFF;
}else TIM8CH3_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM8, TIM_IT_CC3) != RESET)//2¶»ñ1·¢éú2¶»ñê¼t
{
if(TIM8CH3_CAPTURE_STA&0X40) //2¶»ñμ½ò»¸öϽμÑØ
{
TIM8CH3_CAPTURE_STA|=0X80; //±ê¼Ç3é1|2¶»ñμ½ò»′ÎéÏéyÑØ
TIM8CH3_CAPTURE_VAL=TIM_GetCapture3(TIM8);
TIM_OC3PolarityConfig(TIM8,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
}else //»1Î′¿aê¼,μúò»′Î2¶»ñéÏéyÑØ
{
TIM8CH3_CAPTURE_STA=0; //Çå¿Õ
TIM8CH3_CAPTURE_VAL=0;
TIM_SetCounter(TIM8,0);
TIM8CH3_CAPTURE_STA|=0X40; //±ê¼Ç2¶»ñμ½áËéÏéyÑØ
TIM_OC3PolarityConfig(TIM8,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
}
}
}
TIM_ClearITPendingBit(TIM8, TIM_IT_CC3|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
}
试了下用TIM3的REMAP也不行, |