就是自己暑假自学单片机,做了一个壁障小车用了两个红外壁障模块和一个超声波模块做的一个壁障小车,可是小车的壁障却很不理想,不知道哪里出问题所以感觉可能程序有问题,请大佬帮忙看看。
#include"reg51.h"
#include"xiaoche.h"
#include"intrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
char push_val_left=5;
char push_val_right=5;
u8 pwm_val_right=0;
u8 pwm_val_left=0;
u8 pwm_count=0;
char time=0;
unsigned char S=0;//???????????
void delay(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--) //i=xms??????xms????
for(j=112;j>0;j--);
}
void Delay10us(unsigned int i) //10us??????? ????????????????
{
unsigned int j;
do
{
j = 10;
do
{
_nop_();
}
while(--j);
}
while(--i);
}
void startModle()
{
TX=1;
Delay10us(2);
TX=0;
}
void count()//?????????????
{
S=0;
time=TH1*256+TL1;
TH1=0;
TL1=0;
S=time*0.17+10;
}
void forward()
{
IN1=0;//????????
IN2=1;
IN3=1;
IN4=0;
}
void stop()//???
{
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
void back()
{
IN1=1;//????????
IN2=0;
IN3=0;
IN4=1;
}
void left()
{
IN1=1;
IN2=0;
IN3=1;
IN4=0;
}
void right()
{
IN1=0;
IN2=1;
IN3=0;
IN4=1;
}
/******************************************************************/
/* ???????? */
void pwm_out_right_moto(void)
{
if(Right_PWM_ON)
{
if(pwm_val_right<=push_val_right) //20ms??????? 111 111 0000 0000 0000 00
{
EN2=1; //????6:20
}
else
{
EN2=0;
}
if(pwm_val_right>=20)
pwm_val_right=0;
}
else
{
EN2=0; //??δ????PWM??EN2=0 ???? ??
}
}
/*????????*/
void pwm_out_left_moto(void)
{
if(Left_PWM_ON)
{
if(pwm_val_left<=push_val_left)
{
EN1=1;
}
else
{
EN1=0;
}
if(pwm_val_left>=20)
pwm_val_left=0;
}
else
{
EN1=0; //??δ????PWM??EN1=0 ???? ??
}
}
void Timer0() interrupt 1
{
TH0 = 0xff; //??????0.1ms
TL0 = 0xa3;
pwm_count++;
if(pwm_count>=10)
{
pwm_val_right++;
pwm_val_left++;
pwm_out_right_moto();
pwm_out_left_moto();
pwm_count=0;
}
}
void main()
{
TMOD|=0X11;
EA=1;
ET0=1;
ET1=0;
TR0=1;
TR1=1;
TH1=0;
TL1=0;
count();
while(1)
{
if((hongwai_left==0)&&(S<=300))
{
stop();
delay(50);
right();
delay(50);
}
if((hongwai_right==0)&&(S<=300))
{
stop();
delay(50);
left();
delay(50);
}
else
{
forward();
}
}
}
|