想做一个小车,用了三个超声波,在小车的前和左右。想通过左右的距离检测,控制舵机左右转动。电机只有一个,同轴带动两个轮子转动。前面的两个轮子连接舵机。达到转向的目的。现在的问题是左右超声波模块好像没有起作用,十分困惑。
#include<reg52.h>
#include <intrins.h>
sbit ECHO_L= P2^1; //超声波接口定义
sbit TRIG_L = P2^0; //超声波接口定义
sbit ECHO_R = P2^7; //超声波接口定义
sbit TRIG_R = P2^6;
sbit ECHO_b=P2^4;
sbit TRIG_b=P2^3; //超声波接口定义
#define dianji_pwm P2^2 //PWM信号端
sbit Sevro_moto_pwm= P1^7 ;
sbit IN1=P2^0;
sbit IN2=P2^1;
sbit ENA=P2^2;
//光电管排布 : 左1 左2 右2 右1
sbit left1=P0^0;//寻迹左1
sbit left2=P0^1;//寻迹左2
sbit right2=P0^2;//寻迹右2
sbit right1=P0^3;//寻迹右1
bit Right_moto_stop=1;
#define dianji_go {IN1=0,IN2=1,ENA=1;} //电机向前走
#define dianji_back {IN1=1,IN2=0,ENA=1;} //电机向后走
#define dianji_Stop {ENA=0;} //电机停转
unsigned char pwm_val_dianji =0;//变量定义
unsigned char push_val_dianji =0;//电机占空比N/20
unsigned char pwm_val_duoji= 0;//变量定义
unsigned char push_val_duoji =15;//舵机归中,产生约,1.5MS 信号
unsigned int time;
unsigned int timer;
float S1;
float S2;
unsigned int flag;
unsigned int time=0; //时间变量
unsigned int timer=0; //延时基准变量
unsigned char timer1=0; //扫描时间变量
void delay(unsigned int k) //延时函数 11.059mhz下约10ms
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
void delay_1ms(unsigned char x) //1ms延时函数100ms以内可用
{
unsigned char i;
while(x--)
for(i=124;i>0;i--);
}
void Delay40us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
void Conut1(void) //计算距离
{
TRIG_L= 1; //拉高超声波模块触发IO
Delay40us();// 延时20us
TRIG_L= 0; //拉低超声波模块触发IO
while(ECHO_L==0);//等待超声波模块输出IO拉高
TR0 = 1; //开启定时器计时
while(ECHO_L==1); //等待超声波模块输出IO拉低
TR0 = 0;
time=(TH0*256+TL0);
S1=time*0.170;
TH0=0;
TL0=0;
}
void Conut2(void) //计算距离
{
TRIG_R= 1; //拉高超声波模块触发IO
Delay40us();// 延时20us
TRIG_R= 0; //拉低超声波模块触发IO
while(ECHO_R==0);//等待超声波模块输出IO拉高
TR0 = 1; //开启定时器计时
while(ECHO_R==1); //等待超声波模块输出IO拉低
TR0 = 0;
time=(TH0*256+TL0);
S2=time*0.170;
TH0=0;
TL0=0; //算出来是CM
}
void Conut3(void) //计算距离
{
TRIG_b= 1; //拉高超声波模块触发IO
Delay40us();// 延时20us
TRIG_b= 0; //拉低超声波模块触发IO
while(ECHO_b==0);//等待超声波模块输出IO拉高
TR0 = 1; //开启定时器计时
while(ECHO_b==1); //等待超声波模块输出IO拉低
TR0 = 0;
time=(TH0*256+TL0);
S2=time*0.170;
TH0=0;
TL0=0; //算出来是CM
}
/*调节push_val_duoji的值改变舵机角度,占空比 */
void pwm_Servomoto(void)
{
if(pwm_val_duoji<=push_val_duoji)
Sevro_moto_pwm=1;
else {Sevro_moto_pwm=0;}
if(pwm_val_duoji>=200)
pwm_val_duoji=0;
}
/*调节电机占空比*/
void pwm_out_dianji(void)
{
if(pwm_val_dianji<=push_val_dianji)
{
ENA=1;
}
else
{
ENA=0;
}
if(pwm_val_dianji>=20)
pwm_val_dianji=0;
else
{
ENA=0;
}
}
void COMM(void)
{
Conut1();
Conut2();
if(S3<20)
{
ENA=0;;
if(S1<10&&S2>10) flag=0;
if(S2<10&&S1>10) flag=1;
if(S1<10&&S2<10) flag=2;
if(S2>10&&S1>10) flag=3;
switch(flag)
{
case 0 :push_val_duoji=17; break;
case 1 :push_val_duoji=11; break;
case 2 :push_val_duoji=14; break;
case 3 :push_val_duoji=14; break;
}}
}
void COMM1()
{
}
/***************************************************/
void main(void)
{
TMOD=0X11;
TH1=(65536-100)/256; //100US定时
TL1=(65536-100)%256;
TR1= 1;
ET1= 1;
EA = 1;
push_val_duoji=15; //舵机归中
delay();
while(1) //无限循环
{
COMM();
}
}
///*TIMER1中断服务子函数产生PWM信号*/
void timer0()interrupt 1
{
}
void time1()interrupt 3
{
TH1=(65536-100)/256; //100US定时
TL1=(65536-100)%256;
timer++; //定时器100US为准。在这个基础上延时
pwm_val_duoji++;
pwm_out_dianji();
pwm_Servomoto();
}
|