超声波测距源码
超声波测距源码.docx
(12.91 KB, 下载次数: 4)
- /**************************************************************************/
- *********采用IO口TRIG触发测距,给至少10us的高电平信号; (2)模块自动发送8个40khz
- 的方波,自动检测是否有信号返回; (3)有信号返回,通过IO口ECHO输出一个高电平,
- 高电平持续的时间就是超声波从发射到返回的时间。*****************************/
- #include"reg52.h"
- #include"intrins.h"
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table[]={'0','1','2','3','4','5','6','7','8','9'};// 显示段码值0123456789
- sbit lcden=P3^4; //定义LCD使能端
- sbit lcdrs=P3^5; //定义LCD读写端
- sbit dula=P2^6; //数码管段先端
- sbit wela=P2^7; //数码管位先端
- /***********************************************/
- /**********超声波检测引脚和变量定义*******************************/
- sbit Trig=P2^4;// 定义HC-SR04发送端
- sbit Echo=P2^5;//定义HC-SR04接收端
- uint gewei='0'; //测量距离的个位
- uint shiwei='0'; // 测量距离的十位
- uint baiwei='0'; //测量距离的百位
- uint s,time,flag;
- /***********************************************/
- /**********延时MS函数*******************************/
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /***********************************************/
- /**********延时uS函数*******************************/
- void delay_uS(uint z)
- {
- while(z--);
- }
- /***********************************************/
- /**********写命令*******************************/
- void write_com(uchar com)
- {
- lcdrs=0; //写命令信号
- P0=com; //写命令字
- delay(5); //延时
- lcden=1; //开使能端
- delay(5);
- lcden=0;
- }
- /***********************************************/
- /**********写数据*******************************/
- void write_data(uchar date)
- {
- lcdrs=1; //写数据信号
- P0=date; //写数据
- delay(5); //延时
- lcden=1; //开使能端
- delay(5);
- lcden=0;
- }
- /**********************************************/
- /***********************************************/
- /**********LCD1602初始化函数*******************************/
- void init()
- {
- dula=0;
- wela=0;
- lcden=0;
- write_com(0x38); //设置16X2显示,5X7点阵显示
- write_com(0x0c); //开显示,不显光标
- write_com(0x06); //显示一个字符后,地址指针加1且光标加1
- write_com(0x01); //清屏
- write_com(0x80+0x01); //从第一行0X10地址位开始显示
- }
- /***********************************************/
- /**********计算距离函数*******************************/
- void count(void)
- {
- time=TH0*256+TL0; //检测所需时间
- TH0=0;
- TL0=0;
- s=time/58;// 计算距离,算出来的单位是CM
- gewei=table[s%1000];
- shiwei=table[s%1000/100];
- baiwei=table[s%1000%100%10];
- }
- /***********************************************/
- /**********T0中断函数*******************************/
- void Time0() interrupt 1 //T0中断用作计数器溢出,超出测距范围
- {
- flag=1; //中断溢出标志
- }
- /***********************************************/
- /**********启动模块函数*******************************/
- void startmodle(void) //启动检测模块
- {
- Trig=1; //启动一次检测模块
- delay_uS(20);
- Trig=0; //停止向检测模块Trig端发送高电平
- }
- /***********************************************/
- /**********中断初始化函数*******************************/
- void InterruptInit(void) //中断初始化
- {
- TMOD=0x01; //高T0工作方式为方式1,GATE=1
- TH0=0;
- TL0=0;
- ET0=1; //允许T0中断
- EA=1; // 开启总中断
- }
- /***********************************************/
- /**********显示函数*******************************/
- void Display(void)
- {
- write_com(0x80+0x02);
- write_data(baiwei);
- write_com(0x80+0x03);
- write_data('.');
- write_com(0x80+0x04);
- write_data(shiwei);
- write_com(0x80+0x05);
- write_data(gewei);
- write_com(0x80+0x06);
- write_data('M');
- }
- /***********************************************/
- /**********主函数*******************************/
- void main(void)
- {
- init(); //初始化LCD1602
- while(1)
- {
- InterruptInit(); //中断初始化
- startmodle(); // 启动检测模块
- while(!Echo); //当接收为零时等待
- TR0=1; //开启计数
- while(Echo); //当接收为1计数并等待
- TR0=0; //关闭计数
- count(); //计算距离
- Display(); // 显示距离,以M为单位
- delay(500); //延时80MS
- }
- }
复制代码
|