单片机学习小组
直播中

康沧鹤

7年用户 171经验值
私信 关注

超声波测距程序下载

#include
#include
#include"1602.h"
#include"delay.h"
***it trig = P2^0;        //        产生脉冲引脚   tx
***it echo = P2^1;     //         回波引脚         rx
unsigned char flag;
unsigned int s,s1,s2,s3,time;




unsigned char code dofly_code[]={'0','1','2','3','4','5','6','7','8','9'};//转换成液晶显示的字符



void Conut(void)
         {
          time=TH0*256+TL0;
          TH0=0;
          TL0=0;
          //s=(time*1.56672)/100;    //晶振11.0592算出来是CM  
          s=(time*1.9)/100;//用的时候根据实际反求的
           s1=dofly_code[s/100];
          s2=dofly_code[(s%100)/10];
          s3=dofly_code[(s%100)%10];
          //S=(time*1.7)/100;     //晶振12M算出来是CM
          if((s>=700)||flag==1) //超出测量范围显示“
          {         
           flag=0;
           LCD_Write_String(1,2,"   S:--- CM  XX ");         
           delay(500);
           LCD_Write_String(1,2,"   S:    CM     ");
           }
          else
          {
           LCD_Write_String(1,2,"   S:    CM  OK ");         
           LCD_Write_Char(6,2,s1) ;   
           LCD_Write_Char(7,2,s2) ;      
              LCD_Write_Char(8,2,s3) ;      
          // LCD_Write_Char(6,2,s1) ;      
          }      
         }




void zd0() interrupt 1                  //T0中断用来计数器溢出,超过测距范围
   {
     flag=1;                                                         //中断溢出标志
   }



     void  StartModule()                          //启动模块
   {
           trig=1;                                             //启动一次模块
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           trig=0;
   }





void main(void)
{


         LCD_Init() ;
         LCD_Write_String(1,1," Csheng bo test ");
         LCD_Write_String(1,2,"   S:    CM  OK ");               
         while(1)
         {
          TMOD=0x01;                   //设T0为方式1,GATE=1;
          TH0=0;
          TL0=0;         
          ET0=1;             //允许T0中断
          EA=1;                           //开启总中断                       
         while(1)
           {
              StartModule();
              while(!echo);                //当Echo为零时为真
              TR0=1;                            //开启计数
              while(echo);                //当Echo为1为真
              TR0=0;                                //关闭计数
          Conut();                        //计算
                  delay(80);                //80MS         
           }
         }
}





这上面是主函数,1602函数和延时函数放在附件里面吧

        俠歌无畏.zip
  

更多回帖

发帖
×
20
完善资料,
赚取积分