找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索

超聲波測距程序就是不能顯示測出的距離,單片機程序沒錯誤,求大佬們教我如何解決

查看數(shù): 1864 | 評論數(shù): 6 | 收藏 0
關(guān)燈 | 提示:支持鍵盤翻頁<-左 右->
    組圖打開中,請稍候......
發(fā)布時間: 2022-4-6 17:22

正文摘要:

程序沒有錯誤 /* 計算公式 我們知道聲速是340m/s 根據(jù)x=vt 因為超聲波發(fā)送出去和回來是測量距離的兩倍,所以假設(shè)距離是L 2L=344xt t我們用定時器測出來 一般都是us 所以就是tx172x10的-6次方=L,L單位為cm ...

回復(fù)

ID:1005855 發(fā)表于 2022-4-8 10:48
#include "lcd.h"

//---------------------------------------------------------------------
//函數(shù):判忙函數(shù)
//功能:判斷液晶是否忙碌
void busy()
{
        uchar sta;
        P0= 0xFF;                //P0初始化
        RS = 0;                        //選擇指令寄存器
        RW = 1;                        //讀模式
        do
        {
                EN = 1;
                _nop_();
                sta = P0;         //讀取狀態(tài)字
                EN = 0;                //當E端由高電平跳變成低電平時,液晶模塊執(zhí)行命令
               
        }
        while (sta & 0x80); //bit7 等于 1 表示液晶正忙,重復(fù)檢測直到其等于 0 為止
}

//---------------------------------------------------------------------
//函數(shù):寫命令函數(shù)
//功能:向液晶寫入一字節(jié)命令(8位命令), cmd-待寫入命令值
void LCD_Write_Com(unsigned char cmd)
{
        busy(); //判斷液晶是否忙碌
        RS = 0; //選擇指令寄存器
        RW = 0; //寫模式
        P0 = cmd;//把數(shù)據(jù)給P0,即寫入指令和地址
        EN = 1;
    _nop_();//短暫延時1us
        _nop_();//短暫延時1us       
        EN = 0;        //當E端由高電平跳變成低電平時,液晶模塊執(zhí)行命令
       
}

//---------------------------------------------------------------------
//函數(shù):寫數(shù)據(jù)函數(shù)
//功能:向液晶寫入一字節(jié)數(shù)據(jù)(8位數(shù)據(jù)), dat-待寫入數(shù)據(jù)值
void LCD_Write_Data(unsigned char dat)
{
        busy(); //判斷液晶是否忙碌
        RS = 1; //選擇數(shù)據(jù)寄存器
        RW = 0; //寫模式
        P0= dat;//把數(shù)據(jù)給P0
        EN = 1;
        _nop_();//短暫延時1us
        _nop_();//短暫延時1us               
        EN = 0;        //當E端由高電平跳變成低電平時,液晶模塊執(zhí)行命令
       
}


//---------------------------------------------------------------------
//函數(shù):初始化液晶函數(shù)
//功能:初始化液晶,并顯示啟動界面
void LCD_Init()
{
        LCD_Write_Com(0x38); //16*2 顯示, 5*7 點陣, 8 位數(shù)據(jù)傳送
        LCD_Write_Com(0x0C); //顯示器開,光標關(guān)閉
        LCD_Write_Com(0x06); //文字不動,地址自動+1
        LCD_Write_Com(0x01); //清屏
       
}


//---------------------------------------------------------------------
/*函數(shù):寫入字符函數(shù)*/
//功能:設(shè)置顯示 DDRAM 起始地址,亦即光標位置, (x,y)-對應(yīng)屏幕上的字符坐標
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{     
    if (y == 0) //由輸入的屏幕坐標計算顯示 DDRAM 的地址
        {     
        LCD_Write_Com(0x80 + x); //第一行字符地址從 0x80 起始   
        }   
    else
        {     
        LCD_Write_Com(0xC0 + x); //第二行字符地址從 0xc0起始   
        }        
    LCD_Write_Data( Data);           //寫數(shù)據(jù)-設(shè)置 DDRAM 地址
}

/*函數(shù):寫入字符串函數(shù)*/
//可參考網(wǎng)上的
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{            
    while (*s) //先取 s 指向的數(shù)據(jù),然后s自加 1,指向下一個待寫字符
    {     
        LCD_Write_Char(x,y,*s);     
        s++;  
        x++;   
    }
}
ID:1005855 發(fā)表于 2022-4-8 10:17
lkc8210 發(fā)表于 2022-4-7 21:14
我又沒有l(wèi)cd.h和delay.h

#ifndef __LCD_H_
#define __LCD_H_

/**********************************
包含頭文件
**********************************/
#include <reg52.h>
#include "intrins.h"

//---重定義關(guān)鍵詞---//
#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint
#define uint unsigned int
#endif

/**********************************
            PIN口定義
**********************************/
sbit EN = P2^7;
sbit RW = P2^5;
sbit RS = P2^6;

/**********************************
            函數(shù)聲明
**********************************/
void busy();//忙檢測函數(shù)

/*LCD1602寫入8位命令子函數(shù)*/
void LCD_Write_Com(unsigned char cmd);

/*LCD1602寫入8位數(shù)據(jù)子函數(shù)*/       
void LCD_Write_Data(unsigned char dat);

/*LCD1602初始化子程序*/               
void LCD_Init(void);

void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data);

void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s);
                                                  

#endif



#ifndef __DELAY_H__
#define __DELAY_H__

void DelayUs2x(unsigned char t);

void DelayMs(unsigned char t);

#endif




ID:161164 發(fā)表于 2022-4-7 21:14
lj2443454061 發(fā)表于 2022-4-7 19:53
還是不能顯示數(shù)據(jù),還要其他的方法嗎

我又沒有l(wèi)cd.h和delay.h
ID:1005855 發(fā)表于 2022-4-7 19:53
lkc8210 發(fā)表于 2022-4-6 23:09
試試在while(1)之前加個DelayMs(1000);

還是不能顯示數(shù)據(jù),還要其他的方法嗎
ID:161164 發(fā)表于 2022-4-6 23:09
試試在while(1)之前加個DelayMs(1000);

評分

參與人數(shù) 1黑幣 +20 收起 理由
admin + 20 回帖助人的獎勵!

查看全部評分

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表