超声波模块SRF05
纯干货:
超声波代码:
//
// 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 RB1void 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 printvoid main(void)
{unsigned int range;setup(); // sets up the PIC16F877 I2C portclrscn(); // clears the LCD03 displycursor(2); // sets cursor to 1st row of LCD03sprintf(s,"SRF04 Ranger Test");// text, printed into our bufferprint(s); // send it to the LCD03while(1) { // loop foreverrange = get_srf04();// get range from srf04 (round trip flight time in 0.8uS units)cursor(24); // sets cursor to 2nd row of LCD03sprintf(s,"Range = %dcm ", range/72);// convert to cmprint(s); // send it to the LCD03 cursor(44); // sets cursor to 3rd row of LCD03sprintf(s,"Range = %dinch ", range/185);// convert to inchesprint(s); // send it to the LCD03 TMR1H = 0; // 52mS delay - this is so that the SRF04 ranging is not too rapidTMR1L = 0; // and the previous pulse has faded away before we start the next oneT1CON = 0x21; // 1:4 prescale and runningTMR1IF = 0;while(!TMR1IF);// wait for delay timeTMR1ON = 0; // stop timer }
}unsigned int get_srf04(void)
{TMR1H = 0xff; // prepare timer for 10uS pulseTMR1L = -14;T1CON = 0x21; // 1:4 prescale and runningTMR1IF = 0; trig = 1; // start trigger pulsewhile(!TMR1IF);// wait 10uStrig = 0; // end trigger pulseTMR1ON = 0; // stop timerTMR1H = 0; // prepare timer to measure echo pulseTMR1L = 0;T1CON = 0x20; // 1:4 prescale but not running yetTMR1IF = 0;while(!echo && !TMR1IF);// wait for echo pulse to start (go high)TMR1ON = 1; // start timer to measure pulsewhile(echo && !TMR1IF);// wait for echo pulse to stop (go low)TMR1ON = 0; // stop timerreturn (TMR1H<<8)+TMR1L;// TMR1H:TMR1L contains flight time of the pulse in 0.8uS units
}void clrscn(void)
{SEN = 1; // send start bitwhile(SEN); // and wait for it to clearSSPIF = 0;SSPBUF = 0xc6;// LCD02 I2C addresswhile(!SSPIF);// wait for interruptSSPIF = 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 bitwhile(PEN); //
}void cursor(char pos)
{SEN = 1; // send start bitwhile(SEN); // and wait for it to clearSSPIF = 0;SSPBUF = 0xc6;// LCD02 I2C addresswhile(!SSPIF);// wait for interruptSSPIF = 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 bitwhile(PEN); //
}void print(char *p)
{SEN = 1; // send start bitwhile(SEN); // and wait for it to clearSSPIF = 0;SSPBUF = 0xc6;// LCD02 I2C addresswhile(!SSPIF);// wait for interruptSSPIF = 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 bitwhile(PEN); //
}void setup(void)
{unsigned long x;TRISB = 0xfe; // RB0 (trig) is outputPORTB = 0xfe; // and starts lowTRISC = 0xff;PORTC = 0xff;SSPSTAT = 0x80;SSPCON = 0x38;SSPCON2 = 0x00;SSPADD = 50; // SCL = 91khz with 20Mhz Oscfor(x=0; x<300000L; x++);// wait for LCD03 to initialise
}
超声波模块SRF05相关推荐
- arduino 多个超声波模块HC-SR04 Newping.h库的使用——摆脱万恶的阻塞等待
目录 前言(一点废话) 问题的发现 Newping.h库 简介 安装 例程1:定时器示例TimerExample 例程2:超声波模块的读取NewPingExample 例程3:中断读取超声波NewPi ...
- 快速上手Arduino -- 打印超声波模块测距信息到OLED屏幕上
文章目录 快速上手Arduino -- 打印超声波模块测距信息到OLED屏幕上 实现效果: 模块说明: 引脚接线方法: 程序源码 附录 快速上手Arduino – 打印超声波模块测距信息到OLED屏幕 ...
- 树莓派超声波模块测距
参考:树莓派超声波模块测距及C语言demo 作者:一只青木呀 发布时间: 2020-07-22 16:54:16 网址:https://blog.csdn.net/weixin_45309916/ar ...
- STM32超声波模块测距串口输出/通用定时器中断并输出PWM控制舵机/系统定时器延时
参考:stm32 超声波模块 原理 实现测距 +舵机使用 作者:点灯小哥 发布时间: 2021-03-10 19:37:16 网址:https://blog.csdn.net/weixin_46016 ...
- stm32超声波测距代码_超声波模块另类用法,悬浮,你也能做到
今天,给大家介绍一个 「超酷的DIY声波悬浮」研究项目. 虽然超声波悬浮,不能像磁悬浮那样悬浮比较重的物品.不过能把小泡沫球.水滴或者蚂蚁这样的小物体悬浮起来也是很有意思的. 制作这样一个简易的超声波 ...
- 【蓝桥杯单片机】超声波模块(测距原理,驱动方式)
实验开发板为CT107D蓝桥官方板,编译环境为MDK5 超声波探头测距原理: 发射头发射超声波,遇到障碍物后返回,接收探头接受到返回信号. 两个动作产生的时间差可以是我们根据声音在空气中传播的速度来( ...
- STM32超声波模块实验(定时器实现)
实验材料准备 1个HC-SR04超声波模块 1台STM32F103ZET6开发板 若干杜邦线 HC-SR04模块简介 STM32定时器设置 实验中接收发送信号都用定时器,方便管理 上表在<STM ...
- 超声波模块测距 Arduino代码
1.本篇是记录关于使用超声波模块测距的记录,本次使用的超声波测距模块(HC-SR04)探测角度为<5° 宽电压工作在3.3~5V之间,最大测距为7m. 在Arduino中定义引脚,距离变量等. ...
- 用8266学习单片机-13-HC-SR04超声波模块测距示例-Ultrasonic-US-015
HC-SR04原理 重点 5v供电.Tri引脚需要至少10us的高脉冲去触发.Echo返回的是超声波发射到返回的时间 距离怎么算看上面第三张图,US-015原理相似,注意测量范围最小值和最大值 接线 ...
- HC-SR04超声波模块的使用(stm32f103c8t6/HAL)(超详细)
系列文章目录(STM32常用外设/HAL库版) 一.HC-SR04超声波模块的使用(本篇) 二.OLED的HAL库代码介绍及使用 三.编码电机以及双电机驱动 本文主要介绍超声波模块HC-SR04的两种 ...
最新文章
- python从零开始进阶_从零开始学Python - 第020课:函数使用进阶
- tcpdump软件使用
- 谈谈怎样提高炼丹手速
- win10c语言安装未响应,重新安装win10以后,系统总会死机,不响应任何操作,严重影响正常使用。...
- java基础—文件的切割与合并
- web界面左边菜单设计_前端产品经理难点|“取消按钮”逻辑设计
- 这些标点符号你真的会输入吗
- 具体数学-第4课(多重求和方法)
- 拓端tecdat|R语言可视化探索BRFSS数据并逻辑回归Logistic回归预测中风
- 服务器机柜设备信息卡,信息机房标识标准V.doc
- win10安装apache环境
- ACM-ICPC 2016 沈阳赛区现场赛 I. The Elder HDU 5956(斜率DP)
- LaTeX中的拼写及语法检查
- hp batterie batterie charing port
- windows 1903用linux使用,Win10 build 1903中的WSL Update允许您从Windows访问Linux文件
- 何登成大神对Innodb加锁的分析
- Tita 绩效宝:绩效面谈的8个流程
- 敏涵控股董事长刘敏——不一样的巾帼慈善企业家
- 文件IO实现图片的加密操作
- python random库画多彩蟒蛇,Python Turtle库绘制蟒蛇
热门文章
- LeetCode算法解析之“箭爆气球问题”
- (转载)C# Dictionary
- 关于win10开始菜单点击无反应解决方案
- 如何将flv转换成mp3格式
- sql2000 sp3、sql2000 sp4升级补丁下载和安装须知
- 阿里云域名动态解析dns,ddns php定时更新
- PAT 甲级1021 Deepest Root
- runaction 旋转_使用cc.tween(缓动系统)代替runAction
- 蘑菇街2016校园招聘之编程题解析-技术类
- win7管理员取得所有权