标题: 51单片超声波测距程序 [打印本页]

作者: yangyyf1975    时间: 2021-4-4 20:17
标题: 51单片超声波测距程序
超声波测距 + 串口发送距离
#include<reg51.h>  
#include<intrins.h>  
  
#define uint unsigned int  
#define uchar unsigned char  
sbit trig=P1^7;  
sbit echo=P3^2;  
float tA,dis;  
  
uint distance,timeh,timel;  
void delay(uint z)  
{  
    uint x,y;  
    for(x=z;x>0;x--)  
       for(y=1100;y>0;y--);  
}  
void delay20us()  
{  
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();   
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();   
}  
  
void initT1_SCI()  
{  
   SM0 = 0; //  
   SM1 = 1; //10位异步收发,波特率可变  
  
   TMOD = 0x21;  
   TH1 = 0xfd;            
   TL1 = 0xfd;     
   TR1 = 1;            
}  
  
void Trig()  
{  
    trig = 0;  
    echo = 0;  
    TH0 = 0;  
    TL0 = 0;   
  
    trig = 1;  
    delay20us();  
    trig = 0;  
      
    while(echo == 0);                     
    EX0 = 1;                    //外部中断0允许  
    ET0 = 1;                    //定时器0中断允许  
    TR0 = 1;                    //定时器0运行控制位  
    delay(60);   
}  
  
void putChar(uchar c)  
{  
    SBUF = c;  
    while( !TI ); TI = 0;  
}  
  
void putStr(uchar str[], uint n)  
{  
    while( n-- )  
    {  
        putChar( str[n] );  
    }  
}  
  
int main()  
{  
    initT1_SCI();  
    EA = 1;  
      
    while(1)  
    {  
        Trig();  
    }  
    return 0;  
}  
   
void T0_time() interrupt 1  
{  
   TH0 = 0;  
   TL0 = 0;  
}  
  
void ex0()interrupt 0  
{  
   uchar num[2];  
   timeh = TH0;  
   timel = TL0;  
   tA = (timeh * 256 + timel) * 1.09;  
   dis = tA * 0.17;  
   distance = (int)dis;  
  
   num[1] = distance / 256;  num[0] = distance % 256;  
   putStr(num, 2);  
   TH0 = 0;  
   TL0 = 0;  
   TR0 = 0;  
   EX0 = 0; //关闭外部中断0  
   ET0 = 0; //关闭定时器1中断  
}     
Correct
#include<reg51.h>
#include<intrins.h>
#include "6x8.h"
#include "LQ12864.h"
#define uint unsigned int
#define uchar unsigned char
sbit trig=P2^0;
sbit echo=P3^2;
uint succeed;
float tA,dis;
uint distance,timeh,timel;
uint flagCG = 0;
uint flag600 = 0, flag50 = 1;
void delay(uint z)
{
    uint x,y;
        for(x=z;x>0;x--)
           for(y=110;y>0;y--);
}
void delay20us()
{
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
void initT1_SCI()
{
   TH0 = 0;
   TL0 = 0;
   TR0 = 0;
   SM0 = 0;        //
   SM1 = 1;        //10位异步收发,波特率可变
   TMOD = 0x21;
   TH1 = 0xfd;                  
   TL1 = 0xfd;   
   TR1 = 1;                       
}
void putChar(uchar c)
{
        SBUF = c;
        while( !TI ); TI = 0;
}
void putStr(uchar str[], uint n)
{
        while( n-- )
        {
                putChar( str[n] );
        }
}
void display()
{
   unsigned char SPEED[4] = {0,0,0,1};
   SPEED[2]=distance/100;                 ready(6,0,SPEED[2]);
   SPEED[1]=(distance%100)/10;         ready(12,0,SPEED[1]);
   SPEED[0]=distance%10;                ready(18,0,SPEED[0]);
   putStr(SPEED, 3);
}
void Trig()
{
                trig = 0;
            echo = 0;
                succeed = 0;
                TH0 = 0;
                TL0 = 0;
      
                trig = 1;
                delay20us();
                trig = 0;
               
                while(echo == 0);                                       
                EX0 = 1;                                        //外部中断0允许
                ET0 = 1;                                        //定时器0中断允许
                TR0 = 1;                                        //定时器0运行控制位
                delay(60);
}
void main()
{   
    LCD_Init();
        initT1_SCI();
        EA = 1;
   
      
        while(1)
    {      
                Trig();
                if(succeed)
                {
                        LCD_Cler_6x8(0,0,4);                  
                        display();
                }   
                delay(100);                       
        }
}
void T0_time() interrupt 1
{
   TH0 = 0;
   TL0 = 0;
   succeed = 0;
}
void ex0()interrupt 0
{
   timeh = TH0;
   timel = TL0;
   tA = (timeh * 256 + timel) * 1.09;
   dis = tA * 0.17;
   distance = (int)dis;
   succeed = 1;
   TH0 = 0;
   TL0 = 0;
   TR0 = 0;
   EX0 = 0; //关闭外部中断0
   ET0 = 0; //关闭定时器1中断
}      






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1