#include
#include "intrins.h"
#include "codetab.h"
#include "LQ12864.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit led=P1^2;
sbit fmq=P1^4; //声光报警
电路
sbit Trig=P0^0;
sbit Echo=P3^2; //超声波模块
uchar
times; //time为接收超声波的时间,s为障碍物的距离
uchar juli[3]; //超声波的距离数字储存
void delay_20us()
{ uchar bt ;
for(bt=0;bt<100;bt++);
}
void OLED12864_IIC_(int x) //显示模块
{
juli[0]=x/100+'0';
juli[1]=x % 100 /10 + '0';
juli[2]=x%10+'0';
OLED_P8x16Str(00"The distance is");
OLED_P8x16Str(02juli);
OLED_P8x16Str(242" cm");
OLED_P8x16Str(04"Tanyu");
}
void delay100us(void) //蜂鸣器延时
{
unsigned char ab;
for(b=1;b>0;b--)
for(a=47;a>0;a--);
}
void Beep()
{
if(s<20||s>500)
{
led=0;
fmq=0;
delay100us();
fmq=1;
delay100us();
fmq=0;
delay100us();
fmq=1;
delay100us();
fmq=0;
delay100us();
fmq=1;
delay100us();
}
else
{
led=1;
fmq=1;
}
}
void Timer0Init()
{
TMOD=0x11;
ET0=1;
EA=1;
TH0=0X00;
TL0=0X00;
TR0=0;
}
void Timer1Init()
{
TH1=0x00;
TL1=0x00;
ET1=1;
TR1=0;
}
void Int0Init()
{
EA=1;
IT0=1;
EX0=1;
}
void main()
{
OLED_Init();
Timer0Init();
Timer1Init();
Int0Init();
Trig=0;
while(1)
{
EX0=1;
Trig=1;
delay_20us();
Trig=0;
while(Echo==0);
TR0=1;
TH1=0x00;
TL1=0x00;
TR1=1;
while(TH1 < 0xfa);
TR1=0;
EX0=0;
s=time*1.7;
time=0;
OLED12864_IIC_(s); //显示模块显示距离值
// Beep(); //距离过近时产生报警
}
}
void Int0() interrupt 0
{
TR0=0;
time = TH0*256+TL0;
TH0=0x00;
TL0=0x00;
TH1=0xff;
}