采用原子哥的代码 修改为通道4只能捕获高电平 低电平不能进入中断要二次高电平到来时 才能得到高电平持续时间值
TIM_ICInitTypeDef TIM5_ICInitStructure;
//¶¨ê±Æ÷5í¨μà1êäèë2¶»ñÅäÖÃ
//arr£o×Ô¶ˉÖØ×°Öμ(TIM2,TIM5êÇ32λμÄ!!)
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM5_CH4_Cap_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //TIM5ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //ê1ÄüPORTAê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //GPIOA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //ÏÂà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA3
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM5); //PA3¸′óÃ붨ê±Æ÷5
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
//3õê¼»ˉTIM5êäèë2¶»ñ2Îêy
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=04 Ñ¡Ôñêäèë¶Ë IC4ó3éäμ½TI4éÏ
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI4éÏ
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM5_ICInitStructure.TIM_ICFilter = 0x00; //IC4F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC4,ENABLE); //ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_Cmd(TIM5,ENABLE ); //ê1Äü¶¨ê±Æ÷5
//DMA_Cmd(DMA1_Stream1, ENABLE);
//TIM_DMACmd(TIM5, TIM_DMA_CC4, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
TIMERdev.TIM5CH4_CAPTURE_STA=0;
TIMERdev.TIM5CH4_CAPTURE_VAL=0;
TIMERdev.timer=0;
TIMERdev.ti=0;
}
//2¶»ñ×′ì¬
//[7]:0,ûóD3é1|μÄ2¶»ñ;1,3é1|2¶»ñμ½ò»′Î.
//[6]:0,»1û2¶»ñμ½μíμçƽ;1,òѾ-2¶»ñμ½μíμçƽáË.
//[5:0]:2¶»ñμíμçƽoóòç3öμÄ′Îêy(¶Ôóú32붨ê±Æ÷à′Ëμ,1us¼ÆêyÆ÷¼ó1,òç3öê±¼ä:4294Ãë)
//¶¨ê±Æ÷5ÖD¶Ï·tÎñ3ìDò
void TIM5_IRQHandler(void)
{
//èç1ûê1óÃucos,Ôò°üà¨ÏÂÃæμÄí·Îļt¼′¿é.
//#if SYSTEM_SUPPORT_OS
OSIntEnter(); //½øèëÖD¶Ï
//#endif
if((TIMERdev.TIM5CH4_CAPTURE_STA&0X80)==0)//»1Î′3é1|2¶»ñ
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)//òç3ö
{
if(TIMERdev.TIM5CH4_CAPTURE_STA&0X40)//òѾ-2¶»ñμ½¸ßμçƽáË
{
if((TIMERdev.TIM5CH4_CAPTURE_STA&0X3F)==0X3F)//¸ßμçƽì«3¤áË
{
TIMERdev.TIM5CH4_CAPTURE_STA|=0X80; //±ê¼Ç3é1|2¶»ñáËò»′Î
TIMERdev.TIM5CH4_CAPTURE_VAL=0XFFFFFFFF;
}else TIMERdev.TIM5CH4_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET)//2¶»ñ4·¢éú2¶»ñê¼t
{
TIMERdev.ti++;
if(TIMERdev.TIM5CH4_CAPTURE_STA&0X40) //2¶»ñμ½ò»¸öϽμÑØ
{
LED1=1;
TIMERdev.TIM5CH4_CAPTURE_STA|=0X80; //±ê¼Ç3é1|2¶»ñμ½ò»′θßμçƽÂö¿í
TIMERdev.TIM5CH4_CAPTURE_VAL=TIM_GetCapture4(TIM5);//»ñè¡μ±Ç°μÄ2¶»ñÖμ.
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
}else //»1Î′¿aê¼,μúò»′Î2¶»ñéÏéyÑØ
{
TIMERdev.TIM5CH4_CAPTURE_STA=0; //Çå¿Õ
TIMERdev.TIM5CH4_CAPTURE_VAL=0;
TIMERdev.TIM5CH4_CAPTURE_STA|=0X40; //±ê¼Ç2¶»ñμ½áËéÏéyÑØ
TIM_Cmd(TIM5,DISABLE ); //1رն¨ê±Æ÷5
TIM_SetCounter(TIM5,0);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
TIM_Cmd(TIM5,ENABLE ); //ê1Äü¶¨ê±Æ÷5
LED1=0;
}
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC4|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
//#if SYSTEM_SUPPORT_OS
OSIntExit(); //′¥·¢èÎÎñÇD»»èíÖD¶Ï
//#endif
}
//Ö÷oˉêy
int main(void)
{
while(1)
{
if(TIMERdev.TIM5CH4_CAPTURE_STA&0X80) //3é1|2¶»ñμ½áËò»′θßμçƽ
{
TIMERdev.timer=TIMERdev.TIM5CH4_CAPTURE_STA&0X3F;
TIMERdev.timer*=0XFFFFFFFF; //òç3öê±¼ä×üoí
TIMERdev.timer+=TIMERdev.TIM5CH4_CAPTURE_VAL; //μÃμ½×üμĸßμçƽ걼ä
//printf("HIGH:%lld us
",temp); //′òó¡×üμĸßμãƽ걼ä
TIMERdev.TIM5CH4_CAPTURE_STA=0; //¿aÆôÏÂò»′Î2¶»ñ
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>