标题:
超声波测距模块的单片机源码
[打印本页]
作者:
youliqiong
时间:
2018-4-15 14:40
标题:
超声波测距模块的单片机源码
超声波测距模块
单片机源程序如下:
/*******************************************************************************
* 日期 : 2015.7.29
* 实 验 名 : 1602 显示 超声波 返回距离值
* 实验说明 : 1.基于22.1184MHz晶振
* 注 意 : 用本程序扫描得出最大距离为 60 厘米 最小为 2 厘米
扫描周期60ms以上 并将 扫描标志位 设置在 u_speed()函数 后面
-----------------------------------------------------------------------------------
* 修改内容 :
*******************************************************************************/
#include <reg52.h>
#include "Ultrasonic.h"
#include "1602.h"
#define LED P0
#define LED_SEL P2
static char flag = 0;
unsigned char code LED_num []={ //共阴
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f} ;
unsigned char code place[]={0x00,0x01,0x02,0x03} ;
void t_init(); //中断初始化
int main()
{
unsigned int re_1 = 0, re_2 = 0;
unsigned char time = 0;
unsigned char index = 0;
unsigned int length = 0;
t_init();
// Lcd1602_Int ();
// writecom(0x80 + 3);
// writedata('0');
// writedata('0');
// writedata('0');
// writedata('0');
// writedata(' ');
// writedata('M');
// writedata('M');
while(1)
{
if(flag == 1) //80ms 为一次超声波扫描周期
{
time++;
flag = 0;
// writecom(0x80 + 3);
// writedata('0' + re_1%10000/1000);
// writedata('0' + re_1%1000/100);
// writedata('0' + re_1%100/10);
// writedata('0' + re_1%10/1);
switch(index)
{
case 0:LED_SEL = place[0] ; LED = LED_num[length%10000/1000];break;
case 1:LED_SEL = place[1] ; LED = LED_num[length%1000/100];break;
case 2:LED_SEL = place[2] ; LED = LED_num[length%100/10];break;
case 3:LED_SEL = place[3] ; LED = LED_num[length%10/1];break;
default : ;
}
if(index<3)
{
index++;
}
else
{
index = 0;
}
}
if(time == 7)
{
time = 0;
TR1 = 0;
//length = u_range(); //扫描标志位要放在返回值后面, u_range()函数 执行有点长 影响测量周期
re_1 = length;
TR1 = 1;
}
}
return 0;
}
void t_init()
{
TMOD = 0X11;
TH1 = 0x0B8; //定时10ms
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
超声波测距.rar
(54.72 KB, 下载次数: 6)
2018-4-15 14:39 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1