找回密码
 立即注册

QQ登录

只需一步,快速开始

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

超声波PIC单片机C程序

[复制链接]
跳转到指定楼层
楼主
ID:229453 发表于 2017-8-30 20:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
超声波PIC单片机C程序
单片机源程序如下:
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //     PIC16F877 + HC-SR04 + LCD03 example
  4. //     Written October 2008 , using HITECH PIC16 compiler
  5. //
  6. //                 Note - assumes a 20MHz crystal, which is 5MHz timer clock
  7. //                 A 1:4 prescaler is used to give a 1.25MHz timer count (0.8uS per tick)
  8. //
  9. //     This code is Freeware - Use it for any purpose you like.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////


  12. #include <pic.h>
  13. #include <stdio.h>
  14.          
  15. __CONFIG(0x3b32);               

  16. #define trig        RB0
  17. #define echo        RB1

  18. void clrscn(void);                                                        // prototypes
  19. void cursor(char pos);
  20. void print(char *p);
  21. void setup(void);
  22. unsigned int get_srf04(void);

  23. char s[21];                                                                                // buffer used to hold text to print

  24. void main(void)
  25. {
  26. unsigned int range;

  27.         setup();                                                                                // sets up the PIC16F877 I2C port
  28.         clrscn();                                                                        // clears the LCD03 disply
  29.         cursor(2);                                                                        // sets cursor to 1st row of LCD03
  30.         sprintf(s,"SRF04 Ranger Test");                // text, printed into our buffer
  31.         print(s);                                                                        // send it to the LCD03

  32.         while(1) {                                                                        // loop forever
  33.                 range = get_srf04();                                        // get range from srf04 (round trip flight time in 0.8uS units)
  34.                 cursor(24);                                                                // sets cursor to 2nd row of LCD03
  35.                 sprintf(s,"Range = %dcm  ", range/72);        // convert to cm
  36.                 print(s);                                                                // send it to the LCD03       
  37.                 cursor(44);                                                                // sets cursor to 3rd row of LCD03
  38.                 sprintf(s,"Range = %dinch  ", range/185);        // convert to inches
  39.                 print(s);                                                                // send it to the LCD03       

  40.                 TMR1H = 0;                                                                // 52mS delay - this is so that the SRF04 ranging is not too rapid
  41.                 TMR1L = 0;                                                                // and the previous pulse has faded away before we start the next one
  42.                 T1CON = 0x21;                                                        // 1:4 prescale and running
  43.                 TMR1IF = 0;
  44.                 while(!TMR1IF);                                                // wait for delay time
  45.                 TMR1ON = 0;                                                                // stop timer       
  46.         }
  47. }

  48. unsigned int get_srf04(void)
  49. {
  50.         TMR1H = 0xff;                                                // prepare timer for 10uS pulse
  51.         TMR1L = -14;
  52.         T1CON = 0x21;                                                // 1:4 prescale and running
  53.         TMR1IF = 0;       
  54.         trig = 1;                                                        // start trigger pulse
  55.         while(!TMR1IF);                                        // wait 10uS
  56.         trig = 0;                                                        // end trigger pulse
  57.         TMR1ON = 0;                                                        // stop timer
  58.                
  59.         TMR1H = 0;                                                        // prepare timer to measure echo pulse
  60.         TMR1L = 0;
  61.         T1CON = 0x20;                                                // 1:4 prescale but not running yet
  62.         TMR1IF = 0;
  63.         while(!echo && !TMR1IF);                // wait for echo pulse to start (go high)
  64.         TMR1ON = 1;                                                        // start timer to measure pulse
  65.         while(echo && !TMR1IF);                        // wait for echo pulse to stop (go low)
  66.         TMR1ON = 0;                                                        // stop timer
  67.         return (TMR1H<<8)+TMR1L;                // TMR1H:TMR1L contains flight time of the pulse in 0.8uS units
  68. }

  69. void clrscn(void)
  70. {
  71.         SEN = 1;                                                                // send start bit
  72.         while(SEN);                                                        // and wait for it to clear

  73.         SSPIF = 0;
  74.         SSPBUF = 0xc6;                                                // LCD02 I2C address
  75.         while(!SSPIF);                                                // wait for interrupt
  76.         SSPIF = 0;                                                        // then clear it.

  77.         SSPBUF = 0;                                                        // address of register to write to
  78.         while(!SSPIF);                                                //
  79.         SSPIF = 0;                                                        //

  80.         SSPBUF = 12;                                                // clear screen
  81.         while(!SSPIF);                                                //
  82.         SSPIF = 0;                                                        //

  83.         SSPBUF = 4;                                                        // cursor off
  84.         while(!SSPIF);                                                //
  85.         SSPIF = 0;                                                        //
  86.          
  87.         PEN = 1;                                                                // send stop bit
  88.         while(PEN);                                                        //
  89. }

  90.                
  91. void cursor(char pos)
  92. {
  93.         SEN = 1;                                                                // send start bit
  94.         while(SEN);                                                        // and wait for it to clear

  95.         SSPIF = 0;
  96.         SSPBUF = 0xc6;                                                // LCD02 I2C address
  97.         while(!SSPIF);                                                // wait for interrupt
  98.         SSPIF = 0;                                                        // then clear it.

  99.         SSPBUF = 0;                                                        // address of register to write to
  100.         while(!SSPIF);                                                //
  101.         SSPIF = 0;                                                        //

  102.         SSPBUF = 2;                                                        // set cursor
  103.         while(!SSPIF);                                                //
  104.         SSPIF = 0;                                                        //
  105.         SSPBUF = pos;                                                //  
  106.         while(!SSPIF);                                                //
  107.         SSPIF = 0;                                                        //
  108.          
  109.         PEN = 1;                                                                // send stop bit
  110.         while(PEN);                                                        //
  111. }

  112.                
  113. void print(char *p)
  114. {
  115.         SEN = 1;                                                                // send start bit
  116.         while(SEN);                                                        // and wait for it to clear

  117.         SSPIF = 0;
  118.         SSPBUF = 0xc6;                                // LCD02 I2C address
  119.         while(!SSPIF);                                // wait for interrupt
  120.         SSPIF = 0;                                        // then clear it.

  121.         SSPBUF = 0;                                        // address of register to write to
  122.         while(!SSPIF);                                //
  123.         SSPIF = 0;                                        //

  124.         while(*p) {
  125.                 SSPBUF = *p++;                        // write the data
  126.                 while(!SSPIF);                        //
  127.                 SSPIF = 0;                                //
  128.         }

  129.         PEN = 1;                                        // send stop bit
  130.         while(PEN);                                        //
  131. }

  132. ……………………

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


超声波PIC单片机C程序.rar (1.63 KB, 下载次数: 19)


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

使用道具 举报

沙发
ID:209855 发表于 2017-11-22 14:01 | 只看该作者
超声波传感器是用的什么型号的?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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