求:基于89c52单片机的超声波测距的程序。

2025-01-03 23:04:00
推荐回答(1个)
回答1:

void CLR_SCREEN(void);
void LCD_DISOFF(void);
void LCD_DISON(void);
void LCD_CUROFF(void);
void LCD_CURON(void);
void LCD_INT(void);
void LCD_PRINT(unsigned char row,unsigned char col,unsigned char count,unsigned int dats);
void LCD_PRINTF(unsigned char row,unsigned char col,unsigned char InBuffer[33]);
void LCD_PRINTCHAR(unsigned char row,unsigned char col,unsigned char Buffer);
void CPU_DELAYTIME(unsigned int count);
void LCD_WRI(unsigned char adr);
void LCD_WRD(unsigned char asc);
//--------------------------------------------------显示数值
void LCD_PRINT(unsigned char row,unsigned char col,unsigned char count,unsigned int dats){
unsigned int sxh4;
unsigned int sxh3;
unsigned int sxh2;
unsigned int sxh1;
LCD_DISON();
CPU_DELAYTIME(100);
switch (row)
{
case 1:row=0x80;break;
case 2:row=0xC0;break;
default: break;
}
LCD_WRI(row+col-1);
CPU_DELAYTIME(200);
sxh4=dats/10;
sxh3=sxh4/10;
sxh2=sxh3/10;
sxh1=sxh2/10;
if(count>=5){LCD_WRD(sxh1+48);}
if(count>=4){LCD_WRD(sxh2-sxh1*10+48);}
if(count>=3){LCD_WRD(sxh3-sxh2*10+48);}
if(count>=2){LCD_WRD(sxh4-sxh3*10+48);}
LCD_WRD(dats-sxh4*10+48);
}
//--------------------------------------------------LCD初始化
void LCD_INT(void){
LCD_WRI(0X38); //8位数据总线 两行显示模式 5X7点阵+光标显示模式
LCD_CUROFF(); //无光标跟随
LCD_WRI(0X80); //设置显示地址00H
}
//--------------------------------------------------关闭显示
void LCD_DISOFF(void){
LCD_WRI(0X08);
}
//--------------------------------------------------打开显示
void LCD_DISON(void){
LCD_WRI(0X0C);
}
//--------------------------------------------------光标消失
void LCD_CUROFF(void){
LCD_WRI(0X0C);
}
//--------------------------------------------------光标跟随
void LCD_CURON(void){
LCD_WRI(0X0F);
}
//--------------------------------------------------显示字符串
void LCD_PRINTF(unsigned char row,unsigned char col,unsigned char InBuffer[33])
{
unsigned char I_LOOP;
LCD_DISON();
CPU_DELAYTIME(500);
switch (row)
{
case 1:row=0x80;break;
case 2:row=0xC0;break;
default: break;
}
LCD_WRI(row+col-1);
for(I_LOOP=0;I_LOOP<=32;I_LOOP++){
if(InBuffer[I_LOOP]=='%'&&InBuffer[I_LOOP+1]=='d'){ break;}
if(col+I_LOOP==17&&row==0x80){LCD_WRI(0xC0);}
LCD_WRD(InBuffer[I_LOOP]);
}
}
//------------------------------------------------------------------
void LCD_PRINTCHAR(unsigned char row,unsigned char col,unsigned char Buffer){
LCD_DISON();
CPU_DELAYTIME(500);
switch (row)
{
case 1:row=0x80;break;
case 2:row=0xC0;break;
default: break;
}
LCD_WRI(row+col-1);
LCD_WRD(Buffer);
}
//-------------------------------------------------清屏指令
void CLR_SCREEN(void){
LCD_INT();
LCD_WRI(1);
CPU_DELAYTIME(200);
}
//-------------------------------------------------延时函数
void CPU_DELAYTIME(unsigned int count){
unsigned int iu_LOOP;
for(iu_LOOP=0;iu_LOOP<=count;iu_LOOP++){; }
}

//-----------------------------------------------显示屏写指令子程序
void LCD_WRI(unsigned char adr)
{ LCD_RW=0;
LCD_RS=0;
LCD_E=1;
LCD_DAT=adr;
LCD_E=0;
CPU_DELAYTIME(20);
}
//----------------------------------------------显示屏写数据子程序
void LCD_WRD(unsigned char asc){
LCD_RW=0;
LCD_RS=1;
LCD_E=1;
LCD_DAT=asc;
LCD_E=0;
CPU_DELAYTIME(80);
}