您的位置 首页 芯闻

51单片机驱动超声波测距模块C51程序

includereg52h>defineucharunsignedchardefineuintunsignedintdefineulongunsignedlong*************

#include

#define  uchar unsigned char#define  uint  unsigned int#define  ulong unsigned long//******************* 函数声明************************** void init_Total(); //总初始化 void init_T0();    //初始化定时器T0 void init_T1();    //初始化定时器T1 void init_inter0();//初始化外部中止1 void send_T(); void delay(uint z);//延时一段时间 void delay_300us();//延时300us void delay_100us();//延时100us //******************************************************** sbit  lcdrs=P1^7;sbit  lcdrw=P3^1;sbit  lcden=P1^5;//1602液晶操控端sbit  send=P1^0;//sbit BEEP=P2^5;sbit wei=P2^6;sbit duan=P2^7;volatile uchar Count_TH ,Count_TL;//别离读计数器T1的高位TH1,低位TL1 uchar t0,flag;uint time;uchar code table1[]=" distance       ";uchar code table2[]="                ";//初始化显现void write_com(uchar com)//1602写指令 函数{lcdrs=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;        }void write_data(uchar datb)//1602写数据 函数{lcdrs=1;P0=datb;delay(1);lcden=1;delay(1);lcden=0;        }void distance(uchar addr,uint datb)     {uchar bai,shi,ge ;bai=datb/100;shi=datb%100/10;ge=datb%10; write_com(0x80+0x40+addr);write_data(0x30+bai);   write_data(0x30+shi);   write_data(0x30+ge);}/************************************************************************************** * 名        称 :void init_Total() * 功        能 :总初始化 * 入 口 参  数 :NULL * 全 局 变  量 :NULL   * 返   回   值 :NULL **************************************************************************************/ void init_Total() { init_T0();    //初始化定时器T0为工作方法2 init_T1();   //初始化定时器T1为工作方法1 init_inter0();//初始化外部中止1 EA=1;   //开总中止 } /************************************************************************************** * 名        称 :void init_T0() * 功        能 :初始化定时器T0为工作方法2 * 入 口 参  数 :NULL * 全 局 变  量 :NULL   * 返   回   值 :NULL **************************************************************************************/void init_T0() { TMOD=0X12; TH0=0XE7; TL0=0XE7; EA=0; ET0=1; TR0=1; } /************************************************************************************** * 名        称 :void init_T1() * 功        能 :初始化定时器T1为工作方法1 * 入 口 参  数 :NULL * 全 局 变  量 :NULL   * 返   回   值 :NULL **************************************************************************************/void init_T1() { TMOD=0X12; TH1=0; TL1=0; EA=0; ET1=1; TR1=1; } /************************************************************************************** * 名        称 :void init_inter1() * 功        能 :初始化外部中止1为低电平触发方法 * 入 口 参  数 :NULL * 全 局 变  量 :NULL   * 返   回   值 :NULL **************************************************************************************/void init_inter0() { IT0=0; //低电平触发 EA=0; EX0=0;      //关外部中止1   } /************************************************************************************** * 名        称 :void inter_T0() interrupt 1 * 功        能 :定时器T0中止函数发生40KHZ的方波 * 入 口 参  数 :NULL * 全 局 变  量 :NULL   * 返   回   值 :NULL **************************************************************************************/void inter_T0() interrupt 1 { send=~send; } /************************************************************************************** * 名        称 :void inter_T1() interrupt 3 * 功        能 :定时器T1中止函数 * 入 口 参  数 :NULL * 全 局 变  量 :NULL   * 返   回   值 :NULL **************************************************************************************/void inter_T1() interrupt 3 { TR1=0; EX0=0;  //关外部中止0TH1=0; TL1=0; flag=2; } /************************************************************************************** * 名        称 :void inter1() interrupt 2 * 功        能 :外部中止1函数 * 入 口 参  数 :NULL * 全 局 变  量 :Count_TH,Count_TL   * 返   回   值 :NULL **************************************************************************************/void inter0() interrupt 0{ TR1=0; EX0=0; flag=1; } /************************************************************************************** * 名        称 :void send_T()  * 功        能 :发送10个超声波脉冲 * 入 口 参  数 :NULL * 全 局 变  量 :Count * 返   回   值 :NULL **************************************************************************************/void send_T()     { delay_100us();//发送100us的方波 TR0=0;//关定时器T0 } /************************************************************************************** * 名        称 :void delay(uint z) * 功        能 :延时一段时间 * 入 口 参  数 :z * 全 局 变  量 :NULL * 返   回   值 :NULL **************************************************************************************/void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=1000;y>0;y--); } /************************************************************************************** * 名        称 :void delay_300us() * 功        能 :延时300us * 入 口 参  数 :NULL * 全 局 变  量 :NULL * 返   回   值 :NULL **************************************************************************************/void delay_300us() { uint x; for(x=75;x>0;x--); } /************************************************************************************** * 名        称 :void delay_100us() * 功        能 :延时100us * 入 口 参  数 :NULL * 全 局 变  量 :NULL * 返   回   值 :NULL **************************************************************************************/void delay_100us() { uint x; for(x=24;x>0;x--); } //*************************************************************************************** void main()  {  uint t,s; init_Total();//总初始化  while(1) {   TR1=1;//发动定时器1 TR0=1;//发动定时器0  send_T();     //发送100us超声波脉冲 delay_300us();//延时300us越过盲区 EX1=1;        //开外部中止1 while(!flag); //等候回波或定时器T1溢出 if(flag==1)   //回波 { Count_TH=TH1;//读计数器T1的高位TH1 Count_TL=TL1;//读计数器T1的低位TL1 t=Count_TH*256+Count_TL; s=(33140*t)/400000; distance(4,s);//液晶1602显现间隔 }          /*    else   //定时器T1溢出 { Count_TH=0XFE; Count_TL=0X7F; // P2=Count_TH; P2=Count_TL; } */delay(150); flag=0;//标志位清0 TH1=0; //T1清0从头计时 TL1=0;  } } 

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/xinwen/318518.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部