////////////////////////////////////////////////////////////////////////////////

//

//     PIC16F877 + HY-SRF05 + LCD03 example

//     Written October 2008 , using HITECH PIC16 compiler

// 

// Note - assumes a 20MHz crystal, which is 5MHz timer clock

// A 1:4 prescaler is used to give a 1.25MHz timer count (0.8uS per tick)

//

//     This code is Freeware - Use it for any purpose you like.

//

///////////////////////////////////////////////////////////////////////////////









#include <pic.h>

#include <stdio.h>

 

__CONFIG(0x3b32);





#define trig RB0

#define echo RB1





void clrscn(void); // prototypes

void cursor(char pos);

void print(char *p);

void setup(void);

unsigned int get_srf04(void);





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





void main(void)

{

unsigned int range;





setup();
// sets up the PIC16F877 I2C port

clrscn();
// clears the LCD03 disply

cursor(2);
// sets cursor to 1st row of LCD03

sprintf(s,"SRF04 Ranger Test");
// text, printed into our buffer

print(s);
// send it to the LCD03





while(1) {
// loop forever

range = get_srf04();
// get range from srf04 (round trip flight time in 0.8uS units)

cursor(24);
// sets cursor to 2nd row of LCD03

sprintf(s,"Range = %dcm  ", range/72);
// convert to cm

print(s);
// send it to the LCD03

cursor(44);
// sets cursor to 3rd row of LCD03

sprintf(s,"Range = %dinch  ", range/185);
// convert to inches

print(s);
// send it to the LCD03





TMR1H = 0;
// 52mS delay - this is so that the SRF04 ranging is not too rapid

TMR1L = 0;
// and the previous pulse has faded away before we start the next one

T1CON = 0x21;
// 1:4 prescale and running

TMR1IF = 0;

while(!TMR1IF);
// wait for delay time

TMR1ON = 0;
// stop timer

}

}





unsigned int get_srf04(void)

{

TMR1H = 0xff;
// prepare timer for 10uS pulse

TMR1L = -14;

T1CON = 0x21;
// 1:4 prescale and running

TMR1IF = 0;


trig = 1;
// start trigger pulse

while(!TMR1IF);
// wait 10uS

trig = 0;
// end trigger pulse

TMR1ON = 0;
// stop timer



TMR1H = 0;
// prepare timer to measure echo pulse

TMR1L = 0;

T1CON = 0x20;
// 1:4 prescale but not running yet

TMR1IF = 0;

while(!echo && !TMR1IF);
// wait for echo pulse to start (go high)

TMR1ON = 1;
// start timer to measure pulse

while(echo && !TMR1IF);
// wait for echo pulse to stop (go low)

TMR1ON = 0;
// stop timer

return (TMR1H<<8)+TMR1L;
// TMR1H:TMR1L contains flight time of the pulse in 0.8uS units

}





void clrscn(void)

{

SEN = 1;
// send start bit

while(SEN);
// and wait for it to clear





SSPIF = 0;

SSPBUF = 0xc6;
// LCD02 I2C address

while(!SSPIF);
// wait for interrupt

SSPIF = 0;
// then clear it.





SSPBUF = 0;
// address of register to write to 

while(!SSPIF);
// 

SSPIF = 0;
//





SSPBUF = 12;
// clear screen 

while(!SSPIF);
// 

SSPIF = 0;
//





SSPBUF = 4;
// cursor off 

while(!SSPIF);
// 

SSPIF = 0;
//

 

PEN = 1;
// send stop bit

while(PEN);
//

}







void cursor(char pos)

{

SEN = 1;
// send start bit

while(SEN);
// and wait for it to clear





SSPIF = 0;

SSPBUF = 0xc6;
// LCD02 I2C address

while(!SSPIF);
// wait for interrupt

SSPIF = 0;
// then clear it.





SSPBUF = 0;
// address of register to write to 

while(!SSPIF);
// 

SSPIF = 0;
//





SSPBUF = 2;
// set cursor 

while(!SSPIF);
// 

SSPIF = 0;
//

SSPBUF = pos;
//  

while(!SSPIF);
// 

SSPIF = 0;
//

 

PEN = 1;
// send stop bit

while(PEN);
//

}







void print(char *p)

{

SEN = 1;
// send start bit

while(SEN);
// and wait for it to clear





SSPIF = 0;

SSPBUF = 0xc6;
// LCD02 I2C address

while(!SSPIF);
// wait for interrupt

SSPIF = 0;
// then clear it.





SSPBUF = 0;
// address of register to write to 

while(!SSPIF);
// 

SSPIF = 0;
//





while(*p) {

SSPBUF = *p++;
// write the data 

while(!SSPIF);
// 

SSPIF = 0;
// 

}





PEN = 1;
// send stop bit

while(PEN);
//

}









void setup(void)

{

unsigned long x;





TRISB = 0xfe;
// RB0 (trig) is output

PORTB = 0xfe;
// and starts low





TRISC = 0xff;

PORTC = 0xff;





SSPSTAT = 0x80;

SSPCON = 0x38;

SSPCON2 = 0x00;

SSPADD = 50;
// SCL = 91khz with 20Mhz Osc





for(x=0; x<300000L; x++);
// wait for LCD03 to initialise

}

超声波模块SRF05的更多相关文章

  1. 关于HC04超声波模块测距的思考(51版)

    之前写过一篇HC04的使用文章,当时是使用stm32来实现的,原文链接. 后来又多次使用51来驱动这个模块,有时候有测距需要,使用了几次,总是感觉我上次那个程序不是很好, 所以这次对它进行了改进.虽然 ...

  2. 树莓派控制HC-SR04超声波模块测距(新手向+C语言向)

    因为作业要求使用c语言代码,这里先附上一段摘自网上的代码 感谢KalaerSun的c语言代码,摘自https://blog.csdn.net/qq_25247589/article/details/6 ...

  3. [TPYBoard - Micropython之会python就能做硬件 8] 学习使用超声波模块制作避障小车

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604   一.实验器材 1.TPYboard V102板  一块 2.电机驱动模块L2 ...

  4. stm32驱动超声波模块

    下面是关于stm32驱动超声波模块的一段代码,有需要的朋友可以复制参考,希望对大家能够有所帮助和启发. #define HCSR04_PORT GPIOB #define HCSR04_CLK RCC ...

  5. 超声波模块HC-SR04简介以及编程

    HC-SR04 一.主要参数1:使用电压:DC-5V2:静态电流:小于2mA3:电平输出:高5V4:电平输出:底0V5:感应角度:不大于15度6:探测距离:2cm-450cm7:高精度 可达0.2cm ...

  6. 【Arduino】超声波模块(HC-SR04)

    还好,这个模块有现成的库能够用: https://github.com/bosgood/arduino-playground/tree/master/lib/HCSR04Ultrasonic 下面仅仅 ...

  7. [自娱自乐] 2、超声波测距模块DIY笔记(二)

    前言 上一节我们已经大致浏览下目前销售的超声波测距模块同时设计了自己的分析电路,这次由于我买的电子元件都到了,所以就动手实验了下!至写该笔记时已经设计出超声波接收模块和超声波发射模块,同时存在超声波发 ...

  8. 基于STM32F103ZET6 HC_SR04超声波测距模块

    这是最后的实验现象,改变不同的角度即可测得距离 板子 PZ6806L 超声波模块 HC_SR04 HC_SR04模块讲解 通过该超声波模块说明书,可明白供电需VCC 5V  还需GND  ECHO(回 ...

  9. loto示波器实践——超声波测距模块

    我们这里用到的超声波测距模块,一般是用于arduino智能小车自动避障的.经常见到的应用是使用单片机或者stm32和这种模块结合进行开发的. 我们使用LOTO示波器可以更直观和快速的看到超声波测量距离 ...

随机推荐

  1. Qt学习之路(24): QPainter(改写paintEvent)

    多些大家对我的支持啊!有朋友也提出,前面的几节有关event的教程缺少例子.因为event比较难做例子,也就没有去写,只是把大概写了一下.今天带来的是新的部分,有关Qt的2D绘图.这部分不像前面的内容 ...

  2. JVM参数说明(转)

    做了这么多年java,自以为算是熟悉,其实还差得远,啥也别说了,还是踏踏实实地学吧.今天总结一下常用的JVM的启动参数. 参数类别 参数项 说明 标准参数(-,所有的JVM实现都必须实现这些参数的功能 ...

  3. JavaScript对滚动栏的操作

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. YT工作日志-0911

    上午 在导师的帮助下,帮我从svn上下载了项目.但是因为项目太大(不算jar包有730M),很多模块不是我工作中涉及的,但是运行的时候会报错,所以导师还帮我找了很多错误.把那些不需要的东西注释了.就这 ...

  5. MDK的优化应用

    MDK的优化应用 http://blog.163.com/zhaojun_xf/blog/static/300505802011291384721/ 使用Keil/MDK这么多年了,一直都没有使用它的 ...

  6. 读取中兴3G告警log告警文件到集合

    1.文件格式 ALARM_ID=102305_404205 EVENT_TIME=-- :: NOTIFICATION_TYPE= MANAGED_OBJECT_INSTANCE=NodeId=,Bs ...

  7. C++ 库研究笔记——通过inline避免hpp 的mutiple definition 错误

    C++用了这么多年,这个却第一次知道,以前没用过hpp 这样: // foo.hpp void foo() { /* body */ } // a.cpp #include "foo.hpp ...

  8. perl 类里的函数调用其他类的函数

    perl 类里的函数调用其他类的函数: package Horse; use base qw(Critter); sub new { my $invocant = shift; my $class = ...

  9. [poj 1904]King's Quest[Tarjan强连通分量]

    题意:(当时没看懂...) N个王子和N个女孩, 每个王子喜欢若干女孩. 给出每个王子喜欢的女孩编号, 再给出一种王子和女孩的完美匹配. 求每个王子分别可以和那些女孩结婚可以满足最终每个王子都能找到一 ...

  10. Swift - 经纬度位置坐标与真实地理位置相互转化

    通过CoreLocation类,得到的定位信息都是以经度和纬度等表示的地理信息,通过CLGeocoder类可以将其反编码成一个地址.反之,也可根据一个地址获取经纬度. 1,通过经纬度获取地址 1 2 ...