硬件已经做好,输入捕获口接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也不行,
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
void TIM3_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê1ÄüTIM3ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); //ê1ÄüGPIOAê±Öó
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer32¿·ÖÖØó3éä TIM3_CH3->PC8
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //Timer32¿·ÖÖØó3éä TIM3_CH3->PC8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PA0 Çå3y֮ǰéèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 êäèë
GPIO_Init(GPIOC, &GPIO_InitStructure);
//GPIO_ResetBits(GPIOA,GPIO_Pin_0); //PA0 ÏÂà-
//3õê¼»ˉ¶¨ê±Æ÷5 TIM3
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(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM3êäèë2¶»ñ2Îêy
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
//ÖD¶Ï·Ö×é3õê¼»ˉ
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏèÕ¼óÅÏ輶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(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_Cmd(TIM3,ENABLE ); //ê1Äü¶¨ê±Æ÷5
}
u8 TIM3CH3_CAPTURE_STA=0; //êäèë2¶»ñ×′ì¬
u16 TIM3CH3_CAPTURE_VAL; //êäèë2¶»ñÖμ
//¶¨ê±Æ÷5ÖD¶Ï·tÎñ3ìDò
void TIM3_IRQHandler(void)
{
if((TIM3CH3_CAPTURE_STA&0X80)==0)//»1Î′3é1|2¶»ñ
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
if(TIM3CH3_CAPTURE_STA&0X40)//òѾ-2¶»ñμ½¸ßμçƽáË
{
if((TIM3CH3_CAPTURE_STA&0X3F)==0X3F)//¸ßμçƽì«3¤áË
{
TIM3CH3_CAPTURE_STA|=0X80;//±ê¼Ç3é1|2¶»ñáËò»′Î
TIM3CH3_CAPTURE_VAL=0XFFFF;
}else TIM3CH3_CAPTURE_STA++;
}
}
LED1=!LED1;
if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)//2¶»ñ1·¢éú2¶»ñê¼t
{
if(TIM3CH3_CAPTURE_STA&0X40) //2¶»ñμ½ò»¸öϽμÑØ
{
TIM3CH3_CAPTURE_STA|=0X80; //±ê¼Ç3é1|2¶»ñμ½ò»′ÎéÏéyÑØ
TIM3CH3_CAPTURE_VAL=TIM_GetCapture3(TIM3);
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
}else //»1Î′¿aê¼,μúò»′Î2¶»ñéÏéyÑØ
{
TIM3CH3_CAPTURE_STA=0; //Çå¿Õ
TIM3CH3_CAPTURE_VAL=0;
TIM_SetCounter(TIM3,0);
TIM3CH3_CAPTURE_STA|=0X40; //±ê¼Ç2¶»ñμ½áËéÏéyÑØ
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
}
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
}
上面是使用TIM3的REMAP调试的程序,也不行啊,大侠们帮看下,另外程序是修改正点原子的精英版输入捕获试验的例程。
一周热门 更多>