下面是程序,有大神告诉一下小白要如何解决吗?在这个论坛下载的
- /**************************************************************/
- //使用时将US100跳帽拔下,使其设置为电平触发模式
- /***************************************************************/
- #include <reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- //引脚定义
- sbit Trig1 = P2 ^ 1; //
- sbit Ecno1 = P2 ^ 0; //
- sbit Trig2 = P3 ^ 2; //
- sbit Ecno2 = P3 ^ 3; //
- sbit Trig3 = P1 ^ 2; //
- sbit Ecno3 = P1 ^ 3; //
- sbit Trig4 = P3 ^ 0; //
- sbit Ecno4 = P3 ^ 1; //
- #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
- #define LCD_data P0 //数据口
- sbit LCD_RS = P3^5; //寄存器选择输入
- sbit LCD_RW = P3^6; //液晶读/写控制
- sbit LCD_EN = P3^4; //液晶使能控制
- sbit LCD_PSB = P3^1; //串/并方式控制
- sbit wela = P2^6;
- sbit dula = P2^7;
- // 函数声明
- void Write_char(uchar dat);
- bit lcd_busy();
- void Lcd_init(void);
- void LCD_Write_string(uchar *str);
- void LCD_set_xy( unsigned char x, unsigned char y );
- void StartModule() ;
- void Clr_Scr();
- unsigned char code num[]={"0123456789 :."};
- unsigned char code table1[]={"前方距离:"};
- unsigned char code table2[] ={"后方距离:"};
- unsigned char code table3[] ={"左方距离:"};
- unsigned char code table4[] ={"右方距离:"};
- unsigned char code M[] = {"CM"};
- unsigned int time1=0;
- unsigned int time2=0;
- unsigned int time3=0;
- unsigned int time4=0;
- long S1=0;
- long S2=0;
- long S3=0;
- long S4=0;
- bit flag =0;
-
- unsigned char disbuff[4]={ 0,0,0,0,};
- /******************************************************************************/
- /*******************************************************************/
- /* */
- /* 延时函数 */
- /* */
- /*******************************************************************/
- void delay(uint x)
- {
- uint i,j;
- for(i= x;i>0;i--)
- for(j=110;j>0;j--);
- }
- void lcd_wcmd(uchar cmd)
- {
- while(lcd_busy());
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_EN = 0;
- _nop_();
- _nop_();
- P0 = cmd;
- delayNOP();
- LCD_EN = 1;
- delayNOP();
- LCD_EN = 0;
- }
- void Lcd_init(void) //初始化LCD
- {
- LCD_PSB = 1; //并口方式
- lcd_wcmd(0x34); //扩充指令操作
- delay(5);
- lcd_wcmd(0x30); //基本指令操作
- delay(5);
- lcd_wcmd(0x0C); //显示开,关光标
- delay(5);
- lcd_wcmd(0x01); //清除LCD的显示内容
- delay(5);
- }
- bit lcd_busy()
- {
- bit result;
- LCD_RS = 0;
- LCD_RW = 1;
- LCD_EN = 1;
- delayNOP();
- result = (bit)(P0&0x80);
- LCD_EN = 0;
- return(result);
- }
- /******************************************************************************/
- /*void Write_char(uchar dat) //写指令或数据
- {
- while(lcd_busy());
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_EN = 0;
- P0 = dat;
- delayNOP();
- LCD_EN = 1;
- delayNOP();
- LCD_EN = 0;
- }
- */
- /******************************************************************************/
- void Delaynms(unsigned int di) //延时
- {
- unsigned int da,db;
- for(da=0;da<di;da++)
- for(db=0;db<10;db++);
- }
- /******************************************************************************/
- void Clr_Scr(void)//清屏函数
- {
- lcd_wcmd(0x01);
- }
- /******************************************************************************/
- void LCD_set_xy( unsigned char X, unsigned char Y )//设置LCD显示的起始位置,X为行,Y为列
- {
- uchar pos;
- if (X==0)
- {X=0x80;}
- else if (X==1)
- {X=0x90;}
- else if (X==2)
- {X=0x88;}
- else if (X==3)
- {X=0x98;}
- pos = X+Y ;
- lcd_wcmd(pos); //显示地址
- }
- void lcd_wdat(uchar dat)
- {
- while(lcd_busy());
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_EN = 0;
- P0 = dat;
- delayNOP();
- LCD_EN = 1;
- delayNOP();
- LCD_EN = 0;
- }
- /******************************************************************************/
-
- void LCD_Write_string(uchar *str)//D′è?×?·?′?oˉêy
- {
-
-
- for(;*str!='\0';str++)
- {
- lcd_wdat(*str);
- delay(2);
- }
-
- }
- /******************************************************************************/
- void LCD_Write_number(unsigned char s)// 数字显示函数
- {
- lcd_wdat(num[s]);
- Delaynms(1);
- }
- /******************************************************************************/
- void Lcd_Mark2(void)
- {
- Clr_Scr();//清屏
- LCD_set_xy(0,0);
- LCD_Write_string("前方");//
- LCD_set_xy(0,7);
- LCD_Write_string("CM");
- LCD_set_xy(1,0);
- LCD_Write_string("后方");//
- LCD_set_xy(1,7);
- LCD_Write_string("CM");
- LCD_set_xy(2,0);
- LCD_Write_string("左方");//
- LCD_set_xy(2,7);
- LCD_Write_string("CM");
- LCD_set_xy(3,0);
- LCD_Write_string("右方");//
- LCD_set_xy(3,7);
- LCD_Write_string("CM");
- }
- /********************************************************/
- void Conut1(void)
- {
- time1=TH0*256+TL0;
- TH0=0x00;
- TL0=0x00;
- S1=time1*1.87/100; // 11.0592M晶振
- if(flag==1) //超出测量
- {
- flag=0;
- LCD_set_xy( 0, 5 );
- LCD_Write_string("超");
-
- }
- else
- {
- disbuff[1]=S1%1000/100;
- disbuff[2]=S1%1000%100/10;
- disbuff[3]=S1%1000%100%10;
- LCD_set_xy( 0, 5 );
- LCD_Write_number(disbuff[1]);
- LCD_Write_number(disbuff[2]);
- LCD_set_xy( 0, 6 );
- LCD_Write_number(disbuff[3]);
- }
- }
-
- /****************************************************/
- void Conut2(void)
- {
- time2=TH0*256+TL0;
- TH0=0x00;
- TL0=0x00;
- S2=time2*1.87/100; // 11.0592M晶振
- if(flag==1) //超出测量
- {
- flag=0;
- LCD_set_xy( 1, 5 );
- LCD_Write_string("超");
-
- }
- else
- {
- disbuff[1]=S2%1000/100;
- disbuff[2]=S2%1000%100/10;
- disbuff[3]=S2%1000%100%10;
- LCD_set_xy( 1, 5 );
- LCD_Write_number(disbuff[1]);
- LCD_Write_number(disbuff[2]);
- LCD_set_xy( 1, 6 );
- LCD_Write_number(disbuff[3]);
- }
- }
- /****************************************************/
- void Conut4(void)
- {
- time4=TH0*256+TL0;
- TH0=0x00;
- TL0=0x00;
- S4=time4*1.87/100; // 11.0592M晶振
- if(flag==1) //超出测量
- {
- flag=0;
- LCD_set_xy( 2, 5 );
- LCD_Write_string("超");
-
- }
- //else
- //{
- disbuff[1]=S4%1000/100;
- disbuff[2]=S4%1000%100/10;
- disbuff[3]=S4%1000%100%10;
- LCD_set_xy( 2, 5 );
- LCD_Write_number(disbuff[1]);
- LCD_Write_number(disbuff[2]);
- LCD_set_xy( 2, 6 );
- LCD_Write_number(disbuff[3]);
- //}
- }
- /****************************************************/
- void Conut3(void)
- {
- time3=TH0*256+TL0;
- TH0=0x00;
- TL0=0x00;
- S3=time3*1.87/100; // 11.0592M晶振
- if(flag==1) //超出测量
- {
- flag=0;
- LCD_set_xy( 3, 5 );
- LCD_Write_string("超");
-
- }
- else
- {
- disbuff[1]=S3%1000/100;
- disbuff[2]=S3%1000%100/10;
- disbuff[3]=S3%1000%100%10;
- LCD_set_xy( 3, 5 );
- LCD_Write_number(disbuff[1]);
- LCD_Write_number(disbuff[2]);
- LCD_set_xy( 3, 6 );
- LCD_Write_number(disbuff[3]);
- }
- }
- void changkuan()
- {
- }
- /********************************************************/
- void delay20us(void) //误差 -0.016637731481us
- {
- unsigned char a,b;
- for(b=1;b>0;b--)
- for(a=52;a>0;a--);
- //if Keil,require use intrins.h
- }
- void Delay5ms() //@11.0592MHz
- {
- unsigned char i, j;
- i = 54;
- j = 199;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay50ms() //@11.0592MHz
- {
- unsigned char i, j, k;
- i = 3;
- j = 26;
- k = 223;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
-
-
-
- void Delay300ms() //@11.0592MHz
- {
- unsigned char i, j, k;
- i = 13;
- j = 156;
- k = 83;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- /********************************************************/
- void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
- {
- flag=1; //中断溢出标志
- }
- /********************************************************/
- void StartModule1() //超声波触发函数
- { Trig1=0;
- delay20us();
- Trig1=1;
- delay20us(); //发射一个大于10us的脉冲,触发超声波发射
- Trig1=0;
- }
- /******************************************************/
- void StartModule2() //超声波触发函数
- { Trig2=0;
- delay20us();
- Trig2=1;
- delay20us(); //发射一个大于10us的脉冲,触发超声波发射
- Trig2=0;
- }
- /******************************************************/
- void StartModule3() //超声波触发函数
- { Trig3=0;
- delay20us();
- Trig3=1;
- delay20us(); //发射一个大于10us的脉冲,触发超声波发射
- Trig3=0;
- }
- /******************************************************/
- void StartModule4() //超声波触发函数
- { Trig4=0;
- delay20us();
- Trig4=1;
- delay20us(); //发射一个大于10us的脉冲,触发超声波发射
- Trig4=0;
- }
- /********************************************************/
- void main(void)
- {
- TMOD=0x01; //设T0为方式1
- TH0=0x00;
- TL0=0x00;
- TR0=1;
- ET0=1; //允许T0中断
- EA=1; //开启总中断
- Lcd_init(); //设置液晶显示器
- Clr_Scr(); //清屏
- Delaynms(1000);
- Lcd_init(); //
- Lcd_Mark2();
- Delaynms(1000);
- while(1)
- {
- StartModule1(); //发出信号
- Ecno1 =1; //把Ecno脚置高后等待高电平回波信号
- while(!Ecno1); //当Ecno为零时等待
- TR0=1; //开启计数
- while(Ecno1); //当RX为1计数并等待
- TR0=0; //关闭计数
- Conut1();
- delay(200);
- StartModule2(); //发出信号
- Ecno2 =1; //把Ecno脚置高后等待高电平回波信号
- while(!Ecno2); //当Ecno为零时等待
- TR0=1; //开启计数
- while(Ecno2); //当RX为1计数并等待
- TR0=0; //关闭计数
- Conut2();
- delay(250);
- StartModule4(); //发出信号
- Ecno4 =1; //把Ecno脚置高后等待高电平回波信号
- while(!Ecno4); //当Ecno为零时等待
- TR0=1; //开启计数
- while(Ecno4); //当RX为1计数并等待
- TR0=0; //关闭计数
- Conut4();
- delay(300); //延时时间即为液晶屏的刷新周期
- StartModule3(); //发出信号
- Ecno3 =1; //把Ecno脚置高后等待高电平回波信号
- while(!Ecno3); //当Ecno为零时等待
- TR0=1; //开启计数
- while(Ecno3); //当RX为1计数并等待
- TR0=0; //关闭计数
- Conut3();
- delay(350);
-
- }
- }
复制代码
|