历史上的今天
今天是:2024年11月13日(星期三)
2019年11月13日 | 超声波PIC单片机C程序
2019-11-13 来源:51hei
单片机源程序如下:
////////////////////////////////////////////////////////////////////////////////
//
// PIC16F877 + HC-SR04 + 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 #include __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
史海拾趣
|
2、书写Debug版和Release版的程序 ---------------- 程序在开发过程中必然有许多程序员加的调试信息。我见过许多项目组,当程序开发结束时,发动群众删除程序中的调试信息,何必呢?为什么不像VC++那样建立两个版本的目标代码?一个是debug版本的 ...… 查看全部问答> |
|
如下图: 这是BGA布线规则中的图,VIA 分别朝左上、左下、右上、右下方向打,但是为什么在内部打不了过孔啊?还有就是只要修改一个过孔的尺寸,整个的过孔都改了,这怎么回事啊? [ 本帖最后由 静若幽兰 于 2010-5-14 18:13 编辑 ]… 查看全部问答> |
|
[笔记].在Quartus II中使用JTAG模式固化程序到EPCS中的方法.[Quartus II] 范例 流水灯 图1 流水灯范例 实现步骤 步骤1: 在Quartus II中,单击File->Convert Programming Files..。打开编程文件转换程序,如图2所示。 图2 编程文件转换程序界面 在此界面中。在Programming file&nb ...… 查看全部问答> |
|
ZLG/FS文件系统读SD卡数据的OSFileRead函数怎么用 uint32 OSFileRead(void *Buf, uint32 Size, HANDLE Handle); Buf是保存读到的数据的指针,size是要读的字节数,Handle是文件句柄,返回值是实际读到的字节数。 我是这样用的: char *FileNameRead = \"A:\\\\toRead.txt\"; char ReadFileDat ...… 查看全部问答> |
|
115200下,PC端给PDA发送数据,每100ms发送530bytes,有时会有数据丢失。 调用ClearCommError发现输入缓冲区中才60多个bytes,肯定还没满,状态返回CE_OVERRUN,硬件溢出了,天。有没有别的办法,除了改驱动,加流控。 读写我是有单独线程来完成 ...… 查看全部问答> |
|
本帖最后由 dontium 于 2015-1-23 13:33 编辑 能源技术的无限可能 TInergy是一个能源社区,社区中的人们相信技术创新使生活的方方面面更具效率,技术创新更是通向清洁、低价和可再生能源的桥梁。 作为社区的一分子,德州仪器的能源专家将分享 ...… 查看全部问答> |




