找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1995|回复: 0
打印 上一主题 下一主题
收起左侧

基于51的US100串口测距+12864液晶显示

[复制链接]
跳转到指定楼层
楼主
应用US-100超声波模块实现 串口精确定位的超声波测距


单片机源程序如下:
  1. #include"main.h"
  2. #include"LCD.h"
  3. #include"uart.h"

  4. uchar pos;//LCD显示器文字位置
  5. uchar Glen[2];//两个八位距离接受数据
  6. uchar distHL[4];//两个八位距离最终数据
  7. unsigned long dist=0;//十六位距离

  8. uchar code table1[]={"当前距离:"};
  9. uchar code table2[]={"毫米"};
  10. uchar code table3[]={"角度:"};
  11. uchar code table4[]={"度"};
  12. uchar code table5[]={"状态:"};
  13. uchar code table6[]={"安全!!"};
  14. uchar code table7[]={"警报!!"};
  15. uchar code table8[]={"超出测量范围!"};


  16. void dispose_dist(unsigned long dist);

  17. void main()
  18. {
  19.         LCD_init();
  20.         UART_init();
  21.         display();       
  22. }

  23. void delay(uchar z)
  24. {
  25.         uchar x,y;
  26.         for(x=110;x>0;x--)
  27.                 for(y=z;y>0;y--);       
  28. }

  29. void time0() interrupt 1
  30. {
  31.         uchar num=0;
  32.         TH0=(65536-2500)/256;
  33.         TL0=(65536-2500)%256;
  34.         num++;
  35.         if(num==100)//每25毫秒发送一次数据
  36.         {
  37.                 num=0;
  38.                 dist_UART();
  39.         }       
  40. }

  41. void UART_recv() interrupt 4
  42. {
  43.         uchar k=0;
  44.         ES=0;
  45.         if(RI)
  46.         {
  47.                 RI=0;
  48.                 Glen[k]=SBUF;
  49.                 k++;
  50.                 if(k==2)
  51.                         k=0;
  52.         }
  53.         ES=1;
  54.         dist=Glen[0]*256+Glen[1];//实际距离公式
  55. }

  56. void display()
  57. {
  58.         uchar i=0;
  59.         uchar j=0;
  60.         uchar k=0;
  61.         uchar n=0;
  62.         uchar m=0;
  63.         uchar n1=0;
  64.         uchar n2=0;
  65.         uchar n3=0;
  66. //LCD第一行文字设置
  67.         pos=LCD_pos(0,0);
  68.         LCDwrite_cmd(pos);
  69.         while(table1[i]!='\0')
  70.         {
  71.                 LCDwrite_dat(table1[i]);
  72.                 i++;       
  73.         }
  74. //第二行距离单位显示
  75.         pos=LCD_pos(5,1);
  76.         LCDwrite_cmd(pos);
  77.         while(table2[m]!='\0')
  78.         {
  79.                 LCDwrite_dat(table2[m]);
  80.                 m++;       
  81.         }
  82. //第四行状态显示
  83.         pos=LCD_pos(0,3);
  84.         LCDwrite_cmd(pos);
  85.         while(table5[n3]!='\0')
  86.         {
  87.                 LCDwrite_dat(table5[n3]);
  88.                 n3++;       
  89.         }
  90. //LCD第二行距离显示
  91.         if(dist>4500)
  92.         {
  93.                 pos=LCD_pos(0,1);
  94.                 LCDwrite_cmd(pos);
  95.                 while(table8[j]!='\0')
  96.                 {
  97.                         LCDwrite_dat(table8[j]);
  98.                         j++;       
  99.                 }
  100.                 //状态文字显示:警报
  101.                 pos=LCD_pos(3,3);
  102.                 LCDwrite_cmd(pos);
  103.                 while(table7[n1]!='\0')
  104.                 {
  105.                         LCDwrite_dat(table7[n1]);
  106.                         n1++;       
  107.                 }       
  108.         }
  109.         else if(dist<=1000)
  110.         {
  111.                 pos=LCD_pos(0,1);
  112.                 LCDwrite_cmd(pos);
  113.                 dispose_dist(dist);
  114.                 for(k=0;k<4;k++)
  115.                 {
  116.                         LCDwrite_dat(distHL[k]);
  117.                         k++;       
  118.                 }
  119.                 //状态文字显示:警报
  120.                 pos=LCD_pos(3,3);
  121.                 LCDwrite_cmd(pos);
  122.                 while(table7[n1]!='\0')
  123.                 {
  124.                         LCDwrite_dat(table7[n1]);
  125.                         n1++;       
  126.                 }       
  127.                 //报警器设置                       
  128.         }
  129.         else
  130.         {
  131.                 pos=LCD_pos(0,1);
  132.                 LCDwrite_cmd(pos);
  133.                 dispose_dist(dist);
  134.                 for(k=0;k<4;k++)
  135.                 {
  136.                         LCDwrite_dat(distHL[k]);
  137.                         k++;       
  138. ……………………

  139. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
基于51的US100串口测距 12864液晶显示.zip (45.21 KB, 下载次数: 38)




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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