找回密码
 注册

QQ登录

只需一步,快速开始

搜索

基于51单片机超声波雷达测距(源程序+Proteus仿真)

[复制链接]
eng 发表于 2021-7-12 16:12:40 | 显示全部楼层 |阅读模式
仿真原理图如下(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币)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-27 07:59

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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