#include <reg51.h>
#include <intrins.h>
#include <beep.H>
#include <LCD1602.H>
sbit Beep = P1^5 ;
#define LED P3
#define VELOCITY_30C 3495
#define VELOCITY_23C 3453
#define uchar unsigned char
#define uint unsigned int
sbit Echo = P1^4; //回声接收端口
sbit Trig = P1^3;//超声触发端口
sbit LB=P2^0;
sbit LF=P2^1;
sbit RB=P2^2;
sbit RF=P2^3;
sbit ENA=P2^4;
sbit ENB=P2^5;
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
long int distance=0; //距离变量
static uint temp;
uchar count; //全局变量定义
uint key;
uint flag;
void delays(uint i)
{
while(i--);
}
void Delay_xMs(unsigned int x)
{
unsigned int i,j;
for(i = 0;i < x;i++ )
{
for(j = 0;j < 3;j++ )
{
;
}
}
}
void delayt(uint x)
{
uchar j;
while(x-- > 0)
{
for(j = 0;j < 125;j++)
{
;
}
}
}
void stop()
{
LF=0;
LB=0;
RF=0;
RB=0;
}
void forward()
{
LF=1;
LB=0;
RF=1;
RB=0;
}
void back()
{
LF=0;
LB=1;
RF=0;
RB=1;
}
void Init_MCU(void)
{
TMOD = 0x11; //定时器2初始化,设置为16位自动重装模式
TH0 = 0xfc; //1ms (65536-922)/256 高八位求模(除法)
TL0 = 0x66; // (65536-922)%256 低八位求余
ET1=1;
ET0 = 1; //开定时器2
EA = 1; //总中断使能
}
void Init_Parameter(void)
{
Trig =1; //Trig 触发控制信号输入
Echo = 1; //Echo 回响信号输出
count = 0;
distance = 0;
}
void Trig_SuperSonic(void)//出发声波
{
Trig = 1; //发送trig信号
delayt(1); //延时0.1ms
Trig = 0; //发送trig信号完毕
}
void Measure_Distance(void)
{
uchar l;
uint h,y;
TR0 = 1; //启动定时器0,收到回声则开始计时
while(Echo) //
{
;
}
TR0 = 0;
l = TL0; //把定时器0低8位装入L
h = TH0; //把定时器0高8位装入h
y = (h << 8) + l; //将计数值转换为16位(把h向左移八位加上低八位
y = y - 0xfc66; //us(微秒)部分,不足1ms的时间
temp=distance;
distance = y + 1000 * count; //计算总时间(定时器一次1ms) 单位:us
TL0 = 0x66;
TH0 = 0xfc;
delayt(30);
distance = VELOCITY_30C * distance /20000;
}
void distancescan()
{
if(distance>3000)
{
LCD_ShowNum(1,1,distance,4);
LED=0xff;
}
else if(distance>2500||distance<=3000)
{
LCD_ShowNum(1,1,distance,4);
LED=0xfe;
}
else if(distance>1500||distance<=2500)
{
LCD_ShowNum(1,1,distance,4);
LED=0xfc;
}
else if(distance>1000||distance<=1500)
{
LCD_ShowNum(1,1,distance,4);
LED=0xfb;
}
else if(distance>500||distance<=1500)
{
LCD_ShowNum(1,1,distance,4);
LED=0xf3;
}
else if(distance>250||distance<=500)
{
LCD_ShowNum(1,1,distance,4);
LED=0xef;
}
else if(distance>100||distance<=250)
{
LCD_ShowNum(1,1,distance,4);
LED=0xcf;
}
else if(distance<=100)
{
LCD_ShowNum(1,1,distance,4);
LED=0xc0;
}
}
void csbscan()
{
Trig_SuperSonic(); //触发超声波发射
while(Echo == 0) //等待回声 如果有回波则Echo=1 则跳出循环开始计时
{
;
}
Measure_Distance(); //计算脉宽并转换为距离
distancescan();
beep(0.16*distance+34);
Init_Parameter(); // 参数重新初始化
delayt(600); //延时,两次发射之间要至少有60ms间隔
}
unsigned char keyscan()
{
static unsigned char KeyNumber=0;
if(k1==0)
{ delays(1000);
if(k1==0)
KeyNumber=1;
}
if(k2==0)
{ delays(1000);
if(k2==0)
KeyNumber=2;
}
if(k3==0)
{ delays(1000);
if(k3==0)
KeyNumber=3;
}
return KeyNumber;
}
int main()
{
stop();
// forward();
LCD_Init();
LCD_ShowString(1,5,"mm");
Init_MCU();
Init_Parameter();
while(1)
{
flag=keyscan();
switch(flag)
{
case 1:back();LCD_ShowString(2,1,"back ");break;
case 2:forward();LCD_ShowString(2,1,"forward");break;
case 3:stop();LCD_ShowString(2,1,"stop ");flag=0;break;
default:;
}
csbscan();
}
}
void timer0 () interrupt 1
{
TF0 = 0;
TL0 = 0x66;
TH0 = 0xfc; //1ms
count++;
if(count == 18) //超声波回声脉宽最多18ms
{
TR0 =0;
TL0 = 0x66;
TH0 = 0xfc;
count = 0;
}
}
各位帮忙看看哪里有问题嘛,非常感谢
|