#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit rs=P1^2;
sbit rw=P1^1;
sbit en=P1^0;
char idata disp_buf1[16];//1602显示缓冲区1(即上排16个字符)
char idata disp_buf2[16];//1602显示缓冲区2(即下排16个字符)
uchar code table[]="celiang ok !";
uchar i;
char idata julidata[3]; //存放距离值变量
unsigned long int JuLi;//距离
unsigned char Gao,Di;//存储计时器高位,低位
void ChuShiHua();//初始化
void FaShe();//发射声波
void Shou();//接收外部中断程序
void Deng();//空等待
//
void delay(uint n) //延时时间为n*1ms
{ uchar x=0;
for(;n>0;n--)
{
for(;x<250;x++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void check_busy() //LCD忙检测函数
{
P0=0xff;
rs=0;
rw=1;
en=1;
while(P0&0x80);
en=0;
}
void lcd_wcom(uchar com) //写命令
{ check_busy();
rw=0;
rs=0;
P0=com;
en=1;
en=0;
}
void lcd_wdat(uchar dat) //写数据
{ check_busy();
rw=0;
rs=1;
P0=dat;
en=1;
en=0;
}
void lcd_init() //初始化
{
lcd_wcom(0x38); //8位,双列,5*7
lcd_wcom(0x0c); //开显示,关光标,光标所在字符不闪烁
lcd_wcom(0x06); //显示地址递增
lcd_wcom(0x01); //清屏,光标移到左上角
}
/
void int_display() //初始显示
{
uchar m=0;
lcd_init();
lcd_wcom(0x80); //显示地址
for(m=0;m<16;m++)
{
lcd_wdat(table[m]); //将数组中字符显示出来
delay(1000);
}
delay(10);
lcd_wcom(0x01);
}
void updata_disp() //更新缓冲区数据程序
{
disp_buf1[0]=p;
disp_buf1[1]=r;
disp_buf1[2]=s;
disp_buf1[3]=e;
disp_buf1[4]=n;
disp_buf1[5]=t;
disp_buf1[6]=-;
disp_buf1[7]=d;
disp_buf1[8]=i;
disp_buf1[9]=s;
disp_buf1[10]=t;
disp_buf1[11]=a;
disp_buf1[12]=n;
disp_buf1[13]=t;
disp_buf1[14]=:;
disp_buf1[15]=:;
disp_buf2[0]=-;
disp_buf2[1]=-;
disp_buf2[2]=>;
disp_buf2[3]=0;
disp_buf2[4]=0;
disp_buf2[5]=0;
disp_buf2[6]=0;
disp_buf2[7]=julidata[0]+0x30;
disp_buf2[8]=julidata[1]+0x30;
disp_buf2[9]=julidata[2]+0x30;
disp_buf2[10]=c;
disp_buf2[11]=m;
disp_buf2[12]=*;
disp_buf2[13]=*;
disp_buf2[14]=*;
disp_buf2[15]=*;
}
void display() //显示缓冲区数据函数
{
uchar i;
lcd_wcom(0x80);
for(i=0;i<16;i++)
lcd_wdat(disp_buf1[i]);
lcd_wcom(0xc0);
for(i=0;i<16;i++)
lcd_wdat(disp_buf2[i]);
}
void int_t0() interrupt 1 using 1 //定时中断
{
TH0=0x3c;
TL0=0xb0;
i++;
if(i==10)
{
i=0;
display(); //更新显示数据
}
}
//初始化------------------------------------------------------
void ChuShiHua()
{
IE = 0x82; //开T0中断
TMOD = 0x10; //定时器1模式0,定时器0模式1
PX1 = 1; //INT1高优先级
PT0 = 0; //T0 低级中断
IT1 = 0; //INT1电平触发0
TR0 = 1; //启动定时器0
}
//空等待------------------------------------------------------
void Deng()
{
unsigned int i,j;
for (i = 0; i 100; i++)
for (j = 0; j 100; j++);
}
//发射方波----------------------------------------------------
void FaShe()
{
unsigned int counter;
IE = 0; //关中断
TH1 = 0;
TL1 = 0; //计数器清零
TR1 = 1; //启动计算器;
for (counter = 0;counter 16; )
{
P3 = P3 ^ 0x20;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
counter++;
}
P3_5 = 1;
for (counter = 0;counter 300; counter++) _nop_();//2.1315
IE = 0x86; //开中断
}
//收,外部中断程序---------------------------------------------
void Shou() interrupt 2 //外部中断INT1
{
IE = 0x00; //关中断
JuLi = 0;
TR1 = 0; //关定时器1
Gao = TH1;
Di = TL1;
JuLi = JuLi | Gao;
JuLi = JuLi < 8;
JuLi = JuLi | Di;
JuLi = JuLi - 44;
JuLi = JuLi * 85;
JuLi = JuLi/10000;
julidata[0] = JuLi / 100;
julidata[1] = (JuLi - (julidata[0]*100))/10;
julidata[2] = JuLi;
updata_disp();
IE = 0x82; //关int1中断,开显示中断
}
//主程序------------------------------------------------------
main()
{
display(); //更新显示数据
int_display();
ChuShiHua();
while (1)
{
FaShe();
Deng();
//数据送入显示缓冲区
}
}