各位大侠,我修改正点原子的例程想用TIM8的CH3做输入捕获高电平脉宽,代码如下,用开发板测试不成功,请各位帮忙看下,谢谢。
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_APB1PeriphClockCmd(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 ÏÂà-
//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_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)
{
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_OC1PolarityConfig(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_OC1PolarityConfig(TIM8,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
}
}
}
TIM_ClearITPendingBit(TIM8, TIM_IT_CC3|TIM_IT_Update); //
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>