标题: 超声波测距并显示在LCD1602模块上 [打印本页]

作者: 51heidianzi97    时间: 2018-3-18 22:57
标题: 超声波测距并显示在LCD1602模块上
这是51单片机和超声测距模块US-100。显示模块是LCD1602。想学习超声测距的同学,欢迎学习,共同进步。




以下是部分程序:

#include<reg51.h>
#include<intrins.h>
#include"lcd.h"
sbit Trig = P2^1;
sbit Echo = P2^0;

unsigned char PuZh[]=" Pechin Science ";
unsigned char code ASCII[15] =    {'0','1','2','3','4','5','6','7','8','9','.','-','M'};
static unsigned char DisNum = 0; //显示用指针      
       unsigned int  time=0;
    unsigned long S=0;
    bit      flag =0;
    unsigned char disbuff[4]    ={ 0,0,0,0,};
/*******************************************************************************
* 函 数 名         : main
* 函数功能     : 主函数
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void Conut(void)
{
  time=TH0*256+TL0;
  TH0=0;
  TL0=0;

  S=(time*1.7)/100;     //算出来是CM
  if((S>=700)||flag==1) //超出测量范围显示“-”
  {  
   flag=0;
  
   DisplayOneChar(0, 1, ASCII[11]);
   DisplayOneChar(1, 1, ASCII[10]); //显示点
   DisplayOneChar(2, 1, ASCII[11]);
   DisplayOneChar(3, 1, ASCII[11]);
   DisplayOneChar(4, 1, ASCII[12]); //显示M
  }
  else
  {
   disbuff[0]=S%1000/100;
   disbuff[1]=S%1000%100/10;
   disbuff[2]=S%1000%10 %10;
   DisplayOneChar(0, 1, ASCII[disbuff[0]]);
   DisplayOneChar(1, 1, ASCII[10]); //显示点
   DisplayOneChar(2, 1, ASCII[disbuff[1]]);
   DisplayOneChar(3, 1, ASCII[disbuff[2]]);
   DisplayOneChar(4, 1, ASCII[12]); //显示M
  }
}
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 delayms(unsigned int ms)
{
unsigned char i=100,j;
for(;ms;ms--)
{
  while(--i)
  {
   j=10;
   while(--j);
  }
}
}
void main(void)
{
  TMOD=0x01;     //设T0为方式1,GATE=1;
  TH0=0;
  TL0=0;         
  ET0=1;             //允许T0中断
  EA=1;      //开启总中断
InitLcd1602();
LcdShowStr(0,0,PuZh);
while(1)

程序 - 副本.zip

25.98 KB, 下载次数: 7, 下载积分: 黑币 -5


作者: 陈Cc    时间: 2018-3-19 10:46
这么好的帖子,必须顶起!刚好在做着实验




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1