1. 程式人生 > >使用STM32F407控制5路超聲波感測器

使用STM32F407控制5路超聲波感測器

void SingleChannelHandler(TIM_CAPTURE * tch ) { if((tch->CAPTURE_STA&0X80)==0)//»¹Î´³É¹¦²¶»ñ { if(TIM_GetITStatus(tch->timX, TIM_IT_Update) != RESET)//Òç³ö { if(tch->CAPTURE_STA&0X40)//ÒѾ­²¶»ñµ½¸ßµçƽÁË { if((tch->CAPTURE_STA&0X3F)==0X3F)//¸ßµçƽ̫³¤ÁË { tch->CAPTURE_STA|=0X80; //±ê¼Ç³É¹¦²¶»ñÁËÒ»´Î tch->CAPTURE_VAL=0XFFFFFFFF; }else tch->CAPTURE_VAL++; } } if(TIM_GetITStatus(tch->timX, tch->TIM_IT_CCX) != RESET)//²¶»ñ1·¢Éú²¶»ñʼþ { if(tch->CAPTURE_STA&0X40) //ÒѾ­²¶»ñµ½¸ßµçƽ£¬±¾´Î²¶»ñµ½Ï½µÑØ { tch->CAPTURE_STA|=0X80; //±ê¼Ç³É¹¦²¶»ñµ½Ò»´Î¸ßµçƽÂö¿í //tch->CAPTURE_VAL=TIM_GetCapture1(tch->timX);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ. switch( tch->TIM_IT_CCX ) // ¸ù¾Ý²»Í¬Í¨µÀÑ¡Ôñº¯Êý { case TIM_IT_CC1: tch->CAPTURE_VAL=TIM_GetCapture1(tch->timX);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ. TIM_OC1PolarityConfig(tch->timX,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ break; case TIM_IT_CC2: tch->CAPTURE_VAL=TIM_GetCapture2(tch->timX);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ. TIM_OC2PolarityConfig(tch->timX,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ break; case TIM_IT_CC3: tch->CAPTURE_VAL=TIM_GetCapture3(tch->timX);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ. TIM_OC3PolarityConfig(tch->timX,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ break; case TIM_IT_CC4: tch->CAPTURE_VAL=TIM_GetCapture4(tch->timX);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ. TIM_OC4PolarityConfig(tch->timX,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ break; default: break; } //printf(" tch->TIM_IT_CCX = %d ",tch->TIM_IT_CCX); //´òÓ¡×ܵĸߵãƽʱ¼ä //TIM_OC1PolarityConfig(tch->timX,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ }else //»¹Î´¿ªÊ¼,µÚÒ»´Î²¶»ñÉÏÉýÑØ { tch->CAPTURE_STA=0; //Çå¿Õ tch->CAPTURE_VAL=0; tch->CAPTURE_STA|= 0X40; //±ê¼Ç²¶»ñµ½ÁËÉÏÉýÑØ TIM_Cmd(tch->timX,DISABLE ); //¹Ø±Õ¶¨Ê±Æ÷ TIM_SetCounter(tch->timX,0); // printf(" tch->CAPTURE_STA = %d ",tch->CAPTURE_STA); //´òÓ¡×ܵĸߵãƽʱ¼ä //TIM_OC1PolarityConfig(tch->timX,TIM_ICPolarity_Falling); //CC1P=1 ÉèÖÃΪϽµÑز¶»ñ switch( tch->TIM_IT_CCX ) { case TIM_IT_CC1: TIM_OC1PolarityConfig(tch->timX,TIM_ICPolarity_Falling); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ break; case TIM_IT_CC2: TIM_OC2PolarityConfig(tch->timX,TIM_ICPolarity_Falling); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ break; case TIM_IT_CC3: TIM_OC3PolarityConfig(tch->timX,TIM_ICPolarity_Falling); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ break; case TIM_IT_CC4: TIM_OC4PolarityConfig(tch->timX,TIM_ICPolarity_Falling); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ break; default: break; } TIM_Cmd(tch->timX,ENABLE ); //ʹÄܶ¨Ê±Æ÷s } } } TIM_ClearITPendingBit(tch->timX, tch->TIM_IT_CCX | TIM_IT_Update); //Çå³ýÖжϱê־λ }