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

QQ登錄

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

搜索
查看: 2331|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

基于51的US100串口測(cè)距+12864液晶顯示

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
應(yīng)用US-100超聲波模塊實(shí)現(xiàn) 串口精確定位的超聲波測(cè)距


單片機(jī)源程序如下:
  1. #include"main.h"
  2. #include"LCD.h"
  3. #include"uart.h"

  4. uchar pos;//LCD顯示器文字位置
  5. uchar Glen[2];//兩個(gè)八位距離接受數(shù)據(jù)
  6. uchar distHL[4];//兩個(gè)八位距離最終數(shù)據(jù)
  7. unsigned long dist=0;//十六位距離

  8. uchar code table1[]={"當(dāng)前距離:"};
  9. uchar code table2[]={"毫米"};
  10. uchar code table3[]={"角度:"};
  11. uchar code table4[]={"度"};
  12. uchar code table5[]={"狀態(tài):"};
  13. uchar code table6[]={"安全!!"};
  14. uchar code table7[]={"警報(bào)。"};
  15. uchar code table8[]={"超出測(cè)量范圍!"};


  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毫秒發(fā)送一次數(shù)據(jù)
  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];//實(shí)際距離公式
  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第一行文字設(shè)置
  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. //第四行狀態(tài)顯示
  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.                 //狀態(tài)文字顯示:警報(bào)
  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.                 //狀態(tài)文字顯示:警報(bào)
  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.                 //報(bào)警器設(shè)置                       
  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. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
基于51的US100串口測(cè)距 12864液晶顯示.zip (45.21 KB, 下載次數(shù): 38)




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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