51单片机超声波测距(数码管显示)

    科技2022-07-12  144

    51单片机超声波测距(数码管显示)

    程序

    /* 超声波模块的TRIG管脚接在单片机的P20口,ECHO管脚接在单片机的P21口 其他的电源管脚接在单片机 开发板上电源口即可。 */ #include"reg51.h" #include <intrins.h> sbit RX=P2^1; sbit TX=P2^0; unsigned int time=0; unsigned int timer=0; unsigned char posit=0; unsigned long S=0; bit flag =0; //--定义使用的IO--// #define GPIO_DIG P0 sbit LSA=P2^2; sbit LSB=P2^3; sbit LSC=P2^4; //--定义全局变量--// unsigned char code DIG_CODE[17]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码 unsigned char disbuff[4] ={ 0,0,0,0,}; /******************************************************************************* * 函 数 名 : DigDisplay * 函数功能 : 使用数码管显示 * 输 入 : 无 * 输 出 : 无 *******************************************************************************/ void DigDisplay() { unsigned char i; unsigned int j; for(i=0;i<8;i++) { switch(i) //位选,选择点亮的数码管, { case(0): LSA=0;LSB=0;LSC=0; break;//显示第0位 case(1): LSA=1;LSB=0;LSC=0; break;//显示第1位 case(2): LSA=0;LSB=1;LSC=0; break;//显示第2位 case(3): LSA=1;LSB=1;LSC=0; break;//显示第3位 case(4): LSA=0;LSB=0;LSC=1; break;//显示第4位 case(5): LSA=1;LSB=0;LSC=1; break;//显示第5位 case(6): LSA=0;LSB=1;LSC=1; break;//显示第6位 case(7): LSA=1;LSB=1;LSC=1; break;//显示第7位 } GPIO_DIG=disbuff[i];//发送段码 j=10; //扫描间隔时间设定 while(j--); GPIO_DIG=0x00;//消隐 } } /********************************************************/ void Conut(void) { time=TH0*256+TL0; TH0=0; TL0=0; S= (long)(time*0.17); //算出来是CM if((S>=4000)||flag==1) //超出测量范围显示“ERR0” { flag=0; disbuff[0]=0x3f; //“-” disbuff[1]=0x50; //“-” disbuff[2]=0x50; //“-” disbuff[3]=0x79; //“-” } else { disbuff[3]=DIG_CODE[S%10000/1000]; disbuff[2]=DIG_CODE[S%1000/100]; disbuff[1]=DIG_CODE[S%100/10]; disbuff[0]=DIG_CODE[S%10/1]; } } /********************************************************/ void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围 { flag=1; //中断溢出标志 } /********************************************************/ void zd3() interrupt 3 //T1中断用来扫描数码管和计800MS启动模块 { TH1=0xf8; TL1=0x30; DigDisplay(); timer++; if(timer>=100) { timer=0; TX=1; //800MS 启动一次模块 _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); TX=0; } } /*********************************************************/ void main( void ) { TMOD=0x11; //设T0为方式1,GATE=1; TH0=0; TL0=0; TH1=0xf8; //2MS定时 TL1=0x30; ET0=1; //允许T0中断 ET1=1; //允许T1中断 TR1=1; //开启定时器 EA=1; //开启总中断 while(1) { while(!RX); //当RX为零时等待 TR0=1; //开启计数 while(RX); //当RX为1计数并等待 TR0=0; //关闭计数 Conut(); //计算 } }
    Processed: 0.011, SQL: 8