久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1510|回復: 0
打印 上一主題 下一主題
收起左側

想實現(xiàn)52單片機控制兩個超聲波測距然后數(shù)碼管顯示距離(2~9)不過數(shù)碼管只有一個亮

[復制鏈接]
跳轉到指定樓層
樓主
ID:356571 發(fā)表于 2022-3-14 20:00 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
#include <REGX52.H>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint  unsigned int
sbit SH_CP1 = P0^1;
sbit DS = P0^0;
sbit ST_CP1 = P0^2;
int distL,distR;
unsigned char code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
sbit TrigL    = P2^4;                // 超聲波模塊的Trig管腳
sbit EchoL    = P2^5;                // 超聲波模塊的Echo管腳
sbit TrigR    = P2^6;                // 超聲波模塊的Trig管腳
sbit EchoR    = P2^7;                // 超聲波模塊的Echo管腳
//============MAIN===============================        
void Delay(unsigned int xms)  //延時
{
        unsigned char i, j;
        while(xms--)
        {
                i = 2;
                j = 199;
                do
                {
                        while (--j);
                } while (--i);
        }
}
void send_data_74hc595(uint data_74hc595)                    //傳送數(shù)據(jù)給74hc595
{
        uchar a;
        for(a = 0; a < 8; ++a)
        {
                DS = (data_74hc595 << a) & 0x80;                    //從高位開始傳輸
                SH_CP1 = 0;

                _nop_();
                _nop_();
                SH_CP1 = 1;

        }
}

void show_74hc595()                                         //數(shù)據(jù)顯示
{
        ST_CP1 = 0;

        _nop_();
        _nop_();
        ST_CP1 = 1;

}
void Nixie()
{
           switch(distR)
                 {
                         case 1:send_data_74hc595(table[1]);show_74hc595();break;
                         case 2:send_data_74hc595(table[2]);show_74hc595();break;
                         case 3:send_data_74hc595(table[3]);show_74hc595();break;
                         case 4:send_data_74hc595(table[4]);show_74hc595();break;
                         case 5:send_data_74hc595(table[5]);show_74hc595();break;
                         case 6:send_data_74hc595(table[6]);show_74hc595();break;
                         case 7:send_data_74hc595(table[7]);show_74hc595();break;
                         case 8:send_data_74hc595(table[8]);show_74hc595();break;
                         case 9:send_data_74hc595(table[9]);show_74hc595();break;
                 
                 }
                 
                  switch(distL)
                 {
                         case 1:send_data_74hc595(table[1]);show_74hc595();break;
                         case 2:send_data_74hc595(table[2]);show_74hc595();break;
                         case 3:send_data_74hc595(table[3]);show_74hc595();break;
                         case 4:send_data_74hc595(table[4]);show_74hc595();break;
                         case 5:send_data_74hc595(table[5]);show_74hc595();break;
                         case 6:send_data_74hc595(table[6]);show_74hc595();break;
                         case 7:send_data_74hc595(table[7]);show_74hc595();break;
                         case 8:send_data_74hc595(table[8]);show_74hc595();break;
                         case 9:send_data_74hc595(table[9]);show_74hc595();break;
                 
                 
                 }        }        
//模塊程序
uint GetDistanceL(void)
{
        uint s;     
  TH0 = 0;
        TL0 = 0;
  TrigL=1;                                         //800MS  啟動一次模塊
        Delay(0.001);
  TrigL=0;

  while(!EchoL);                //當RX為零時等待
         TR0=1;                            //開啟計數(shù)
  while(EchoL);                        //當RX為1計數(shù)并等待
         TR0=0;                                //關閉計數(shù)
  //timeL=TH0*256+TL0;
         
        // timeL *= 12/11.0592;
  //s=(timeL*1.7)/100;     //算出來是CM
        s=((TH0*256+TL0)*0.034)/2;
        return s;
}

uint GetDistanceR(void)
{        
        uint s;
        TH0 = 0;
        TL0 = 0;
  TrigR=1;                                         //800MS  啟動一次模塊
         Delay(0.001);
  TrigR=0;

  while(!EchoR);                //當RX為零時等待
         TR0=1;                            //開啟計數(shù)
  while(EchoR);                        //當RX為1計數(shù)并等待
         TR0=0;                                //關閉計數(shù)
        s=((TH0*256+TL0)*0.034)/2;
        return s;

}



void main()
{
                                TMOD = 0x02;
                                ET0=0;
                                EA =1;                                                // 選擇定時器0,并且確定是工作方式1(為了超聲波模塊測量距離計時用的)
        TrigL=0;                                                                // 初始化觸發(fā)引腳為低電平
        TrigR=0;

        while(1)
        {         
        //===========Ultrasonic measurement===========
                //left
                 distL = GetDistanceL();
                //right
                 distR = GetDistanceR();                          
                        Nixie();
               
        }//end of while               

}//end of main  

51hei截圖20220314195243.png (65.76 KB, 下載次數(shù): 43)

51hei截圖20220314195243.png
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 人人九九 | 一区二区高清不卡 | 亚洲一区免费 | 亚洲欧美久久 | 国产成人精品在线 | 成人日b视频 | 5060网一级毛片 | 日本羞羞影院 | 欧美日韩综合 | av三级 | 美女一级a毛片免费观看97 | 亚洲国产中文字幕 | 欧美日韩在线免费观看 | 国产a一区二区 | www.9191 | 国产中文字幕在线观看 | 欧美一级在线观看 | 操久久 | 国产精品久久久久久久久久久久久 | www.五月天婷婷.com | 偷派自拍 | 国产视频中文字幕 | 日本国产高清 | 一级特黄网站 | 日韩精品 | 久久亚洲欧美日韩精品专区 | 中文字幕国产精品视频 | 欧美性猛交一区二区三区精品 | 97免费在线视频 | 亚洲人a | 中文字幕二区三区 | 亚洲免费人成在线视频观看 | 日韩www视频 | 国产女人与拘做视频免费 | 亚洲成人精品久久久 | 久久久久久国产精品 | 一级黄色片美国 | 成人在线观看中文字幕 | 精品国产91亚洲一区二区三区www | 精品自拍视频 | 二区视频|