国产免费AV|泡泡玛特欧洲总部将设在伦敦|中文天堂网www新版资源在线|一本久道综合在线中文|国精产品一二三产区的使用方法|香蕉鱼在线观看|www.27eee

 找回密碼
 注冊
搜索

基于51單片機超聲波雷達測距(源程序+Proteus仿真)

[復制鏈接]
樓主
eng 發表于 2021-7-12 16:12:40 | 只看該作者 |只看大圖 |倒序瀏覽 |閱讀模式
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
基于51單片機超聲波雷達測距(源程序+Proteus仿真) 3.gif
部分源碼:完整源碼見附件
  1. #include <reg52.H>//器件配置文件
  2. #include <intrins.h>
  3. #include "eeprom52.h"
  4. //傳感器接口
  5. sbit RX  = P3^2;
  6. sbit TX  = P3^3;
  7. //按鍵聲明
  8. sbit S1  = P1^4;
  9. sbit S2  = P1^5;
  10. sbit S3  = P1^6;
  11. //蜂鳴器
  12. sbit Feng= P2^0;

  13. sbit W1=P1^0;
  14. sbit W2=P1^1;
  15. sbit W3=P1^2;
  16. sbit W4=P1^3;
  17. //變量聲明
  18. unsigned int  time=0;
  19. unsigned int  timer=0;
  20. unsigned char posit=0;
  21. unsigned long S=0;
  22. unsigned long BJS;//報警距離
  23. //模式 0正常模式 1調整
  24. char Mode=0;
  25. bit  flag=0;
  26. bit flag_KEY=0;
  27. unsigned char const discode[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0xff/*-*/};        //數碼管顯示碼0123456789-和不顯示
  28. //unsigned char const positon[4]={0xfd,0xfb,0xf7,0xfe};        //位選
  29. unsigned char disbuff[4]           ={0,0,0,0};                 //數組用于存放距離信息
  30. unsigned char disbuff_BJ[4]        ={0,0,0,0};//報警信息
  31. void Display();

  32. /******************把數據保存到單片機內部eeprom中******************/
  33. void write_eeprom()
  34. {
  35.         SectorErase(0x2000);
  36.         byte_write(0x2000, BJS);
  37.         byte_write(0x2060, a_a);        
  38. }

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

  45. /**************開機自檢eeprom初始化*****************/
  46. void init_eeprom()
  47. {
  48.         read_eeprom();                //先讀
  49.         if(a_a != 1)                //新的單片機初始單片機內問eeprom
  50.         {
  51.                 BJS   = 50;
  52.                 a_a = 1;
  53.                 write_eeprom();           //保存數據
  54.         }        
  55. }

  56. //延時20ms(不精確)
  57. void delay(void)
  58. {
  59.     unsigned char a,b,c;
  60.     for(c=2;c>0;c--)
  61.         for(b=38;b>0;b--)
  62.             for(a=60;a>0;a--);
  63. }

  64. //按鍵掃描
  65. void Key_()
  66. {
  67.         if(flag_KEY==0)
  68.         {
  69.                 if(Mode!=0)
  70.                 {
  71.                         //+
  72.                         if(S1==0)
  73.                         {
  74.                                 delay();           //延時去抖
  75.                                 if(S1==0)
  76.                                 {
  77.                                         BJS++;         //報警值加
  78.                                         flag_KEY=1;
  79.                                         if(BJS>=151) //最大151
  80.                                         {
  81.                                                 BJS=0;
  82.                                         }
  83.                                         write_eeprom();//保存數據
  84. //                                        while(S1==0)
  85. //                                        Display();
  86.                                 }
  87.                                 
  88.                         }
  89.                         //-
  90.                         if(S2==0)
  91.                         {
  92.                                 delay();
  93.                                 if(S2==0)
  94.                                 {
  95.                                         BJS--;         //報警值減
  96.                                         flag_KEY=1;
  97.                                         if(BJS<=1)         //最小1
  98.                                         {
  99.                                                 BJS=150;
  100.                                         }
  101.                                         write_eeprom();//保存數據   
復制代碼
仿真文件和完整源碼下載: 超聲波測距.zip (160.74 KB, 售價: 3 E幣)
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規則

手機版|小黑屋|ELEOK |網站地圖

GMT+8, 2026-5-26 02:11

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回復 返回頂部 返回列表