#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顯示緩沖區(qū)1(即上排16個(gè)字符)
char idata disp_buf2[16];//1602顯示緩沖區(qū)2(即下排16個(gè)字符)
uchar code table[]="celiang ok !";
uchar i;
char idata julidata[3]; //存放距離值變量
unsigned long int JuLi;//距離
unsigned char Gao,Di;//存儲(chǔ)計(jì)時(shí)器高位,低位
void ChuShiHua();//初始化
void FaShe();//發(fā)射聲波
void Shou();//接收外部中斷程序
void Deng();//空等待
//
void delay(uint n) //延時(shí)時(shí)間為n*1ms
{ uchar x=0;
for(;n>0;n--)
{
for(;x<250;x++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void check_busy() //LCD忙檢測函數(shù)
{
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) //寫數(shù)據(jù)
{ check_busy();
rw=0;
rs=1;
P0=dat;
en=1;
en=0;
}
void lcd_init() //初始化
{
lcd_wcom(0x38); //8位,雙列,5*7
lcd_wcom(0x0c); //開顯示,關(guān)光標(biāo),光標(biāo)所在字符不閃爍
lcd_wcom(0x06); //顯示地址遞增
lcd_wcom(0x01); //清屏,光標(biāo)移到左上角
}
/
void int_display() //初始顯示
{
uchar m=0;
lcd_init();
lcd_wcom(0x80); //顯示地址
for(m=0;m<16;m++)
{
lcd_wdat(table[m]); //將數(shù)組中字符顯示出來
delay(1000);
}
delay(10);
lcd_wcom(0x01);
}
void updata_disp() //更新緩沖區(qū)數(shù)據(jù)程序
{
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() //顯示緩沖區(qū)數(shù)據(jù)函數(shù)
{
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 //定時(shí)中斷
{
TH0=0x3c;
TL0=0xb0;
i++;
if(i==10)
{
i=0;
display(); //更新顯示數(shù)據(jù)
}
}
//初始化------------------------------------------------------
void ChuShiHua()
{
IE = 0x82; //開T0中斷
TMOD = 0x10; //定時(shí)器1模式0,定時(shí)器0模式1
PX1 = 1; //INT1高優(yōu)先級
PT0 = 0; //T0 低級中斷
IT1 = 0; //INT1電平觸發(fā)0
TR0 = 1; //啟動(dòng)定時(shí)器0
}
//空等待------------------------------------------------------
void Deng()
{
unsigned int i,j;
for (i = 0; i 100; i++)
for (j = 0; j 100; j++);
}
//發(fā)射方波----------------------------------------------------
void FaShe()
{
unsigned int counter;
IE = 0; //關(guān)中斷
TH1 = 0;
TL1 = 0; //計(jì)數(shù)器清零
TR1 = 1; //啟動(dòng)計(jì)算器;
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; //關(guān)中斷
JuLi = 0;
TR1 = 0; //關(guān)定時(shí)器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; //關(guān)int1中斷,開顯示中斷
}
//主程序------------------------------------------------------
main()
{
display(); //更新顯示數(shù)據(jù)
int_display();
ChuShiHua();
while (1)
{
FaShe();
Deng();
//數(shù)據(jù)送入顯示緩沖區(qū)
}
}
評論