找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2975|回复: 3
收起左侧

超声波1602不能实时显示距离

[复制链接]
ID:140358 发表于 2017-6-9 15:01 来自手机 | 显示全部楼层 |阅读模式
用以下程序做的超声波测距1602不能实时显示距离,显示的距离永远是第一次次测量的距离,求解
#include <AT89x51.H>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define  RX  P2_6
#define  TX  P2_7
#define LCM_RW  P0_2
#define LCM_RS  P0_1
#define LCM_E   P0_3
#define LCM_Data  P1
#define Busy    0x80
unsigned char code mcustudio[] ={"  DONGMUFORYOU  "};
unsigned char code Cls[] =      {"                "};
unsigned char code ASCII[15] =    {'0','1','2','3','4','5','6','7','8','9','.','-','M'};
static unsigned char DisNum = 0;     
uint  time=0;
unsigned long S=0;
bit   flag =0;
uchar disbuff[4]={ 0,0,0,0,};
/*********************5ms延时************/
void delay5Ms(void)
{
uint TempCyc = 5552;
while(TempCyc--);
}
/***********400ms延时******************/
void delay400Ms(void)
{
uchar TempCycA = 5;
uint TempCycB;
while(TempCycA--)
   {
    TempCycB=7269;
    while(TempCycB--);
   };
}
/*********************延迟****************/
void delayms(uint ms)
{
unsigned char i=100,j;
for(;ms;ms--)
{
   while(--i)
   {
    j=10;
    while(--j);
   }
}
}
/**************读状态**********************************/
uchar readStatusLCM(void)
{
LCM_Data = 0xFF;
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
while(LCM_Data & Busy); //检测忙信号
return(LCM_Data);
}  
/*****************读数据***************************************/
/*uchar readDataLCM(void)
{
LCM_RS = 1;
LCM_RW = 1;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
return(LCM_Data);
}  */
/******************写数据*********************************/
void writeDataLCM(uchar WDLCM)
{
readStatusLCM(); //检测忙
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 0; //若晶振速度太高可以在这后加小的延时
LCM_E = 0; //延时
LCM_E = 1;
}
/*****************写指令*********************************/
void writeCommandLCM(unsigned char WCLCM,BuysC) //BuysC为0时忽略忙检测
{
if (BuysC)
   readStatusLCM(); //根据需要检测忙
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
}
/*************初始化*************************************/
void LCMInit(void) //LCM初始化
{
writeCommandLCM(0x38,0); //显示模式设置,不检测忙信号
delay5Ms();
writeCommandLCM(0x38,1); //显示模式设置,开始要求每次检测忙信号
writeCommandLCM(0x08,1); //关闭显示
writeCommandLCM(0x01,1); //显示清屏
writeCommandLCM(0x06,1); // 显示光标移动设置
writeCommandLCM(0x0c,1); // 显示关及光标设置
}
//按指定位置显示一个字符
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y)
{
    X |= 0x40; //当要显示第二行时地址码+0x40;
}
X |= 0x80; //算出指令码
writeCommandLCM(X, 1); //发命令字
writeDataLCM(DData); //发数据
}
/**************显示字符*************************************/
//按指定位置显示一串字符
void displayListChar(uchar X, uchar Y, uchar code *DData)
{
uchar ListLength;
    ListLength = 0;
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
while (DData[ListLength]>0x19) //若到达字串尾则退出
   {
    if (X <= 0xF) //X坐标应小于0xF
     {
      DisplayOneChar(X, Y, DData[ListLength]); //显示单个字符
      ListLength++;
      X++;
     }
   }
}
/************计数*****************************/
void count(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(4, 1, ASCII[disbuff[0]]);
     DisplayOneChar(5, 1, ASCII[10]); //显示点
     DisplayOneChar(6, 1, ASCII[disbuff[1]]);
     DisplayOneChar(7, 1, ASCII[disbuff[2]]);
     DisplayOneChar(8, 1, ASCII[12]); //显示M
   }
}
/*************************中断**************************/
void timer0() interrupt 1    //T0中断用来计数器溢出,超过测距范围
{
     flag=1;        //中断溢出标志
}
/******************启动模块*********************************/
void  startModule()            //启动模块
{
    uchar i;
    TX=1;                        //启动一次模块
    for(i=0;i<20;i++)     //延迟 以便于进行传送数据
    {
     _nop_();
    }
    TX=0;
}
/*****************主函数***********************************/
void main()
{
LCMInit(); //LCM初始化
delay5Ms(); //延时片刻(可不要)
displayListChar(0, 0, mcustudio);
displayListChar(0, 1, Cls);
TMOD=0x01;     //设T0为方式1,GATE=1;
TH0=0;
TL0=0;         
ET0=1;             //允许T0中断
EA=1;      //开启总中断   
while(1)
{
       startModule();
    while(!RX);  //当RX为零时等待
    TR0=1;       //开启计数
       while(RX);   //当RX为1计数并等待
    TR0=0;    //关闭计数
       count();   //计算
    delayms(80);  //80MS
}
大神们求解!急急急
回复

使用道具 举报

ID:164602 发表于 2017-6-10 08:37 | 显示全部楼层
程序太长,没时间看啊。
不过,我做过这个题目,分享给你。不是仿真,是实物做的。
超声波传感器是HC-SR04。

实物图

实物图

1602电路

1602电路

1602显示超声波测距.rar

30.78 KB, 下载次数: 19

完整工程

回复

使用道具 举报

ID:140358 发表于 2017-6-10 11:19 来自手机 | 显示全部楼层
好的,谢啦!
回复

使用道具 举报

ID:715369 发表于 2020-4-23 21:25 | 显示全部楼层
有具体的连线图吗,1602需不需要连接其他线
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表