测距用的是原子哥的捕获,只是测距的话没问题,都正常,一旦用的测得距离就不行了
以下是main函数内容[mw_shl_code=c,true]int main(void)
{
delay_init();
MotorDriver_Init();
TIM2_Int_Init(899,0);
TIM3_PWM_Init(9999,143);
TIM4_PWM_Init(999,1499); /
LED_Init();
UltrasonicWave_Configuration(); //3¬éù2¨3õê¼»ˉ
TIM5_Cap_Init(0XFFFF,72-1);
uart_init(115200); //′®¿ú3õê¼»ˉ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÖD¶Ï·Ö×é
usmart_dev.init(72);
//Æô¶ˉ3μ×ó Ö±DD
TIM_SetCompare2(TIM3,700);
delay_ms(500);
TIM_SetCompare2(TIM3,1110); //ÅäÖöæ»úÖ±DD
// TIM_SetCompare3(TIM4,500);
TIM_SetCompare3(TIM4,100);
// MotorDriver_Turn_Reverse(); //μç»úÕy×a
// MotorDriver_Turn_Stop();
delay_ms(500);
while(1)
{
/* while(1)
{
aa=bijiao();
}*/
do
{
state=bijiao();
if(state)
{
switch(state)
{
case jinle:
TIM_SetCompare2(TIM3,1350);
// LED0=!LED0;
break;
case yuanle:
TIM_SetCompare2(TIM3,700);
// LED1=!LED1;
break;
case zhenghao:
if(flag1)
TIM_SetCompare2(TIM3,1020);
if(!flag1)
TIM_SetCompare2(TIM3,1110);
break;
case tubian:
// aa++;
// if(aa==1)
// {
// TIM_SetCounter(TIM2,0);
// TIM_Cmd(TIM2, ENABLE); }
// if(aa==2)
// {
// TIM_Cmd(TIM2, DISABLE);
// TIM_GetCounter(TIM2); }
bb=1;
break;
// case 0:
}
}
delay_ms(100);
}
while(!bb);
// aa=0;
MotorDriver_Turn_Stop();
width=TIM_GetCounter(TIM2)*50;
LED0=0;
LED1=0;
while(1);
}
}
#include "bijiao.h"
#include "sys.h"
#include "wave.h"
#include "math.h"
#include "delay.h"
#include "usart.h"
extern u8 flag4;
u8 flag1;
// ±ê¼Ç¶æ»úμĽǶè
u8 bijiao()
{
static u8 aa,flag=0; //óÃà′±ê¼Ç¼ì2aí»±ä
float distance[2],*p=distance,chazhi;
UltrasonicWave_StartMeasure();
// while(!flag4); // μè′y2¶»ñ½áêø
if(flag==0)
{
// p[1]=0;
p[0]=getdistance();
flag=1;
}
if(flag==1)
{
// p[2]=0;
p[1]=getdistance();
flag=0;
}
/* if((p[2]>23&&p[2]<25)||(p[1]>23&&p[1]<25))
{
if(p[2]>23&&p[2]<25)
{
if((p[1]>25)&&(p[1]<45))
{
chazhi=p[1]-p[2];
if(chazhi>15)
{ aa++;
if(aa==2)
{ aa=0;
return tubian;
}
}
}
}
if(p[1]>23&&p[1]<25)
{
if((p[2]>25)&&(p[2]<45))
{
chazhi=p[2]-p[1];
if(chazhi>15)
{ aa++;
if(aa==2)
{ aa=0;
return tubian;
}
}
}
}
} */
if(flag==0)
{ if(p[0]>25)
{
flag1=1;
return yuanle;
}
else if(p[0]<23)
{
flag1=0;
return yuanle;
}
else return zhenghao;
}
else if(flag==1)
{ if(p[1]>25)
{
flag1=1;
return yuanle;
}
else if(p[1]<23)
{
flag1=0;
return yuanle;
}
else return zhenghao;
}
return 0;
}
float getdistance()
{
// UltrasonicWave_StartMeasure();
if(TIM5CH1_CAPTURE_STA1&0X80)
{
temp=TIM5CH1_CAPTURE_STA1&0X3F;
temp*=65536;
temp+=TIM5CH1_CAPTURE_VAL1;
TIM5CH1_CAPTURE_STA1=0;
}
printf("distance:%f cm
",temp*0.01954);
// delay_ms(60);
// flag4=1;
return temp*0.01954;
}[/mw_shl_code]
以下是串口打出来的数据
distance:45.919000 cm
distance:45.919000 cm
distance:44.981080 cm
distance:44.981080 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.023620 cm
distance:1.993080 cm
distance:1.993080 cm
distance:1.993080 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.981080 cm
distance:44.981080 cm
distance:44.981080 cm
distance:44.981080 cm
distance:42.753520 cm
distance:42.753520 cm
distance:45.469580 cm
distance:45.469580 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.981080 cm
distance:44.981080 cm
distance:86.718520 cm
distance:1.289640 cm
distance:1.289640 cm
distance:1.289640 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.981080 cm
distance:44.981080 cm
distance:44.961540 cm
distance:44.961540 cm
distance:36.090380 cm
distance:36.090380 cm
distance:44.981080 cm
distance:44.981080 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.961540 cm
distance:74.115220 cm
distance:0.742520 cm
distance:0.742520 cm
distance:0.742520 cm
distance:44.981080 cm
distance:44.981080 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.981080 cm
distance:29.310000 cm
distance:29.310000 cm
distance:29.310000 cm
distance:44.981080 cm
distance:44.981080 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.981080 cm
distance:44.981080 cm
distance:67.491160 cm
distance:0.488500 cm
distance:0.488500 cm
distance:0.488500 cm
distance:44.981080 cm
distance:44.981080 cm
distance:44.961540 cm
distance:44.961540 cm
distance:44.981080 cm
distance:22.568700 cm
distance:22.568700 cm
distance:22.568700 cm
请问哪里不对导致连最起码的测距都不准确了呀
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>