找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3988|回復(fù): 1
收起左側(cè)

單片機(jī)超聲波測(cè)距程序原理圖

[復(fù)制鏈接]
ID:987813 發(fā)表于 2021-12-4 09:37 | 顯示全部樓層 |閱讀模式
·       1.本設(shè)計(jì)采用STC89C51/52(與AT89S51/52、AT89C51/52通用,可任選)單片機(jī)作為主控制器
·       2.數(shù)碼管顯示測(cè)量的距離,74hc573和三極管驅(qū)動(dòng)數(shù)碼管
·       3.HC-SR04超聲波模塊測(cè)距,測(cè)量范圍0.02m~5.5m以上
·       4.本設(shè)計(jì)可用作汽車(chē)的倒車(chē)?yán)走_(dá),報(bào)警距離可以用過(guò)按鍵設(shè)定和更改
·       5.附加功能:報(bào)警頻率可以隨著距離變化,距離越小頻率越快。(使用說(shuō)明:
本設(shè)計(jì)共3個(gè)按鍵,一個(gè)設(shè)置鍵,一個(gè)加,一個(gè)減鍵,只有按下設(shè)置鍵的時(shí)候才可以加減。)

51hei.png

單片機(jī)源程序如下:
  1. #include <reg52.H>//器件配置文件
  2. #include <intrins.h>
  3. #include "eeprom52.h"
  4. //傳感器接口
  5. sbit RX  = P2^3;
  6. sbit TX  = P2^2;
  7. //按鍵聲明
  8. sbit S1  = P1^4;
  9. sbit S2  = P1^5;
  10. sbit S3  = P1^6;

  11. sbit DIAN=P0^5;                        //小數(shù)點(diǎn)引腳
  12. //蜂鳴器
  13. sbit Feng= P2^0;

  14. //變量聲明
  15. unsigned int  time=0;
  16. unsigned int  timer=0;
  17. unsigned char posit=0;
  18. unsigned long S=0;
  19. unsigned long BJS;//報(bào)警距離
  20. char num=0;
  21. //模式 0正常模式 1調(diào)整
  22. char Mode=0;
  23. bit  flag=0;

  24. unsigned char const discode[] ={0x5F,0x44,0x9D,0xD5,0xC6,0xD3,0xDB,0x47,0xDF,0xD7,0x80};        //數(shù)碼管顯示碼0123456789-和不顯示

  25. unsigned char disbuff[4]           ={0,0,0,0};                 //數(shù)組用于存放距離信息
  26. unsigned char disbuff_BJ[4]        ={0,0,0,0};//報(bào)警信息

  27. void Display(void);

  28. sbit W0=P2^4;
  29. sbit W1=P2^5;
  30. sbit W2=P2^6;
  31. sbit W3=P2^7;                                                                  //數(shù)碼管的位選引腳

  32. /******************把數(shù)據(jù)保存到單片機(jī)內(nèi)部eeprom中******************/
  33. void write_eeprom()
  34. {
  35.         SectorErase(0x2000);                          //先清除
  36.         byte_write(0x2000, BJS);                  //寫(xiě)數(shù)據(jù)
  37.         byte_write(0x2060, a_a);        
  38. }

  39. /******************把數(shù)據(jù)從單片機(jī)內(nèi)部eeprom中讀出來(lái)*****************/
  40. void read_eeprom()
  41. {
  42.         BJS   = byte_read(0x2000);
  43.         a_a      = byte_read(0x2060);
  44. }

  45. /**************開(kāi)機(jī)自檢eeprom初始化*****************/
  46. void init_eeprom()
  47. {
  48.         read_eeprom();                //先讀
  49.         if(a_a != 1)                //新的單片機(jī)初始單片機(jī)內(nèi)問(wèn)eeprom
  50.         {
  51.                 BJS   = 25;
  52.                 a_a = 1;
  53.                 write_eeprom();           //保存數(shù)據(jù)
  54.         }        
  55. }
  56. //延時(shí)1ms(不精確)
  57. void delay(unsigned int x)
  58. {
  59.     unsigned int i,j;
  60.     for(i=0;i<x;i++)
  61.         for(j=0;j<121;j++);
  62. }

  63. //按鍵掃描
  64. void Key_()
  65. {
  66.         unsigned char DIS_I;
  67.         //+
  68.         if(S1==0&&Mode==1)
  69.         {
  70.                 for(DIS_I=0;DIS_I<100;DIS_I++)
  71.                 Display();           //延時(shí)去抖
  72.                 if(S1==0)
  73.                 {
  74.                         Feng=0;
  75.                         for(DIS_I=0;DIS_I<200;DIS_I++)
  76.                         Display();           //延時(shí)
  77.                         Feng=1;
  78.                         BJS++;         //報(bào)警值加
  79.                         if(BJS>=151) //最大151
  80.                         {
  81.                                 BJS=0;
  82.                         }
  83.                         disbuff_BJ[0]=BJS%1000/100;
  84.                         disbuff_BJ[1]=BJS%1000%100/10;
  85.                         disbuff_BJ[2]=BJS%1000%10 %10;
  86.                         while(S1==0) Display();
  87.                 }
  88.         }
  89.         //-
  90.         else if(S2==0&&Mode==1)
  91.         {
  92.                 for(DIS_I=0;DIS_I<100;DIS_I++)
  93.                 Display();           //延時(shí)去抖
  94.                 if(S2==0)
  95.                 {
  96.                         Feng=0;
  97.                         for(DIS_I=0;DIS_I<200;DIS_I++)
  98.                         Display();           //延時(shí)
  99.                         Feng=1;
  100.                         BJS--;         //報(bào)警值減
  101.                         if(BJS<=1)         //最小1
  102.                         {
  103.                                 BJS=150;
  104.                         }
  105.                         disbuff_BJ[0]=BJS%1000/100;
  106.                         disbuff_BJ[1]=BJS%1000%100/10;
  107.                         disbuff_BJ[2]=BJS%1000%10 %10;
  108.                         while(S2==0) Display();
  109.                 }
  110.         }
  111.         //功能
  112.         else if(S3==0)                //設(shè)置鍵
  113.         {
  114.                 for(DIS_I=0;DIS_I<100;DIS_I++)
  115.                 Display();           //延時(shí)去抖
  116.                 if(S3==0)
  117.                 {
  118.                         Feng=0;
  119.                         for(DIS_I=0;DIS_I<200;DIS_I++)
  120.                         Display();           //延時(shí)
  121.                         Feng=1;
  122.                         Mode=!Mode;                //模式加
  123.                         num=0;
  124.                         Feng=1;
  125.                         disbuff_BJ[0]=BJS%1000/100;
  126.                         disbuff_BJ[1]=BJS%1000%100/10;
  127.                         disbuff_BJ[2]=BJS%1000%10 %10;
  128.                         while(S3==0) Display();
  129.                         if(Mode==0)
  130.                         write_eeprom();                           //保存數(shù)據(jù)
  131.                 }
  132.         }
  133. }
  134. /**********************************************************************************************************/
  135. //掃描數(shù)碼管
  136. void Display(void)                                 
  137. {
  138.         //正常顯示
  139.         if(Mode==0)                           //正常測(cè)距模式
  140.         {
  141.                 num++;                                                           //計(jì)數(shù)切換不同顯示數(shù)碼管
  142.                 if(num==1)                                                   //第一個(gè)數(shù)碼管
  143.                 {
  144.                         W3=1;                                                   //關(guān)閉第二位
  145.                         W0=1;                                                   //關(guān)閉第二位
  146.                         P0=~discode[disbuff[0]];           //顯示整數(shù)位,因?yàn)槎未a數(shù)組為共陰數(shù)碼管碼表,所以在使用時(shí)要取反
  147.                         DIAN=0;                                                   //點(diǎn)亮小數(shù)點(diǎn)
  148.                         W1=0;                                                   //打開(kāi)位
  149.                 }
  150.                 else if(num==2)                                           //第二位
  151.                 {
  152.                         W1=1;                                                   //關(guān)閉第一位
  153.                         P0=~discode[disbuff[1]];           //顯示小數(shù)點(diǎn)后一位
  154.                         W2=0;                                                   //打開(kāi)第二位
  155.                 }
  156.                 else if(num>=3)                                           //第三個(gè)位置
  157.                 {
  158.                         W2=1;                                                   //關(guān)閉第二位
  159.                         P0=~discode[disbuff[2]];           //顯示小數(shù)點(diǎn)后二位
  160.                         W3=0;                                                   //打開(kāi)第三位
  161.                         num=0;                                                   //清零
  162.                 }
  163.         }
  164.         //報(bào)警顯示
  165.         else
  166.         {
  167.                 num++;
  168.                 if(num==1)
  169.                 {
  170.                         W3=1;
  171.                         P0=~0xCE;                  //11001110 顯示字母H
  172.                         W0=0;
  173.                 }
  174.                 else if(num==2)
  175.                 {
  176.                         W0=1;
  177.                         P0=~discode[disbuff_BJ[0]];                   //顯示報(bào)警值
  178.                         DIAN=0;
  179.                         W1=0;        
  180.                 }
  181.                 else if(num==3)
  182.                 {
  183.                         W1=1;
  184.                         P0=~discode[disbuff_BJ[1]];
  185.                         W2=0;
  186.                 }
  187.                 else if(num>=4)
  188.                 {
  189.                         W2=1;
  190.                         P0=~discode[disbuff_BJ[2]];
  191.                         W3=0;
  192.                         num=0;
  193.                 }
  194.         }
  195. }
  196. /**********************************************************************************************************/
  197. //計(jì)算
  198. void Conut(void)
  199. {
  200.         time=TH0*256+TL0;          //讀出T0的計(jì)時(shí)數(shù)值
  201.         TH0=0;
  202.         TL0=0;                                  //清空計(jì)時(shí)器
  203.         S=(time*1.7)/100;     //算出來(lái)是CM
  204.         //聲音的速度是340m/s,時(shí)間的單位是us,計(jì)算到秒需要將時(shí)間數(shù)據(jù)/1000000,
  205.         //長(zhǎng)度=速度*時(shí)間,340*time/1000000,長(zhǎng)度數(shù)據(jù)單位是m轉(zhuǎn)換成cm需要乘以100得到340*time/10000,
  206.         //小數(shù)點(diǎn)都向左移兩位得到3.4*time/100,因?yàn)槌暡ㄊ峭盗耍栽俪?,得到距離數(shù)據(jù)(time*1.7)/100
  207.         if(Mode==0)                          //非設(shè)置狀態(tài)時(shí)
  208.         {
  209.                 if((S>=700)||flag==1) //超出測(cè)量范圍顯示“-”
  210.                 {        
  211.                         Feng=0;                    //蜂鳴器報(bào)警
  212.                         flag=0;
  213.                         disbuff[0]=10;           //“-”
  214.                         disbuff[1]=10;           //“-”
  215.                         disbuff[2]=10;           //“-”
  216.                 }
  217.                 else
  218.                 {
  219.                         //距離小于報(bào)警距
  220.                         if(S<=BJS)
  221.                         {
  222.                                 Feng=0;        //報(bào)警
  223.                         }
  224.                         else  //大于
  225.                         {
  226.                                 Feng=1;                //關(guān)閉報(bào)警        
  227.                         }
  228.                         disbuff[0]=S%1000/100;                 //將距離數(shù)據(jù)拆成單個(gè)位賦值
  229.                         disbuff[1]=S%1000%100/10;
  230.                         disbuff[2]=S%1000%10 %10;
  231.                 }
  232.         }
  233.         else
  234.         {
  235.                         Feng=1;
  236.                         disbuff_BJ[0]=BJS%1000/100;
  237.                         disbuff_BJ[1]=BJS%1000%100/10;
  238.                         disbuff_BJ[2]=BJS%1000%10 %10;
  239.         }
  240. }
  241. /**********************************************************************************************************/
  242. //定時(shí)器0
  243. void zd0() interrupt 1                  //T0中斷用來(lái)計(jì)數(shù)器溢出,超過(guò)測(cè)距范圍
  244. {
  245. ……………………

  246. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

Keil程序下載:
程序原理圖.7z (257.67 KB, 下載次數(shù): 52)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:616633 發(fā)表于 2022-11-6 18:22 | 顯示全部樓層
哥我想問(wèn)一下驗(yàn)證過(guò)了嗎?

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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