HT-SR04 Ultra-sonic range detect

// lcd.h
#ifndef _LCD_H_
#define _LCD_H_

#include <reg52.h>
#include <intrins.h>

void DelayUs2x(unsigned char t);
void DelayMs(unsigned char t);
bit LCD_Check_Busy(void);
void LCD_Write_Com(unsigned char com);
void LCD_Write_Data(unsigned char Data);
void LCD_Clear(void);
void LCD_Set_Cursor(unsigned char x, unsigned char y);
void LCD_Write_Char(unsigned char x, unsigned char y, unsigned char Data);
void LCD_Write_String(unsigned char x, unsigned char y, unsigned char *s);
void LCD_Init(void);
#endif
// lcd.c
#include "lcd.h"

sbit RS = P1 ^ 0;
sbit RW = P1 ^ 1;
sbit EN = P2 ^ 5;

#define DataPort P0

/***************************
 * OSC 12 MHz
 * T = tx2 + 5us
 ****************************/
void DelayUs2x(unsigned char t)
{
    while (--t)
        ;
}

void DelayMs(unsigned char t)
{
    while (--t)
    {
        // delay about 1ms
        DelayUs2x(245);
        DelayUs2x(245);
    }
}

bit LCD_Check_Busy(void)
{
    RS=0; // RS_CLR;
    RW=1; // RW_SET;
    EN=0; // EN_CLR;
    _nop_();
    EN=1; // EN_SET;
    return (bit)(DataPort & 0x80); // 0x80; 0x00;
}

void LCD_Write_Com(unsigned char com)
{
    while (LCD_Check_Busy())
        ;
    RS=0; // RS_CLR; // RS=L
    RW=0; // RW_CLR; // RW=L
    EN=1; // EN_SET; // EN=H
    DataPort = com;
    _nop_(); // wait for buffer clearance
    EN=0; // EN_CLR; // EN=L
}

void LCD_Write_Data(unsigned char Data)
{
    while (LCD_Check_Busy())
        ;
    RS=1; // RS_SET;
    RW=0; // RW_CLR;
    EN=1; // EN_SET;
    DataPort = Data;
    _nop_();
    EN=0; // EN_CLR;
}

void LCD_Clear(void)
{
    LCD_Write_Com(0x01);
    DelayMs(5);
}

void LCD_Set_Cursor(unsigned char x, unsigned char y) {
    if (y == 0)
    {
        LCD_Write_Com(0x80 + x); // Line 1 x=0H~27H 设置数据地址指针
    }
    else
    {
        LCD_Write_Com(0xC0 + x); // Line 2
    }
}
void LCD_Write_String(unsigned char x, unsigned char y, unsigned char *s)
{
    LCD_Set_Cursor(x, y);
    while (*s)
    {
        LCD_Write_Data(*s);
        s++;
    }
}

void LCD_Write_Char(unsigned char x, unsigned char y, unsigned char Data)
{
    LCD_Set_Cursor(x, y);
    LCD_Write_Data(Data);
}

void LCD_Init(void)
{
    LCD_Write_Com(0x38); // display mode settings
    DelayMs(5);
    LCD_Write_Com(0x38);
    DelayMs(5);
    LCD_Write_Com(0x38);
    DelayMs(5);
    LCD_Write_Com(0x38);
    LCD_Write_Com(0x08); // display off
    LCD_Write_Com(0x01); // clear screen
    LCD_Write_Com(0x06); // cursor display settings
    DelayMs(5);
    LCD_Write_Com(0x0C); // open display & cursor set
}
// main.c
#include <reg52.h>
#include <intrins.h>
#include "lcd.h"
#include "led.h"

/**
* HC-SR04 module characters
* DC5V, 15mA
* angle: <= 15 deg
* range: 2~400cm
*/
#define TEMP 26 // ROOM TEMPERATURE
#define WAVE_BASE_SPEED 332 // WAVE SPEED AT 0 celsius m/s
#define DISTANCE(x) (((TEMP * 0.607)+WAVE_BASE_SPEED)*x/2000)
#define RANGE(x) (x/58) // time/58=cm

sbit echo = P2^0;
sbit trig = P2^1;

unsigned char count;
long int distance;


void init_timer0(void) {
    TMOD=0x01; // timer 0, mode 1, 16-bit overflow
    TL0=0x66;
    TH0=0xfc;
    ET0=1;
  EA=1;
}

void reset() {
    echo = 0;
    trig = 0;
    count = 0;
    distance = 0;
}

/* send 10us pulse */
void trig_pulse() {
    trig = 1;
    delay(1);
    trig = 0;
}

unsigned int trig_capture() {
    unsigned char low;
    unsigned int high,y;
    TR0=1; // start timer
    while(echo); // wait response
    TR0=0;
    low = TL0;
    high= TH0;
    y = (high <<8 ) + low;
    y -= 0xfc66; // minus start
    return y + 1000 * count;
}

long int measure() {
    reset();
    trig_pulse();
    while(echo ==0);
    distance = trig_capture();
    TH0=0xfc;
    TL0=0x66;
    delay(30);
    distance = DISTANCE(distance);
    return distance;
}

void show_distance(int num) {
    unsigned char b,c,d,e;
    b = (num/1000);
    c = (num/100)%10;
    d = (num/10)%10;
    e = num%10;
    LCD_Set_Cursor(0, 1);
    LCD_Write_Data(b+48);
    LCD_Write_Data(c+48);
    LCD_Write_Data(d+48);
    LCD_Write_Data(46); // . ASCII
    LCD_Write_Data(e+48);
    LCD_Write_Data(99); // c ASCII
    LCD_Write_Data(109); // m ASCII
}

long int measure_distance(void) {
    unsigned char l;
    unsigned int h,y;
    TR0 = 1;
    while(echo);
    TR0 = 0;
    l = TL0;
    h = TH0;
    y = (h<<8) + l;
    y = y - 0xfc66;
    distance = y + 1000 * count;
    TH0=0xfc;
    TL0=0x66;
    delay(30);
    //distance = 0.17 * distance;
    return DISTANCE(distance);
}
void measure1() {
    reset();
    while(1) {
        trig_pulse();
        while(echo == 0);
        distance = measure_distance();
        show_distance(distance);
        delay(200);
        reset();
    }
}
void measure2() {
    while(1) {
        distance = measure();
        show_distance(distance);
        delay(200);
    }
}
void main() {
    LCD_Init();
  LCD_Clear();
    init_timer0();
    LCD_Write_String(0, 0, "Distance:");
    // LCD_Write_String(0, 1, "No HT-SR04!!!");
    while(1) {
        distance = measure();
        show_distance(distance);
        delay(200);
    }
}


void timer0 (void) interrupt 1
{
    TF0 = 0;
    TL0 = 0x66;
    TH0 = 0xfc;
    if (count == 18) { // ultrasonic echo pulse width < 18ms;
        TR0 = 0;
        TL0 = 0x66;
        TH0 = 0xfc;
        count = 0;
    }
}

Page Source