#include<reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
sbit IN1=P3^0;
sbit IN2=P3^1;
sbit IN3=P3^2;
sbit IN4=P3^3;
sbit RS=P2^6;
sbit RW=P2^5;
sbit E=P2^7;
sbit trig=P3^4;
sbit echo=P3^5;
sbit BELL=P2^4;
sbit PWM=P2^1;
bit flag = 0;
long SL,SR,S3;
uchar count,time=0;
unsigned long S = 0;
unsigned char code ASCII[13] = "0123456789.-M";
unsigned char disbuff[4] = { 0,0,0,0};
void delayms(zms)
{
uint x,y;
for(x=zms;x>0;x--)
for(y=110;y>0;y--);
}
void wc(com,com1)
{
E=0;
RS=0;
RW=0;
P0=com;
delayms(2);
E=1;
delayms(2);
E=0;
}
void wd(dat)
{
E=0;
RS=1;
RW=0;
P0=dat;
delayms(2);
E=1;
delayms(2);
E=0;
delayms(2);
}
void init(void)
{
wc(0x38,0);
delayms(5);
wc(0x38,0);
delayms(5);
wc(0x38,0);
delayms(5);
wc(0x38,1);
wc(0x01,1);
wc(0x06,1);
wc(0x0E,1);
}
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF;
if (Y) X |= 0x40;
X |= 0x80;
wc(X, 1);
wd(DData);
}
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF;
while (DData[ListLength]>0x19)
{
if (X <= 0xF)
{
DisplayOneChar(X, Y, DData[ListLength]);
ListLength++;
X++;
}
}
}
void measure(void)
{
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100;
if((S>=7000)||flag==1)
{
flag=0;
DisplayOneChar(0, 1, ASCII[11]);
DisplayOneChar(1, 1, ASCII[10]);
DisplayOneChar(2, 1, ASCII[11]);
DisplayOneChar(3, 1, ASCII[11]);
DisplayOneChar(4, 1, ASCII[12]);
}
else
{
disbuff[0]=S%1000/100;
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10%10;
DisplayOneChar(0, 1, ASCII[disbuff[0]]);
DisplayOneChar(1, 1, ASCII[10]);
DisplayOneChar(2, 1, ASCII[disbuff[1]]);
DisplayOneChar(3, 1, ASCII[disbuff[2]]);
DisplayOneChar(4, 1, ASCII[12]);
}
}
}
void zd0() interrupt 2
{
flag=1;
}
void StartModule()
{
trig = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
trig=0;
}
void timer0() interrupt 1
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
time++;
if(time<count) //5 左转90度,15归中,25右转90度
PWM=1;
else PWM=0;
if(time==200)
time=0;
}
void run(void)
{
IN1=0; IN2=1; IN3=1; IN4=0;
}
void back(void)
{
IN1=1; IN2=0; IN3=0; IN4=1;
}
void stop(void)
{
IN1=0; IN2=0; IN3=0; IN4=0;
}
void right(void)
{
IN1=0; IN2=1; IN3=0; IN4=1;
}
void left(void)
{
IN1=1; IN2=0; IN3=1; IN4=0;
}
void avoid()
{
count=5;
delayms(400);
StartModule();
measure();
SL=S;
count=25;
delayms(400);
StartModule();
measure();
SR=S;
count=15;
delayms(100);
StartModule();
measure();
S3=S;
if(SL<20||SR<20||S3<20)
{
back();
delayms(500);
}
if(SL<20&&SR<20&&S3<20)
{
back();
delayms(500);
}
if(SL<SR)
{
right();
delayms(400);
}
else
{
left();
delayms(400);
}
}
void main()
{
init();
while(1)
{
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1;
EA=1;
while(1)
{
StartModule();
while(!echo);
TR0=1;
while(echo);
TR0=0;
measure();
delayms(50);
if(S<300)
{
stop();
avoid();
}
else
if(S>400)
run();
}
}
}
|